#!/bin/bash
#
#Rockey200 install script
#


echo
echo Installing Rockey200 Driver...
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

#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 Install Failure!
		echo
		exit -1
	fi
fi
echo	System enable proc and usbdevfs filesystem!
echo

#
# 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 Please install pcsc-lite firstly.
	echo Install Failure!
	exit -1
fi

PREFIX1=/usr/lib/pcsc/drivers
PREFIX2=/usr/local/pcsc/drivers
PREFIX3=/usr/lib/readers
mkdir -p /usr/lib/pcsc
mkdir -p /usr/lib/pcsc/drivers
mkdir -p /usr/local
mkdir -p /usr/local/pcsc/
mkdir -p /usr/local/pcsc/drivers
mkdir -p /usr/lib/readers
cp -rf Rockey200.bundle $PREFIX1
cp -rf Rockey200.bundle $PREFIX2
cp -rf Rockey200.bundle $PREFIX3

#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 $PREFIX1/Rockey200.bundle/Contents/Linux/*Rockey200.so*
			chcon -t texrel_shlib_t $PREFIX2/Rockey200.bundle/Contents/Linux/*Rockey200.so*
			chcon -t texrel_shlib_t $PREFIX3/Rockey200.bundle/Contents/Linux/*Rockey200.so*
		fi
	fi
fi

if [ -d /etc/udev/rules.d/ ]; then
		cp -f ft.rules /etc/udev/rules.d/
fi


echo Install finished!
echo If pcscd is not running,please run pcscd,
echo else please restart pcscd.
echo

