#! /bin/sh
#
# nbess7-webgui: Sarts nbess7-webgui daemon
#
# chkconfig: 345 96 02
# processname: nginx
# description: Netborder SS7 Gateway Web init script
# config:
# Author: Sangoma Technologies

WEB_PROD_NAME="Netborder SS7 Web GUI"
WEB_SERVICE_NAME=nsg-webgui
NSG_CONF=/etc/nsg/nsg.conf

# Source function library.
if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
fi

# pull in sysconfig settings only if NSG_INSTALLDIR is not already present
if [ -z "$NSG_INSTALLDIR" ]
then
	if [ ! -f $NSG_CONF ]
	then
		echo "FATAL: There is no $NSG_CONF file!!"
		exit
	fi
	. $NSG_CONF
fi

# export nginx env variables
export NGX_PREFIX="$NSG_INSTALLDIR/nginx/"
export NGX_SBIN_PATH="$NSG_INSTALLDIR/nginx/sbin/nginx"
export NGX_PID_PATH="$NSG_INSTALLDIR/nginx/run/nginx.pid"
export NGX_ERROR_LOG_PATH="$NSG_INSTALLDIR/nginx/log/error.log"
export NGX_HTTP_LOG_PATH="$NSG_INSTALLDIR/nginx/log/access.log"
PHP_PID="$NSG_INSTALLDIR/nginx/php/bin/running.pid"
					
nsg_php_mon_daemon()
{
	cmd=$1

	if [ ! -e  $NSG_INSTALLDIR/bin/nsg_php_mon ]; then
		return 1
	fi

	eval "killall -9 nsg_php_mon 2> /dev/null"
	if [ $cmd = "start" ];then
		eval "nice $NSG_INSTALLDIR/bin/nsg_php_mon &"
	fi

	return 0;
}

start()
{
	bindaddr=`cat $NSG_INSTALLDIR/nginx/conf/nginx.conf | grep fastcgi_pass | awk '{print $2}' | sed 's/;//g'`
	webpid=`pidof $WEB_SERVICE_NAME`
	if [ ! -z "$webpid" ]
	then
		echo -n $"$WEB_SERVICE_NAME is already started:"
		failure
		echo 
		exit 1
	fi

	echo -n $"Starting $WEB_PROD_NAME: "


	bindip=$(echo $bindaddr | cut -d':' -f1)
	bindport=$(echo $bindaddr | cut -d':' -f2)
	phpcmd="$NSG_INSTALLDIR/nginx/php/bin/spawn-fcgi -f $NSG_INSTALLDIR/nginx/php/bin/nsg-php-cgi \
						    -a $bindip -p $bindport -P $PHP_PID -C 1 &> /dev/null"
	eval $phpcmd
	RETVAL=$?
	if [ "$RETVAL" = 0 ]
	then
		pidphp=`cat $PHP_PID`
		$NSG_INSTALLDIR/nginx/sbin/nsg-webgui  &> /dev/null
		RETVAL=$?
		if [ "$RETVAL" = 0 ]
		then
			# just finish the routine if we stop seeing the process or the pid file
			# is created. If the pid file is never created by the app this script will
			# hang forever (or more likely until the user stops us)
			while true
			do
				webpid=`pidof $WEB_SERVICE_NAME`
				if [ -z "$webpid" ]
				then
					failure
					kill -9 $pidphp
					rm -f $PHP_PID
					break
				fi
				if [ -f $NGX_PID_PATH ]
				then
					touch /var/lock/subsys/$WEB_SERVICE_NAME
					nsg_php_mon_daemon start
					success
					break
				fi
				usleep 500000
			done
		else
			echo -ne "Failed to start nsg-webgui\n"
			kill -9 $pidphp
			rm -f $PHP_PID
			failure
		fi
	else
		echo -ne "Failed to start PHP FCGI, ret = $RETVAL\n"
		failure
	fi
	echo 
}

stop()
{
	RETVAL=0
	echo -n $"Stopping $WEB_PROD_NAME:"

	nsg_php_mon_daemon stop

	if [ -f $PHP_PID ]
	then
		pid=`cat $PHP_PID`
		kill -15 $pid 2> /dev/null
		usleep 200
	else
		RETVAL=1
	fi
	php_pid=`pidof nsg-php-cgi`
	if [ "$php_pid" != "" ]; then
		eval "kill -9 $php_pid > /dev/null 2> /dev/null"
	fi

	if [ -f $NGX_PID_PATH ]
	then
		pid=`cat $NGX_PID_PATH`
		kill -15 $pid 
		usleep 200
	else
		RETVAL=1
	fi
	
	gui_pid=`pidof nsg-webgui`
	if [ "$gui_pid" != "" ]; then
		eval "kill -9 $gui_pid > /dev/null 2> /dev/null"
	fi

	if [ "$RETVAL" = 0 ]
	then
		# just finish the routine if we stop seeing the process or the pid file
		# is created. If the pid file is never created by the app this script will
		# hang forever (or more likely until the user stops us)
		while true
		do
			webpid=`pidof $WEB_SERVICE_NAME`
			if [ -z "$webpid" ]
			then
				break
			fi
			usleep 500000
		done
	fi

	if [ "$RETVAL" = 0 ] ; then
		rm -f /var/lock/subsys/$WEB_SERVICE_NAME
		success
	else
		failure
	fi
	echo 
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status $WEB_SERVICE_NAME
		RETVAL=$?
		;;
	restart)
		stop
		start
		;;
	*)
		echo $"Usage: $0 {start|stop|status}"
		RETVAL=1
esac
exit $RETVAL


