#!/bin/sh
#run the installer as root
LDCONFIG="/sbin/ldconfig"
USELIBSTDCPP_SO_5=0
USELIBC_SO_6=0

echo "Welcome to use UniOTP PAM Agent"
if [ 0 -ne `id -u` ];then
	echo "We cannot start the installer unless you"
	echo "run it as root"
	exit 1
fi
if [ ! -x "$LDCONFIG" ];then
	LDCONFIG=`whereis ldconfig | awk '{print $2}'`
	if [ ! -x "$LDCONFIG" ];then
		echo "Installer cannot continue for we can not find ldconfig"
		echo "Installer exit"
		exit 1
	fi
fi
echo "Checking dependent libraries..."
echo "Checking libstdc++.so.5..."
sleep 1
LIBSTDCPP_SO_5=`$LDCONFIG -p | awk '{print $4}' | grep libstdc++.so.5`
if [ x"$LIBSTDCPP_SO_5" = x ];then
	LIBSTDCPP_SO_5=`whereis libstdc++.so.5 | awk '{print $2}'`
fi
if [ x"$LIBSTDCPP_SO_5" = x ] || [ ! "$LIBSTDCPP_SO_5" = "libstdc++.so.5" ];then
	echo "Warning: library libstdc++.so.5 cannot found."
	echo "we will install it for you"
	cp ./libstdc++.so.5 /usr/lib
	USELIBSTDCPP_SO_5=1
else
	echo "Library libstdc++.so.5				[Ok]"
fi

echo "Checking libc.so.6..."
sleep 1
LIBSTDC_SO_6=`$LDCONFIG -p | awk '{print $4}' | grep libc.so.6`
if [ x"$LIBSTDC_SO_6" = x ];then
	LIBSTDC_SO_6=`whereis libc.so.6 | awk '{print $2}'`
fi
if [ x"$LIBSTDC_SO_6" = x ] || [ ! "$LIBSTDC_SO_6" = "libc.so.6" ];then
	echo "Warning: library libc.so.6 cannot found."
	echo "we will install it for you"
	cp ./libc.so.6 /usr/lib
	USELIBC_SO_6=1
else
	echo "Library libc.so.6					[Ok]"
fi
#libuniotp_agent_c.so
echo "Install libuniotp_agent_c.so ..."
cp ./libuniotp_agent_c.so /usr/lib

#copy sample configuration file
echo "Install sample configuration file ..."
cp ./secu_pam_agent.conf /etc/


#copy the
PAM_LIB_DIR=""
if [ -x "/lib/security" ];then
	PAM_LIB_DIR="/lib/security/"
elif [ -x "/usr/lib/security" ];then
	PAM_LIB_DIR="/usr/lib/security"
elif [ -x "/lib64/security" ];then
	PAM_LIB_DIR="/lib64/security"
fi
if [ x"$PAM_LIB_DIR" = x ];then
	echo "Warning: path for pam module library cannot found."
	echo "pam_secu.so for UniOTP PAM Agent has been installed"
	echo "in /usr/lib"
	echo "You should move it into correct directory"
	cp ./pam_secu.so /usr/lib
else
	cp ./pam_secu.so $PAM_LIB_DIR
	echo "pam_secu.so has been installed in $PAM_LIB_DIR"
fi

if [ -x "/etc/selinux" ] || [ -x "/etc/sysconfig/selinux" ];then
	echo "In order to use our PAM Agent, we will change the context of"
	echo "shared libraries used by UniOTP PAM Agent."
	chcon -t textrel_shlib_t $PAM_LIB_DIR/pam_secu.so /usr/lib/libuniotp_agent_c.so
	if [ $USELIBSTDCPP_SO_5 -eq 1 ];then
		chcon -t textrel_shlib_t /usr/lib/libstdc++.so.5
	fi
	if [ $USELIBC_SO_6 -eq 1 ];then
		chcon -t textrel_shlib_t /usrlib/libc.so.6
	fi
fi

echo "Change configuration file now?[y/n]"
read changenow
if [ "y" = $changenow ];then
	vi /etc/secu_pam_agent.conf
else
	echo "UniOTP PAM Agent installation has finished."
	echo "Configuration file(secu_pam_agent.conf) for UniOTP PAM Agent"
	echo "is installed in /etc/"
	echo "Before you restart your computer after you configure an "
	echo "application to use UniOTP One-Time-Password to do authentication,"
	echo "you should confirm you have configure the "
	echo "configuration file(/etc/secu_pam_agent.conf) correcttly!"
	echo "Notice: if UniOTP PAM Agent doesn't work, Please check your SELinux policy"
	echo "if SELinux is set enforcing"
fi
