#!/bin/bash

if [ $# -lt 1 ]; then
  echo Usage:  ./install path
  exit 1
fi

KERNEL=$1
DRIVER=$KERNEL/drivers/message/fusion

echo "Update path: " $DRIVER

if [ ! -e $KERNEL/Makefile ]; then
  echo $KERNEL/Makefile not found.
  exit 2
else
  if [ `grep -c "PATCHLEVEL = 5" $KERNEL/Makefile` -gt 0 ]; then
     PATCHLEVEL=5
  elif [ `grep -c "PATCHLEVEL = 4" $KERNEL/Makefile` -gt 0 ]; then
     PATCHLEVEL=4
  elif [ `grep -c "PATCHLEVEL = 2" $KERNEL/Makefile` -gt 0 ]; then
     PATCHLEVEL=4
  fi
fi

if [ ! -d Kernel_2_$PATCHLEVEL ]; then
  echo Kernel_2_$PATCHLEVEL subtree missing! Cannot update.
  exit 3
fi

if [ ! -d $DRIVER ]; then
  echo Path $DRIVER not found.  Creating....
  mkdir -p $DRIVER
  mkdir -p $DRIVER/lsi
elif [ ! -d $DRIVER/lsi ]; then
  rm $DRIVER/*.[ch]
  echo Path $DRIVER/lsi not found.  Creating....
  mkdir -p $DRIVER/lsi
else
  rm $DRIVER/lsi/*.h $DRIVER/*.[ch]
fi
echo Updating $DRIVER/lsi/*.h
cp -f lsi/*.h $DRIVER/lsi
echo Updating core files: $DRIVER/*.[ch]
cp -f *.[ch] $DRIVER
cp ascq_tbl.sh $DRIVER
echo Updating Kernel 2.$PATCHLEVEL specific files
cp -f Kernel_2_$PATCHLEVEL/*.[ch] $DRIVER
cp -f Kernel_2_$PATCHLEVEL/Makefile $DRIVER
if [ "$PATCHLEVEL" = "5" ]; then
    cp -f Kernel_2_$PATCHLEVEL/Kconfig $DRIVER
fi
if [ "$PATCHLEVEL" = "4" ]; then
    cp -f Kernel_2_$PATCHLEVEL/Config.in $DRIVER
fi
