#!/bin/sh

#UniOTP install shell

#root path
UNIOTP_ROOT="/usr/local/UniOTP/1.0"
#authentication server and support file
UNIOTP_ROOT_SRV="$UNIOTP_ROOT/server"
#log location
UNIOTP_ROOT_LOG="$UNIOTP_ROOT/log"
#configuration file
UNIOTP_ROOT_CONF="$UNIOTP_ROOT/config"
#database initialization sql script
UNIOTP_ROOT_INITSQL="$UNIOTP_ROOT/initsqlscript"
#documents
UNIOTP_ROOT_MANUALDOC="$UNIOTP_ROOT/document"
#sdk agent
UNIOTP_ROOT_SDK="$UNIOTP_ROOT/sdk"
UNIOTP_ROOT_AGENT="$UNIOTP_ROOT/Agent"
#management system
UNIOTP_ROOT_MGSYS="$UNIOTP_ROOT/UniOTPMGS"

#check owner
owner=""
while [ x"$1" != x ];do
	case $1 in
		-o) if [ x"$owner" = x ];then
			owner="$2"
		    else
			owner="${2}${owner}"
		    fi
		    ;;
		-g) if [ x"$owner" = x ];then
			owner=":$2"
		    else
			owner="$owner:$2"
		    fi
		    ;;
	esac
		shift
		shift
done
#check for root
if [ `id -u` -ne 0 ];then
	echo "you should run the installer as root"
	exit 1;
fi
#display information about UniOTP
echo "Thank you for chose UniOTP One-Time-Password Authentication System"
echo " "
echo "Copyright (C) 2011 SecuTech Solution Inc."
echo " "
echo "Welcome to UniOTP installer"
echo " "
echo "Have you read and agree to all the terms of the license"
echo "included with the package? [y/n]"
read isagree
if [ "$isagree" != "y" ]
then
	echo "Installer will not continue unless you agree to all the terms of the license and answer yes"
	exit 1
fi
echo "checking install dependences..."
echo " "

LDCONFIG='/sbin/ldconfig'
if [ ! -x "$LDCONFIG" ];then
	echo "find ldconfig..."
	LDCONFIG=`whereis ldconfig | awk '{print $2}'`
fi

#find libraries UniOTP dependent to
LIBODBC=`$LDCONFIG -p | awk '{print $4}' | grep libodbc.so`
LIBSTD_CPP=`$LDCONFIG -p | awk '{print $4}' | grep libstdc++.so.6`
LIBSTD_C=`$LDCONFIG -p | awk '{print $4}' | grep libc.so.6`
#check ODBC drive
sleep 1
if [ x"$LIBODBC" = x ];then
	echo "Check ODBC drive				[failed]"
	echo "Please install ODBC drive before installation"
	exit 1
else
	echo "Check ODBC drive				[ok]"
fi


#check libstdc++
needstdcpp=0
sleep 1
if [ x"$LIBSTD_CPP" = x ];then
	LIBSTD_CPP=`locate libstdc++.so.6 | awk '{print $1}' | head -1`
	if [ x"$LIBSTD_CPP" = x ];then
		needstdcpp=1
		echo "Check libstdc++.so.6				[failed]"
		echo ""
		echo "Warning: libstdc++.so.6 can not be found.We will add it for you."
		echo "If any error occurs during UniOTP Server starting, please install"
		echo "libstdc++.so.6 and try again."
	else
		echo "Check libstdc++.so.6				[ok]"
	fi
else
	echo "Check libstdc++.so.6				[ok]"
fi
needstdc=0
sleep 1
if [ x"$LIBSTD_C" = x ];then
	LIBSTD_C=`locate libc.so.6 | awk '{print $1}' | head -1`
	if [ x"$LIBSTD_C" = x ];then
		needstdc=1	chmod 0644 $UNIOTP_ROOT/copyright.txt
		echo "Check libc.so.6					[failed]"
		echo ""
		echo "Warning: libc.so.6 can not be found.We will add it for you."
		echo "If any error occurs during UniOTP Server starting, please install"
		echo "libc.so.6 and try again."
	else
		echo "Check libc.so.6					[ok]"
	fi
