#!/bin/bash
/usr/bin/clear
SBIN_DIRECTORY=/usr/sbin
DEF_SRC_PATH=/usr/src
ISI_VERSION=isicom_3.05
echo
echo	"	 ISA/PCI ISICOM Driver ($ISI_VERSION) Installation for Linux"
echo	"	             (kernel ver 2.2.x and 2.4.x)		   "
echo	"	 ----------------------------------------------------------"
echo
echo
echo	
echo "  The  installation  will now  try to compile the firmware loader and"
echo "  the driver as a module. This requires the 'make' utility, the GNU C"
echo "  compiler and the kernel sources on your system. The compilation will" 
echo "  fail if any of them are missing."
echo 
echo -n "	        Hit <CR> to continue or <Ctrl-C> to quit." 
read
echo
echo
echo -n "Do you need to compile the driver with symbol version information (y/n) ? [n] :"
read Response rest
echo
MAKE_="make"
$MAKE_ clean
case $Response in

	y|Y)
		MAKE_=$MAKE_" verinfo=y"
		;;

	*)

esac

echo -n "Do you need to compile the driver for an SMP machine (y/n) ? [n] :"
read Response rest
echo
case $Response in

	y|Y)
		MAKE_=$MAKE_" smp=y"
		;;

	*)

esac

# modified on 10/05/01 by anil .s to have a single isi installtion for 2.2.x 
# kernel and 2.4.x kernel

KERN_VER=`uname -r`
KERN_PATCH_LVL=`expr substr $KERN_VER 3 1`
if test $KERN_PATCH_LVL -gt 2
then
	MAKE_=$MAKE_" kern_2_4=y"
fi

ls $DEF_SRC_PATH | while read dr; do
	if test -d $DEF_SRC_PATH/$dr
	then
		if test -f $DEF_SRC_PATH/$dr/Makefile
		then
                  plevel=`cat $DEF_SRC_PATH/$dr/Makefile | grep "PATCHLEVEL ="`

		   plevel_num=`expr substr "$plevel" 14 1`
		   #echo " $DEF_SRC_PATH/$dr/Makefile PATCH LEVEL is $plevel_num"
		   if test $KERN_PATCH_LVL -eq $plevel_num
		   then
		   	#echo "Kernel patch level matches....setting path"
			echo $DEF_SRC_PATH/$dr > ./tmp.isi
			break
		   fi
			
		fi
	fi
done
ABS_SRC_PATH=`cat tmp.isi`
rm -r -f ./tmp.isi
echo "Kernel Sources for the current kernel found in $ABS_SRC_PATH"
echo -n "Do you wish to enter a different path to kernel sources ? [n] :"
read Response rest
echo
case $Response in

	y|Y)
		echo -n "Enter the absolute path to the kernel sources :"
		read inc_path rest
		if test -z $inc_path
		then
			echo "Using the kernel source path as $ABS_SRC_PATH..."
			inc_path=$ABS_SRC_PATH
		fi
		;;
	*)
		echo "Using the kernel source path as $ABS_SRC_PATH..."
		inc_path=$ABS_SRC_PATH

esac

#echo "Adding symbolic link to kernel sources in $inc_path.."
ln -s $inc_path /usr/src/isi_linux


$MAKE_

#echo "Removing symbolic link to kernel sources.."
rm -r -f /usr/src/isi_linux

#till here

if [  -f ./isicom.o ]
then
echo
else
	echo "ISICOM driver compilation failed. Quitting installation..."
	exit
fi
if [ -f ./firmld ]
then
echo
else
	echo "ISICOM firmware loader compilation failed. Quitting installation..."
	exit
fi
/usr/bin/clear
echo -n "Enter the destination folder.[/usr/local/ISICOM]:"
read DESTINATION  rest
if [ -z $DESTINATION ] 
then
	DESTINATION=/usr/local/ISICOM
	echo "Using /usr/local/ISICOM as the destination..."
	mkdir -p /usr/local/ISICOM
else
	if [ -d $DESTINATION ]
	then
		echo "Using $DESTINATION as the destination..."
		echo
	else
		echo "Creating $DESTINATION ..."
		if ( mkdir -p $DESTINATION )
		then
			echo
		else
			echo "Error creating $DESTINATION folder. Quitting Installation..."
			exit
		fi
	fi
fi
#/usr/bin/clear
mkdir -p  /usr/local/ISICOM
# firmld expects the firmware files to be in the /usr/local/ISICOM folder
echo	"Copying firmware files to /usr/local/ISICOM..."
cp	./isi*.bin	/usr/local/ISICOM

echo	"Copying ISICOM Driver to $DESTINATION..."
cp	./isicom.o	$DESTINATION

echo	"Copying firmware loader to $DESTINATION..."
cp	./firmld	$DESTINATION

