#!/bin/sh
SRV_INSTALL_DIR=/opt/safenet_sentinel/common_files/sentinel_keys_server
#! /bin/sh
#########################################################################
#            Copyright (C) 2007 SafeNet, Inc.                           #
#                   All Rights Reserved                                 #
#                                                                       #
# loadserv Start/Stop the Sentinel Keys Server daemon version 1.0.2.    #
# Status - shows the Sentinel Keys Server status.                       #
#                                                                       #
#                                                                       #
#                                                                       #
#########################################################################


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

if [ -r /lib/lsb/init-functions ]; then
	. /lib/lsb/init-functions
	logbegin="log_begin_msg"
	logend="log_end_msg"
#else
#	logbegin="echo -n"
#	logend=`printf "echo .\n"`
fi

# Exit if the daemon binary is NOT available, executable, etc.
test -x /opt/safenet_sentinel/common_files/sentinel_keys_server/SntlKeysSrvrlnx || exit 0

start_firewall() {
	iptables -I INPUT 1 -p udp --dport 7001 -j ACCEPT > /dev/null 2>&1 
	iptables -I INPUT 2 -p udp --dport 7002 -j ACCEPT > /dev/null 2>&1
	iptables -I INPUT 3 -p tcp --dport 7001 -j ACCEPT > /dev/null 2>&1
 	iptables -I INPUT 4 -p tcp --dport 7002 -j ACCEPT > /dev/null 2>&1
	iptables-save >/tmp/firewall 2>&1
}

# Start function
d_start() {
	start_firewall
        chmod +x /etc/init.d/loadserv
        cd /opt/safenet_sentinel/common_files/sentinel_keys_server
        update-rc.d loadserv defaults >/dev/null 2>&1
 
	sudo chmod +x /opt/safenet_sentinel/common_files/sentinel_keys_server/SntlKeysSrvrlnx
cd /opt/safenet_sentinel/common_files/sentinel_keys_server

ps -el|grep SntlKeysSrvrlnx >/dev/null 2>&1 
if [ -$? -eq 0 ]
then
echo "Server is already running!!!"
exit
else
./SntlKeysSrvrlnx
echo "      Server started Successfully!!"
fi
}

# Stop function
d_stop() {
	iptables -D INPUT -p udp --dport 7001 -j ACCEPT > /dev/null 2>&1
	iptables -D INPUT -p udp --dport 7002 -j ACCEPT > /dev/null 2>&1
	iptables -D INPUT -p tcp --dport 7001 -j ACCEPT > /dev/null 2>&1
	iptables -D INPUT -p tcp --dport 7002 -j ACCEPT > /dev/null 2>&1
	start-stop-daemon --stop --quiet --name SntlKeysSrvrlnx
}

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


case "$1" in
	start)
		$logbegin "Starting Sentinel Keys Server: SntlKeysSrvrlnx"
		d_start
		$logend $?
		;;
	stop)
		$logbegin "Stopping Sentinel Keys Server: SntlKeysSrvrlnx"
		d_stop
		$logend $?
		;;
	restart)
		$0 stop
		sleep 1
		$0 start
		;;
	status)
		serverStatus
		;;
	
	
	*)
	log_success_msg "Usage: loadserv {start|stop|restart|status}"
	exit 1
	;;
esac
exit 0