else
	echo "Check libc.so.6					[ok]"
fi

echo "Dependences check finish."
echo "Starting install UniOTP on this work station..."
sleep 1

#copy files into the installation path
#make sure the files are not exist in the installation path

if [ $needstdcpp -eq 1 ];then
#copy libstdc++.so.6 into /usr/lib
	cp ./slib/libstdc++.so.6 /tmp/
	if [ ! -x $owner ];then
		chown $owner /tmp/libstdc++.so.6
	fi
	echo "install libstdc++.so.6 successfully!"
fi

if [ $needstdc -eq 1 ];then
#copy libc.so.6 into /usr/lib/
	cp ./slib/libc.so.6 /tmp/
	if [ ! -x $owner ];then
		chown $owner /tmp/libc.so.6
	fi
	echo "install libc.so.6 successfully!"
fi

#UNIOTP root folder
if [ ! -e "/usr/local/UniOTP" ];then
	mkdir "/usr/local/UniOTP"
	if [ ! -x $owner ];then
		chown $owner "/usr/local/UniOTP"
	fi
	chmod 0755 "/usr/local/UniOTP"
fi

if [ ! -e $UNIOTP_ROOT ];then
	mkdir $UNIOTP_ROOT
	if [ ! -x $owner ];then
		chown -R $owner $UNIOTP_ROOT
	fi
fi
#copy copyright file
cp ./copyright.txt $UNIOTP_ROOT/
if [ ! -x $owner ];then
	chown $owner $UNIOTP_ROOT/copyright.txt
fi
	chmod 0644 $UNIOTP_ROOT/copyright.txt

