#!/bin/bash
#
#ET199 install script
#
SOVER=so.0.3
script_path=`dirname "$0"`
echo
echo Installing ET199 SDK...
echo

#Check whether the current user is root
if test $(id -ur) != 0; then
	echo
	echo "You should logon as root user!"
	echo
	exit -1
fi
get_confirm()
{
	echo "[yes or no]"
	while true
	do
		read X
		case "$X" in 
			y | yes | Y | Yes | YES ) return 1;;
			n | no | N | No | NO )
				echo "Cancelled"
				return 0;;
			*) echo "Please enter yes or no";;
		esac
	done
}

if [ -f /usr/local/include/et199_32.h ] || [ -f /usr/local/lib/libET199.$SOVER ] || [ -f /usr/local/lib/libET199.a ] || [ -f $JAVA_HOME/jre/lib/i386/libJavaET199.so ] || [ -f $JAVA_HOME/jre/lib/amd64/libJavaET199.so ] || [ -f $JAVA_HOME/lib/ET199Java.jar ]; then

	echo "The file Driver that this program was about to install already exists. Overwrite?"
	if get_confirm ; then
	   exit 1
	fi


fi

#check whether proc and usbfs file system is exist!
echo	Check whether proc and usbfs file system is exist!
if [ ! -f /proc/bus/usb/devices ]; then
	if [ ! -d /dev/bus/usb ]; then
		echo
		echo "Warning: It need proc and usbdevfs file system support!"
		echo
		exit -1
	fi
fi
echo	System enable proc and usbdevfs filesystem!
echo

#Create the /usr/local/lib dir and copy libs to this dir
mkdir -pv /usr/local/lib
mkdir -pv /usr/local/include

#Check the Linux system for 32-bit or 64-bit
LINUXSYS=`file /bin/ls | cut -c14-15`

cp -f $script_path/include/et199_32.h /usr/local/include
cp -f $script_path/api/api$LINUXSYS/libET199.$SOVER /usr/local/lib/
cp -f $script_path/api/api$LINUXSYS/libET199.a /usr/local/lib/
ln -sf /usr/local/lib/libET199.$SOVER	/usr/local/lib/libET199.so


#check whether enable SELinux
TEMP_VAR=`whereis chcon | awk '{print $2}'`
TEMP_VAR2=`whereis selinux | awk '{print $2}'`
if [ $TEMP_VAR ] && [ $TEMP_VAR2 ]; then
	$TEMP_VAR -t texrel_shlib_t /usr/local/lib/*ET199.* &>/dev/null
fi
if [ -f /etc/hotplug/usb.usermap ];then
	grep -l "ft.map" /etc/hotplug/usb.usermap>/dev/null
	if [ $? = 1 ];then
		echo "ft.map 0x0000 0x096e 0x0000 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000000">>/etc/hotplug/usb.usermap
	fi
fi

if [ -d /etc/hotplug/usb/ ];then
	cp -f $script_path/ft.map /etc/hotplug/usb/
fi
if [ -d /etc/udev/rules.d/ ]; then
	cp $script_path/ft.rules /etc/udev/rules.d/
	touch /etc/udev/rules.d/ft_H.rules
	echo "SUBSYSTEM==\"usb\",ATTRS{idVendor}==\"096e\",MODE==\"0666\"">/etc/udev/rules.d/ft_H.rules
fi

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

/sbin/ldconfig

echo Install finished!

	
