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

exp_kern=2.6.11-1.1369_FC4
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

if [ -f /sbin/udev ]
then
    echo "udev exists"
    UDEV=1
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 [ $UDEV -eq 0  ]
then
    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
fi

mkdir -p /dev/usb

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

if [ $UDEV -eq 0 ]
then
    echo "Copying hotplug scripts .."
	cp ./contrib/hotplug/scr3xxx /etc/hotplug/usb
fi

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/*.ko /lib/modules/`uname -r`/kernel/drivers/usb
		echo "Copied Debug mode kernel module"
	else
		cp -f ./contrib/Release/*.ko /lib/modules/`uname -r`/kernel/drivers/usb
		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

echo "Building module dependency tree  : please wait...."
depmod
echo "copied proprietary bundles..."

# 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
		
if [ $UDEV -eq 1 ]
then
echo "Updating udev scripts"
cp -rf ./contrib/10-scm.rules /etc/udev/rules.d/10-scm.rules

    echo "                                  :Done"

else
    echo "                                  :Done"
fi


 