#copy configuration files
if [ -e "$UNIOTP_ROOT_CONF" ];then
	echo "config folder exists.Do you want to replace it? [y/n]"
	read resp
	if [ $resp = "y" ];then
		rm -rf $UNIOTP_ROOT_CONF
		cp -rf ./config $UNIOTP_ROOT/
		if [ ! -x $owner ];then
			chown $owner -R $UNIOTP_ROOT_CONF
		fi
		chmod 0755 $UNIOTP_ROOT_CONF
		chmod 0644 $UNIOTP_ROOT_CONF/*.*
	else
		echo "We do not do any change for otp_config.con"
	fi
else
	cp -rf ./config $UNIOTP_ROOT/
	if [ ! -x $owner ];then
		chown $owner -R $UNIOTP_ROOT_CONF
	fi
	chmod 0755 $UNIOTP_ROOT_CONF
	chmod 0644 $UNIOTP_ROOT_CONF/*.*	
fi

#copy document
if [ -e "$UINOTP_ROOT_MANUALDOC" ];then
	echo "${UNIOTP_ROOT_MANUALDOC} exists.Do you want to replace it? [y/n]"
	read resp
	if [ $resp = "y" ];then
		rm -rf $UNIOTP_ROOT_MANUALDOC
		cp -rf ./document $UNIOTP_ROOT/
		if [ ! -x $owner ];then
			chown $owner -R $UNIOTP_ROOT_MANUALDOC
		fi
		chmod 0755 -R $UNIOTP_ROOT_MANUALDOC
	else
		echo "We do not do any change for $UNIOTP_ROOT_MANUALDOC"
	fi
else
	cp -rf ./document $UNIOTP_ROOT/
	if [ ! -x $owner ];then
		chown $owner -R $UNIOTP_ROOT_MANUALDOC
	fi

	chmod 0755 -R $UNIOTP_ROOT_MANUALDOC
fi

#initsqlscript
cp -rf ./initsqlscript $UNIOTP_ROOT

if [ ! -x $owner ];then
	chown $owner -R $UNIOTP_ROOT_INITSQL
fi
chmod 0755 -R $UNIOTP_ROOT_INITSQL

#log
if [ ! -e $UNIOTP_ROOT_LOG ];then
	mkdir $UNIOTP_ROOT_LOG
fi
if [ ! -x $owner ];then
	chown $owner -R $UNIOTP_ROOT_LOG
fi
chmod 0777 -R $UNIOTP_ROOT_LOG

#agent
echo "Do you want to install UniOTP Agent package on your computer? [y/n]"
read resp
if [ $resp = "y" ];then
	if [ -e $UNIOTP_ROOT_AGENT ];then
		rm -rf $UNIOTP_ROOT_AGENT
	fi
	cp -rf ./Agent $UNIOTP_ROOT
	if [ ! -x $owner ];then
		chown $owner -R $UNIOTP_ROOT_AGENT
	fi
	chmod 0755 -R $UNIOTP_ROOT_AGENT
	echo "UniOTP Agent package has been installed in $UNIOTP_ROOT_AGENT"
else
	echo "We do not install UniOTP Agent package for you"	
fi

#sdk
#echo "Do you want to install UniOTP SDK on you computer? [y/n]"
#read resp
#if [ $resp = "y" ];then
#	if [ -e $UNIOTP_ROOT_SDK ];then
#		rm -rf $UNIOTP_ROOT_SDK
#	fi
#	cp -rf ./sdk $UNIOTP_ROOT
#	if [ ! -x $owner ];then
#		chown $owner -R $UNIOTP_ROOT_SDK
#	fi
#	chmod 0755 -R $UNIOTP_ROOT_SDK
#	echo "UniOTP SDK package has been installed in $UNIOTP_ROOT_SDK"
#else
#	echo "We do not install UniOTP SDK for you."
#fi

#UniOTPMGS
cp -rf ./UniOTPMGS $UNIOTP_ROOT
if [ ! -x $owner ];then
	chown $owner -R $UNIOTP_ROOT_MGSYS
fi
#chmod 0755 -R $UNIOTP_ROOT_MGSYS
#chmod 0777 -R $UNIOTP_ROOT_MGSYS/download
#chmod 0777 -R $UNIOTP_ROOT_MGSYS/smarty_cache
#chmod 0777 -R $UNIOTP_ROOT_MGSYS/smarty_compile
#chmod 0777 -R $UNIOTP_ROOT_MGSYS/upload


#UniOTPSRV
cp ./UniOTPSRV $UNIOTP_ROOT
if [ ! -x $owner ];then
	chown $owner $UNIOTP_ROOT/UniOTPSRV
fi
chmod 0755 $UNIOTP_ROOT/UniOTPSRV

#init.d path
INITDPATH="/etc/rc.d/init.d"
if [ ! -e $INITDPATH ];then
	if [ -e "/etc/init.d" ];then
		INITDPATH="/etc/init.d"
	else
		echo "init.d folder not found.Please input init.d path:"
		read INITDPATH
		if [ ! -e $INITDPATH ];then
			echo "Invalid path"
		fi
	fi
fi

if [ ! -e $INITDPATH ];then
	cp ./UniOTPSRVCtl $UNIOTP_ROOT/
	chmod 0755 $UNIOTP_ROOT/UniOTPSRVCtl
	if [ ! -x $owner ];then
		chown $owner $UNIOTP_ROOT/UniOTPSRVCtl
	fi
	echo "UniOTP will not auto start"
	echo "If you want it start when the computer on power"
	echo "you should copy ${UNIOTP_ROOT}/UniOTPSRVCtl into the init.d folder"
	echo "and create a symbol link of it in rcN.d (N can be 2, 3, 4, 5)"
else
	cp ./UniOTPSRVCtl $INITDPATH/
	chmod 0755 $INITDPATH/UniOTPSRVCtl
	if [ ! -x $owner ];then
		chown $owner $INITDPATH/UniOTPSRVCtl
	fi
	ln -s $INITDPATH/UniOTPSRVCtl /etc/rc2.d/S90UniOTPSRVCtl
	ln -s $INITDPATH/UniOTPSRVCtl /etc/rc3.d/S90UniOTPSRVCtl
	ln -s $INITDPATH/UniOTPSRVCtl /etc/rc4.d/S90UniOTPSRVCtl
	ln -s $INITDPATH/UniOTPSRVCtl /etc/rc5.d/S90UniOTPSRVCtl
fi

echo ""
echo "UniOTP One-Time-Password Authentication System has been installed in $UNIOTP_ROOT."

echo "Thank you again for chose UniOTP product."
