#!/bin/bash
/usr/bin/clear
echo
echo	"	   ISA/PCI ISICOM Driver Installation for Linux 2.2.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
case $Response in

	y|Y)
		make "verinfo=y"
		;;

	*)
		make

esac
#if [ $Response = "y" ]
#then
#	make "verinfo=y"
#elif [ $Response = "Y" ]
#then
#	make "verinfo=y"
#else
#	make
#fi
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

CWD=$(pwd)

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

cd $CWD

echo	"Creating device for ISI Firmware Loader..."
MAJOR=10
MINOR=155
	mknod /dev/isictl c $MAJOR $MINOR

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
		mknod /dev/ttyM$CARD$PORT c  $MAJOR $MINOR
		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 $DESTINATION/isicom.o" >> $DESTINATION/ISICOMStart
	echo "No ISA cards have been configured."
else
	echo -n "insmod $DESTINATION/isicom.o  " >> $DESTINATION/ISICOMStart
	echo  " $IOPARAMS $IRQPARAMS" >> $DESTINATION/ISICOMStart
fi

echo  "$DESTINATION/firmld"	>> $DESTINATION/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
