#########################################################################
#            Copyright (C) 2009 SafeNet, Inc.                           #
#                   All Rights Reserved                                 #
#                                                                       #
# loadserv start/stop the Sentinel Protection Server version 7.6.0.     #
# status  - shows the Sentinel Protection Server status.                #
# restart - restart the Sentinel Protection Server.                     #
#                                                                       #
#                                                                       #
#                                                                       #
#########################################################################
#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

SRV_INSTALL_DIR=/opt/sentinel/sentinel_protection_server
SERVER_PORT=6001
HTTP_PORT=6002


###############################################################################
#                      Check for Dependencies
###############################################################################
check_dep()
{
	#check for root
	user_id=`id -u`
	if [ $user_id -ne 0 ]
	then
		echo "ERROR:To operate the server make sure you have Super User (root) permission."  
		echo "Aborted."
		exit
	fi

}

if [ -r /lib/lsb/init-functions ]; then
	. /lib/lsb/init-functions
	logbegin="log_begin_msg"
	logend="log_end_msg"
fi

# Exit if the server binary is NOT available, executable, etc.
test -x /opt/sentinel/sentinel_protection_server/spnsrvlnx || exit 0

start_firewall() 
{
   iptables -I INPUT 1 -p udp --dport $SERVER_PORT -j ACCEPT > /dev/null 2>&1 
   iptables -I INPUT 2 -p udp --dport $HTTP_PORT -j ACCEPT > /dev/null 2>&1
   iptables -I INPUT 3 -p tcp --dport $SERVER_PORT -j ACCEPT > /dev/null 2>&1
   iptables -I INPUT 4 -p tcp --dport $HTTP_PORT -j ACCEPT > /dev/null 2>&1
   rm -f /tmp/firewall >/dev/nul 2>&1
   iptables-save >/tmp/firewall 2>&1

}

# Start function
d_start()
{
	start_firewall
	if [ -f /etc/init.d/loadserv ]
	then
		chmod +x /etc/init.d/loadserv
	fi

	cd /opt/sentinel/sentinel_protection_server
	update-rc.d loadserv defaults >/dev/null 2>&1
	chmod +x /opt/sentinel/sentinel_protection_server/spnsrvlnx

	ps -el|grep spnsrvlnx >/dev/null 2>&1 
	if [ -$? -eq 0 ]
	then
		sleep 1
		echo "Server is already running."
		exit
	else
		./spnsrvlnx $Param
		echo "      Server started successfully."
	fi

}

# Stop function
d_stop()
{
	iptables -D INPUT -p udp --dport $SERVER_PORT -j ACCEPT > /dev/null 2>&1
	iptables -D INPUT -p udp --dport $HTTP_PORT -j ACCEPT > /dev/null 2>&1
	iptables -D INPUT -p tcp --dport $SERVER_PORT -j ACCEPT > /dev/null 2>&1
	iptables -D INPUT -p tcp --dport $HTTP_PORT -j ACCEPT > /dev/null 2>&1
	
	ps -el|grep spnsrvlnx > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		pkill -KILL spnsrvlnx >/dev/null 2>&1 
		echo "      Server stopped successfully."
	else
		sleep 1
		echo "\"Server is not running.\""
		exit 0
	fi
}

serverStatus()
{
	a=`ps -el |grep spnsrvlnx`
	if [ $? -eq 0 ]
	then 
		echo "Sentinel Protection Server is running."
	else
		echo "Sentinel Protection Server is not running."
	fi
}

Param=""
Space=" "
for i in $*
do
	if test $i != start 
	then
		if  test $i != restart 
		then
		Param=$Param$Space$i
		fi
	fi
done

check_dep

case "$1" in
	start)
		echo "Starting Sentinel Protection Server: spnsrvlnx"
		d_start
		sleep 2
		;;
	stop)
		echo  "Stopping Sentinel Protection Server: spnsrvlnx"
		d_stop
		sleep 2
		;;
	restart)
		d_stop
		sleep 1
		d_start
		;;
	status)
		serverStatus
		;;
	
	
	*)
		echo "Usage: loadserv {start|stop|status|restart}"
		exit 1
	;;
esac
exit 0
