#!/bin/sh
args="$#"
if [ $args -ne  2  ]
then
        echo "Usage /opt/egurkha/bin/auto_restart [ install | uninstall ] [ manager | agent ] "
        exit
fi

install_mode=$1
entity=$2

if [  $install_mode = "install"  -o    $install_mode = "uninstall" ]
then
        echo ""
else
        echo "Usage /opt/egurkha/bin/auto_restart [ install | uninstall ] [ manager | agent ] "
        exit
fi

if [  $entity = "manager"  -o    $entity = "agent" ]
then
        echo ""
else
        echo "Usage /opt/egurkha/bin/auto_restart [ install | uninstall ] [ manager | agent ] "
        exit
fi
if [ -f /usr/ucb/whoami ]
then
userName=`/usr/ucb/whoami`
if [ ! $userName = "root"  ]
then
	echo "Current user '$userName' does not have permission to execute this script"
        echo "Only super user can execute this script !"
        exit
fi
fi

if [ $entity = "agent" ]
then

	if [ $install_mode = "install"  ]
	then
       	 	cp /opt/egurkha/bin/rc.egurkha /etc/init.d/egurkha
       	 	chmod 755 /etc/init.d/egurkha

		if [ -h "/etc/rc3.d/K99egurkha"  ]
		then
        		unlink /etc/rc3.d/K99egurkha
		fi

		if [ -h "/etc/rc3.d/S99egurkha"  ]
		then
        		unlink /etc/rc3.d/S99egurkha

		fi

        	ln -s /etc/init.d/egurkha /etc/rc3.d/K99egurkha
		ln -s /etc/init.d/egurkha /etc/rc3.d/S99egurkha
        	echo " Successfully installed the auto_restart feature for the eG Agent "
	else
		rm -f /etc/rc3.d/K99egurkha
		rm -f /etc/rc3.d/S99egurkha
		rm -f /etc/init.d/egurkha
        	echo " Successfully uninstalled the auto_restart feature for the eG Agent "
	fi
fi
if [ $entity = "manager" ]
then

	if [ $install_mode = "install"  ]
	then
       	 	cp /opt/egurkha/bin/rc.egurkha_manager /etc/init.d/egurkha_manager
       	 	chmod 755 /etc/init.d/egurkha_manager

		if [ -h "/etc/rc3.d/K99egurkha_manager"  ]
		then
        		unlink /etc/rc3.d/K99egurkha_manager
		fi

		if [ -h "/etc/rc3.d/S99egurkha_manager"  ]
		then
        		unlink /etc/rc3.d/S99egurkha_manager

		fi

        	ln -s /etc/init.d/egurkha_manager /etc/rc3.d/K99egurkha_manager
		ln -s /etc/init.d/egurkha_manager /etc/rc3.d/S99egurkha_manager
        	echo "Successfully installed the auto_restart feature for the eG Manager "
	else
		rm -f /etc/rc3.d/K99egurkha_manager
		rm -f /etc/rc3.d/S99egurkha_manager
		rm -f /etc/init.d/egurkha_manager
        	echo "Successfully uninstalled the auto_restart feature for the eG Manager "
	fi
fi
