#!/bin/sh
#
# Install script for Rockey4 
#

echo
echo Install Rockey4 SDK tools...
echo

#check whether the current user is root!
echo	Check whether the current user is root!
if test $(id -ur) != 0; then
	echo
	echo "Warning: You should logon as root user!"
	echo
	exit -1
fi
echo	Current user is root!
echo

#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
	echo 
	echo "Warning: It need proc and usbdevfs file system support!"
	echo
	exit -1
fi
echo	System enable proc and usbdevfs filesystem!
echo

#Install the Rockey4 SDK tools
echo	Install the Rockey4 SDK tools
mkdir -p /usr/local/include
mkdir -p /usr/local/lib
cp include/rockey.h /usr/local/include/
cp api/lib*	/usr/local/lib/
ln -sf /usr/local/lib/librockeyapi.so.0.1 /usr/local/lib/librockeyapi.so
cp api/java/*.so /usr/local/lib
/sbin/ldconfig

#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/librockeyapi.so.0.1
		fi
	fi
fi

#Revise usb hotplug script for regular user can use Rockey4 device
if [ -f /etc/hotplug/usb.agent ]; then
	declare -i start=0
        declare -i end=0
        declare -i headline=0
        declare -i endline=0
	start=`cat /etc/hotplug/usb.agent -n | grep "PRO=\`echo $PRODUCT | cut -d/ -f1\`" | head -1 | cut -f1`
        if [ $start == 0 ]; then
		cp /etc/hotplug/usb.agent /etc/hotplug/usb.agent.bak
                grep "hotplug.functions" /etc/hotplug/usb.agent > /dev/null
                if [ $? = 0 ]; then
                	start=`cat /etc/hotplug/usb.agent -n | grep "hotplug.functions" | head -1 | cut -f1`
                	end=`cat /etc/hotplug/usb.agent | wc -l`
                	headline=$start
                	endline=$end-$start
                	head -n $headline /etc/hotplug/usb.agent.bak > /etc/hotplug/usb.agent	
			echo						    >> /etc/hotplug/usb.agent 
			echo	"PRO=\`echo \$PRODUCT | cut -d/ -f1\`"      >> /etc/hotplug/usb.agent 
			echo	"if [ \$PRO == \"96e\" ]; then"	 	    >> /etc/hotplug/usb.agent 
			echo	chmod +666 \$DEVICE			    >> /etc/hotplug/usb.agent 
			echo	fi					    >> /etc/hotplug/usb.agent 
			echo						    >> /etc/hotplug/usb.agent 
                	tail -n $endline /etc/hotplug/usb.agent.bak                   >> /etc/hotplug/usb.agent	
        	else
			start=`cat /etc/hotplug/usb.agent -n | grep "#!/bin/sh" | head -1 | cut -f1`
                        end=`cat /etc/hotplug/usb.agent | wc -l`
                        headline=$start+1
                        endline=$end-$start-1
                        head -n $headline /etc/hotplug/usb.agent.bak > /etc/hotplug/usb.agent
                        echo                                                >> /etc/hotplug/usb.agent
                        echo    "PRO=\`echo \$PRODUCT | cut -d/ -f1\`"      >> /etc/hotplug/usb.agent
                        echo    "if [ \$PRO == \"96e\" ]; then"             >> /etc/hotplug/usb.agent
                        echo    chmod +666 \$DEVICE                         >> /etc/hotplug/usb.agent
                        echo    fi                                          >> /etc/hotplug/usb.agent
                        echo                                                >> /etc/hotplug/usb.agent
                        tail -n $endline /etc/hotplug/usb.agent.bak                   >> /etc/hotplug/usb.agent
		fi
	fi    
else
	if [ -d /etc/udev/rules.d/ ]; then
		cp ft.rules /etc/udev/rules.d/
	else
		echo Please install hotplug or udev tools!!!
	fi
fi


echo Install finished!

