#!/bin/bash
#
#Rockey2 install script
#

PRODUCT=Rockey2


echo
echo Installing $PRODUCT 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

#check whether proc and usbfs file system is exist!
grep usbfs /proc/filesystems >/dev/null
if [ $? = 0 ];then
	USBFS=usbfs
else
	grep usbdevfs /proc/filesystems>/dev/null
	if [ $? = 0 ];then
		USBFS=usbdevfs
	else 
		echo 
		echo "ERROR: Your system has no usbfs support!"
		echo
		exit -1
	fi
fi

if [ -f /proc/bus/usb/devices ]; then
	umount /proc/bus/usb/
fi

mount -t $USBFS none /proc/bus/usb/ -o devmode=0666 2>/dev/null
if [ $? != 0 ];then
	echo
	echo "ERROR: mount usbfs failed!"
	echo
	exit -1
fi


FILENAME=/etc/fstab.bak
FILENAMENEW=/etc/fstab
TEXT="none			/proc/bus/usb		$USBFS	devmode=0666	 0 0"

cp $FILENAMENEW $FILENAME -f
grep "/proc/bus/usb" $FILENAME
if [ $? = 0 ]; then
	declare -i LINENUM=`grep "/proc/bus/usb" $FILENAME -n|awk -F : '{print $1}'`
	declare -i DELLINE=$LINENUM+1
#	echo $LINENUM+$DELLINE
	sed "$LINENUM"i\ "$TEXT" $FILENAME | \
	sed "$DELLINE"d > $FILENAMENEW
	
else
	echo $TEXT >> $FILENAMENEW
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
mkdir -p /usr/local/lib
mkdir -p /usr/local/include

cp -f ./include/rockey2.h /usr/local/include
cp -f ./api/* /usr/local/lib/


chmod go+rx /usr/local/lib/lib*$PRODUCT*

#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/lib*$PRODUCT.so*
		fi
	fi
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 2>/dev/null

echo Install finished!

