#!/bin/bash
iname=ftcard
shlib=libftcard.so
rel=`uname -r`
min=`uname -r | cut -f2 -d.`
moddir=/lib/modules
PCSCVER=1.2.0


#
# check if pcsc-lite is installed
#
if [ -x /sbin/pcscd ]; then
	PCSCD=/sbin/pcscd
elif [ -x /usr/local/sbin/pcscd ]; then
	PCSCD=/usr/local/sbin/pcscd
elif [ -x /usr/local/bin/pcscd ]; then
	PCSCD=/usr/local/bin/pcscd
elif [ -x /usr/sbin/pcscd ]; then
	PCSCD=/usr/sbin/pcscd
else
	PCSCD=null
fi


if [ $PCSCD = "null" ]; then
	echo Warning: It needs pcsc-lite supports.
	echo Warning: PCSC-Lite is not installed.
	echo Please install pcsc-lite-1.2.0 or pcsc-lite-1.2.9* firstly.
	echo Install Failure!
	exit -1
fi

cat > /usr/local/bin/ftcard.sh << eof
#!/bin/bash
#
#auto run script for ePass2000 Driver
#

/sbin/modprobe -s ftcard
sleep 1 
eof

chmod +x /usr/local/bin/ftcard.sh
                                                                                                                             
PCSCVER=`$PCSCD -v | cut -c 19-23 | grep "1.2"`
if [ $PCSCVER == 1.2.9 ]; then

grep -q $shlib /etc/reader.conf 2> /dev/null
if [ $? != 0 ]; then
  cp /etc/reader.conf /etc/reader.conf.bak 2> /dev/null
  cat >> /etc/reader.conf << eof
FRIENDLYNAME		"FT SCR2000"
DEVICENAME		:GEN_SMART_RDR	
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x10000001

FRIENDLYNAME		"FT SCR2000"
DEVICENAME		:GEN_SMART_RDR
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x20000001

FRIENDLYNAME		"FT SCR2000"
DEVICENAME		:GEN_SMART_RDR
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x30000001

FRIENDLYNAME		"FT SCR2000"
DEVICENAME		:GEN_SMART_RDR
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x40000001

eof
fi

else

grep -q $shlib /etc/reader.conf 2> /dev/null
if [ $? != 0 ]; then
  cp /etc/reader.conf /etc/reader.conf.bak 2> /dev/null
  cat >> /etc/reader.conf << eof
FRIENDLYNAME		"FT SCR2000"
DEVICENAME		USBTOKEN
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x10000001

FRIENDLYNAME		"FT SCR2000"
DEVICENAME		USBTOKEN
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x20000001

FRIENDLYNAME		"FT SCR2000"
DEVICENAME		USBTOKEN
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x30000001

FRIENDLYNAME		"FT SCR2000"
DEVICENAME		USBTOKEN
LIBPATH			/usr/local/lib/$shlib
CHANNELID		0x40000001

eof
fi

fi

cp ./Driver/ifdapi/$PCSCVER/$shlib /usr/local/lib

grep -q "/usr/local/lib" /etc/ld.so.conf 2> /dev/null
if [ $? != 0 ]; then
  cp /etc/ld.so.conf /etc/ld.so.conf.bak 2> /dev/null
  echo "/usr/local/lib" >> /etc/ld.so.conf
fi

grep -q ftcard.sh /etc/inittab 2> /dev/null
if [ $? != 0 ]; then
	cp /etc/inittab /etc/inittab.bak
	echo "ft:2345:wait:/usr/local/bin/ftcard.sh">>/etc/inittab
fi

if [ $min == 2 ]; then
  mod_min=misc
  if [ ! -d $moddir/$rel/usb ]; then
    mkdir $moddir/$rel/misc 2> /dev/null
  fi 
elif [ $min == 4 ]; then
  mod_min=kernel/drivers/usb
  if [ ! -d $moddir/$rel/kernel/drivers/usb ]; then
    mkdir $moddir/$rel/kernel/drivers/usb 2> /dev/null
  fi
elif [ $min == 6 ]; then
  mod_min=kernel/drivers/usb
  if [ ! -d $moddir/$rel/kernel/drivers/usb ]; then
    mkdir $moddir/$rel/kernel/drivers/usb 2> /dev/null
  fi
fi

if [ $min == 6 ]; then
cp ./Driver/ftcard/ftcard.ko $moddir/$rel/$mod_min/
chmod 644 $moddir/$rel/$mod_min/ftcard.ko
else
cp ./Driver/ftcard/ftcard.o $moddir/$rel/$mod_min/
chmod 644 $moddir/$rel/$mod_min/ftcard.o
fi

if [ $? == 1 ]; then
  echo Could not copy driver-module, exiting
  exit -1
fi

#
#Reslove usb device hotplug bug
#
VER=`uname -r | cut -d. -f1,2`
if [ $VER == "2.4" ]; then
if [ -f /etc/hotplug/usb.agent ]; then
	declare -i start=0
	declare -i end=0
	declare -i headline=0 
	declare -i endline=0
	start=`cat /etc/hotplug/usb.agent -n | grep "PRO=\`echo $PRODUCT | cut -d/ -f1,2\`" | head -1 | cut -f1`
	if [ $start == 0 ]; then
		cp /etc/hotplug/usb.agent /etc/hotplug/usb.agent.bak
		start=`cat /etc/hotplug/usb.agent -n | grep "load_drivers usb $MAP_CURRENT" | head -1 | cut -f1`
		end=`cat /etc/hotplug/usb.agent | wc -l`
		headline=$start-1
		endline=$end-$start+1
		head -n $headline /etc/hotplug/usb.agent.bak > /etc/hotplug/usb.agent
		echo	"PRO=\`echo \$PRODUCT | cut -d/ -f1,2\`"      >> /etc/hotplug/usb.agent 
		echo	"if [ \$PRO == \"96e/5\" ]; then"	      >> /etc/hotplug/usb.agent 
		echo		exit 0				      >> /etc/hotplug/usb.agent 
		echo	fi					      >> /etc/hotplug/usb.agent 

		tail -n $endline /etc/hotplug/usb.agent.bak		      >> /etc/hotplug/usb.agent
	fi
fi
fi


#
# create reader devices
#
grep -q "nodev	devfs" /proc/filesystems 2> /dev/null
if [ $? != 0 ]; then
	mkdir -p /dev/usb 2>/dev/null

	declare -i count=0
	while [ $count -lt 16 ]
	do
	  mknod /dev/usb/ftcard$count c 180 $((200+count)) 2>/dev/null
	  count=$((count+1))
	done
fi

/sbin/ldconfig
/sbin/depmod -a

#Finally, Load kernel module
echo Attempting to load ftcard module
/sbin/rmmod ftcard 2>/dev/null
sh /usr/local/bin/ftcard.sh


