#!/bin/bash
# Configure script for scr3xxx Smart Card Reader Driver
# Version 4.5.1

exp_kern=2.4.20-8
act_kern=`uname -r`
module_path=/lib/modules/$act_kern/kernel/drivers/usb

currdir=`pwd`
dval="release"

if [ 1 = $# ]; then
        if [ "debug" == $1 ]; then
                dval="debug"
        fi
fi

if [ $act_kern != $exp_kern ]
then
	echo "Kernel module has to be recompiled"
	COMPILE=1
else
	COMPILE=0
fi

echo "configure $dval Mode Drivers Installation..."

# Check if pcsclite daemon is installed
if [ -f /usr/lib/libpcsclite.so ]; then
	pcsclib=/usr/lib
	echo "Library is in "$pcsclib
elif [ -f /usr/local/lib/libpcsclite.so ]; then
	pcsclib=/usr/local/lib
	echo "Library is in "$pcsclib
elif [ -d /usr/pcsc/lib ]; then
	pcsclib=/usr/pcsc/lib
	echo "Library is in "$pcsclib
elif [ -d /usr/local/pcsc/lib ]; then
	pcsclib=/usr/local/pcsc/lib
	echo "Library is in "$pcsclib
else
	echo "pcsclite middleware is not installed."
	exit
fi


mkdir -p /usr/local/scm/ini

mkdir -p /usr/local/pcsc/drivers

if test -d /etc/hotplug
then 
  echo "/etc/hotplug present"
else
  echo "Error: /etc/hotplug not found"
  exit
fi

if test -d /etc/hotplug/usb
then
  echo "/etc/hotplug/usb present"
else
  mkdir /etc/hotplug/usb 
  echo "/etc/hotplug/usb directory created"
fi

mkdir -p /dev/usb


echo "Copying files........"	

cp ./contrib/hotplug/scr3xxx /etc/hotplug/usb

mkdir -p /lib/modules/`uname -r`/kernel/drivers/usb

# kernel module file installation 
# if the current module version matches with the version
# for which the driver is compiled in, we dont do a re-compile.
if [ $COMPILE -eq 0 ]; then
	if [ "debug" == $dval ]; then
		cp -f ./contrib/Debug/*.o /lib/modules/`uname -r`/kernel/drivers/usb
		depmod
		echo "Copied Debug mode kernel module"
	else
		cp -f ./contrib/Release/*.o /lib/modules/`uname -r`/kernel/drivers/usb
		depmod
		echo "Copied Release mode kernel module"
	fi	
else
	cd ./src
	make clean
	if [ "debug" == $dval ]; then
			echo "Compilng Debug mode kernel module"
        	make debug=1
	else
			echo "Compilng Release mode kernel module"
        	make
	fi
	make install
	make clean
	cd $currdir
fi

# Installation of the driver bundle.
if [ "debug" == $dval ]; then
        cp -rf ./proprietary/Debug/*.bundle /usr/local/pcsc/drivers/
        echo "Copied Debug bundle"
else
        cp -rf ./proprietary/Release/*.bundle /usr/local/pcsc/drivers/
        echo "Copied Release bundle"
fi

#installation of configuration scripts 
cp -f ./contrib/scr3xxx /etc/init.d/
i=2
while [ $i -le 5 ]
do
	ln -sf /etc/init.d/scr3xxx /etc/rc.d/rc$i.d/S94scr3xxx
	i=`expr $i + 1`	
done
		

echo "                                  :Done"


 