#Copy the reset modem utility to /usr/local/ISICOM
if [ -f ./ISIResetModem ]
then
	echo	"Copying ISIResetModem utility to /usr/local/ISICOM..."
	cp	./ISIResetModem		/usr/local/ISICOM
else
	echo
	echo	"ISIResetModem utility is not present in the current directory.."
	echo 	"Quitting Installation...."
	echo
	exit
fi


CWD=$(pwd)

#get the absolute destn path
cd $DESTINATION
DESTINATION=$(pwd)

cd $CWD

echo	"Creating device for ISI Firmware Loader..."
MAJOR=10
MINOR=155
if [ ! -c /dev/isictl ]; then
	mknod /dev/isictl c $MAJOR $MINOR
fi

echo "Creating ISI ports ttyMxy, x -> Card Number (1-4), y -> Port Number(a-p)..." 
MAJOR=112
MINOR=0
for CARD in 1 2 3 4 
do
	MINOR=$[ 16 * $[ $CARD - 1] ]
	for PORT in a b c d e f g h i j k l m n o p
	do
		if [ ! -c /dev/ttyM$CARD$PORT ]; then
			mknod /dev/ttyM$CARD$PORT c  $MAJOR $MINOR
		fi
		MINOR=$[ $MINOR + 1]
	done
done

#/usr/bin/clear
echo > $DESTINATION/ISICOMStart
chmod 755 $DESTINATION/ISICOMStart
echo -n "IOPARAMS=\"io=" > /tmp/isiio.param
echo -n "IRQPARAMS=\"irq=" > /tmp/isiirq.param
CARD=1
CONFIGURED=0
echo
echo
echo -n "Do you need to configure any ISA ISI Cards (y/n) ? [n] :"
read CONFIG_ISA rest
case $CONFIG_ISA in

	y|Y)

	for CARD in 1 2 3 4
	do
		/usr/bin/clear
		echo "			ISA ISI Card$CARD Configuration"
		echo "			-------------------------------"
		echo
		
		echo "I/O Base addresses from 0x100 to 0x300 are supported and needs"
		echo "to be divisible by 16. Hit <CR> if you do not want to configure"
		echo "this card."
		echo
		echo
		echo -n "Base Address in hexadecimal (0x..):"
		read BASE rest
		if [ -z $BASE ]
		then
			continue
		fi
		if [ $CARD -eq 1 ]
		then
			echo -n "$BASE" >> /tmp/isiio.param
		else
			echo -n ",$BASE" >> /tmp/isiio.param
		fi
		echo 
		echo
		echo -n "Select an unused IRQ from 2,3,4,5,7,10,11,12,15 :"
		read IRQ rest
		if [ $CARD -eq 1 ]
		then
			echo -n "$IRQ" >> /tmp/isiirq.param
		else
			echo -n ",$IRQ" >> /tmp/isiirq.param
		fi
		CONFIGURED=$[$CONFIGURED + 1]
	done
	;;

	*) 
 
esac

echo -n "\"" >> /tmp/isiio.param
echo -n "\"" >> /tmp/isiirq.param
source /tmp/isiio.param
source /tmp/isiirq.param
rm /tmp/isiio.param
rm /tmp/isiirq.param
/usr/bin/clear
if [ $CONFIGURED -eq 0 ]
then
	echo "insmod -o $ISI_VERSION $DESTINATION/isicom.o" >> $DESTINATION/ISICOMStart
	echo "No ISA cards have been configured."
else
	echo -n "insmod -o $ISI_VERSION $DESTINATION/isicom.o  " >> $DESTINATION/ISICOMStart
	echo  " $IOPARAMS $IRQPARAMS" >> $DESTINATION/ISICOMStart
fi

echo  "$DESTINATION/firmld"	>> $DESTINATION/ISICOMStart
if [ -L $SBIN_DIRECTORY/ISICOMStart ]; then
	rm -f  $SBIN_DIRECTORY/ISICOMStart  
fi
ln -s  $DESTINATION/ISICOMStart  $SBIN_DIRECTORY/ISICOMStart

echo	"	The ISA/PCI ISI driver has been successfully installed...."
echo
echo	"To load the driver automatically after Linux has booted up, include"
echo	"the line 		\"$DESTINATION/ISICOMStart\""
echo	"in one the startup scripts that loads the modules on your system."
echo	
echo	"To manually load the driver, configuration for ISA cards need to be"
echo	"passed to the driver as:"
echo
echo	"\"insmod isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4"
echo
echo	"All the PCI cards and their configuration will be autodetected"
echo
echo	"The ports for ISI Cards are \"ttyMxy\", where x->card number (1-4)"
echo	"and y->port number, (a-h) for an 8 port card and (a-d) for a 4 port"
echo	"card."
echo
echo
exit
