#!/bin/bash
#
#Rockey4ND install script
#
SOVER=so.0.3
script_path=`dirname "$0"`
OSV=`file /bin/ls | cut -c14-15`
echo
echo Installing Rockey4ND 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/rockey.h ] || [ -f /usr/local/lib/libRockey4ND.$SOVER ] || [ -f /usr/local/lib/libJRockey4ND.$SOVER ] || [ -f /usr/local/lib/libRockey4ND.a ]; 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 usbdevfs 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 -p /usr/local/lib
mkdir -p /usr/local/include
cp -f $script_path/include/rockey.h /usr/local/include
cp -f $script_path/api/api$OSV/libRockey4ND.$SOVER /usr/local/lib/
cp -f $script_path/api/api$OSV/java/libJRockey4ND.$SOVER /usr/local/lib/
cp -f $script_path/api/api$OSV/libRockey4ND.a /usr/local/lib/
ln -sf /usr/local/lib/libRockey4ND.$SOVER	/usr/local/lib/libRockey4ND.so
ln -sf /usr/local/lib/libJRockey4ND.$SOVER	/usr/local/lib/libJRockey4ND.so

#check whether enable SELinux and gcc version
GCCVER=`cat /proc/version | awk '{print $7}' | cut -d. -f 1`
if [ $GCCVER = "4" ]; then
	if [ -f /etc/sysconfig/selinux ]; then
		grep "SELINUX=disable" /etc/sysconfig/selinux
		if [ $? == 1 ]; then
			chcon -t texrel_shlib_t /usr/local/lib/*Rockey4ND.so.*
		fi
	fi
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!


