#! /bin/sh
#
# netborder-ss7: Sarts netborder-ss7 daemon
#
# chkconfig: 345 96 02
# processname: nsg
# description: Netborder SS7 init script
# config:
# Author: Sangoma

NBESS7_PROD_NAME="Netborder SS7 Gateway"
NBESS7_SERVICE_NAME=nsg
NBESS7_CONF=/etc/nsg/nsg.conf
#NBESS7_OPTIONS="-core -hp -nc -nonat"
NBESS7_OPTIONS="-core -hp -nonat"
NBESS7_USLEEP=500000
NBESS7_USLEEP_TIMEOUT=$(((15*1000000)/$NBESS7_USLEEP))

NBESS7_TMPFS_OPTIMIZATION=NO
NBESS7_TMPFS_LOG_BACKUP=NO
NBESS7_TMPFS_LOG_BACKUP_LIMIT=20
NBESS7_TMPFS_LOG_DIR=/dev/shm/nsg/log
NBESS7_TMPFS_DB_DIR=/dev/shm/nsg/db


# Source function library.
. /etc/init.d/functions

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

export LD_LIBRARY_PATH="$NSG_INSTALLDIR/lib"
PID_FILE=$NSG_INSTALLDIR/run/nsg.pid
NBESS7_BINARY=$NSG_INSTALLDIR/bin/$NBESS7_SERVICE_NAME
NBESS7_STOPFILE=$NSG_INSTALLDIR/.stopping

nbess7_tmpfs_optimization()
{
	local home=$(pwd)
	if [ "$NBESS7_TMPFS_OPTIMIZATION" != "YES" ]; then
		return 0
	fi
	if [ ! -e /dev/shm ]; then
		return 0
	fi
	
	cd $NSG_INSTALLDIR;	
	if [ ! -e $NBESS7_TMPFS_LOG_DIR ]; then
		mkdir -p $NBESS7_TMPFS_LOG_DIR
	fi
	if [ ! -e $NBESS7_TMPFS_DB_DIR ]; then
		mkdir -p $NBESS7_TMPFS_DB_DIR
	fi
	if [ ! -h log ]; then
		if [ -e log ]; then
			rm -rf log_persist;
			mv log log_persist;
		fi
		ln -s $NBESS7_TMPFS_LOG_DIR log
	fi
	if [ ! -h db ]; then
		if [ -e db ]; then
			rm -rf db_persist;
			mv db db_persist;
		fi
		ln -s $NBESS7_TMPFS_DB_DIR db
	fi

	cd $home
	return 0
}

nbess7_tmpfs_log_backup()
{
	local home=$(pwd)
	local cnt=0
	if [ "$NBESS7_TMPFS_LOG_BACKUP" != "YES" ]; then
		return 0
	fi

	cd $NSG_INSTALLDIR;

	if [ ! -d log_persist ]; then
		mkdir log_persist
	fi

	if [ -h log ] && [ -d log_persist ]; then
		cp -rf log/* log_persist/
		cd log_persist
		logs=`ls -lt sangomagw.log.* | awk {'print $9'} | xargs`
		cnt=0	
		for log in $logs
		do
			cnt=$((cnt+1))
			if [ $cnt -gt $NBESS7_TMPFS_LOG_BACKUP_LIMIT ]; then
				rm -f $log;
			fi
		done
		
	fi

	cd $home
	return 0
}


run_nbess7()
{
	local nbess7cmd="$NBESS7_BINARY -base $NSG_INSTALLDIR -conf $NSG_INSTALLDIR/conf -log $NSG_INSTALLDIR/log -run $NSG_INSTALLDIR/run -db $NSG_INSTALLDIR/db -mod $NSG_INSTALLDIR/mod $NBESS7_OPTIONS"

	mydate=`date`
	echo "[$mydate] Starting $nbess7cmd" >> $NSG_INSTALLDIR/init.log
	while :; do
		$nbess7cmd &> /dev/null
		EXITSTATUS=$?
		mydate=`date`
		if test "x$EXITSTATUS" = "x0" ; then
			exit 0
		elif test "x$EXITSTATUS" = "x1" ; then
			echo "[$mydate] $nbess7prog ended due module loading error" >> $NSG_INSTALLDIR/init.log
			exit 1
		elif test "0$EXITSTATUS" -gt "128" ; then
			EXITSIGNAL=$(($EXITSTATUS - 128))
			echo "[$mydate] $nbess7prog ended with status $EXITSTATUS with signal $EXITSIGNAL" >> $NSG_INSTALLDIR/init.log
		else
			echo "[$mydate] $nbess7prog ended with status $EXITSTATUS" >> $NSG_INSTALLDIR/init.log
		fi

		# if the app segfaults or errors on shutdown, no point in restarting
		if [ -f $NBESS7_STOPFILE ] ; then
			echo "[$mydate] Not restarting $nbess7prog ($nbess7cmd) due to stop file" >> $NSG_INSTALLDIR/init.log
			rm -f $NBESS7_STOPFILE
			if [ -f $PID_FILE ] ; then
				# the process most likely died violently, we must clean the PID ourselves
				rm $PID_FILE
			fi
			nbess7_tmpfs_log_backup
			exit 0
		fi

		# if there is no PID file, then this is a clean exit (most likely)
		if [ ! -f $PID_FILE ]; then
			echo "[$mydate] $nbess7prog ended with no pid file" >> $NSG_INSTALLDIR/init.log
			exit $EXITSTATUS
		fi

		sleep 1
		mydate=`date`
		echo "[$mydate] Restarting $nbess7cmd" >> $NSG_INSTALLDIR/init.log
		# the process most likely died violently, we must clean the PID ourselves
		rm $PID_FILE

		nbess7_tmpfs_log_backup
	done
		
	nbess7_tmpfs_log_backup
	exit 0
}

nbess7_setlimits() {
        ulimit -c unlimited
        ulimit -d unlimited
        ulimit -f unlimited
        ulimit -i unlimited
        ulimit -n 999999
        ulimit -q unlimited
        ulimit -u unlimited
        ulimit -v unlimited
        ulimit -x unlimited
        ulimit -s 244
        ulimit -l unlimited
        return 0
}


start()
{
	nbess7pid=`pidof $NBESS7_SERVICE_NAME`
	if [ ! -z "$nbess7pid" ]
	then
		echo -n $"$NBESS7_PROD_NAME is already started:"
		failure
		echo 
		exit 1
	fi

	echo -n $"Starting $NBESS7_PROD_NAME:"
	rm -f $NBESS7_STOPFILE
	nbess7_setlimits	
	nbess7_tmpfs_optimization
	nbess7_tmpfs_log_backup

	#$NBESS7_BINARY -base $NSG_INSTALLDIR -conf $NSG_INSTALLDIR/conf -log $NSG_INSTALLDIR/log -run $NSG_INSTALLDIR/run -db $NSG_INSTALLDIR/db -mod $NSG_INSTALLDIR/mod $NBESS7_OPTIONS
	run_nbess7 &
	RETVAL=$?
	[ "$RETVAL" = 0 ] && touch /var/lock/subsys/$NBESS7_SERVICE_NAME

	
	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
			nbess7pid=`pidof $NBESS7_SERVICE_NAME`
			if [ -z "$nbess7pid" ]
			then
				RETVAL=1
				rm -f /var/lock/subsys/$NBESS7_SERVICE_NAME
				failure
				break
			fi
			if [ -f $PID_FILE ]
			then
				success
				break
			fi
			usleep $NBESS7_USLEEP
		done
	else
		failure
	fi

	echo 
}

stop()
{
	echo -n $"Stopping $NBESS7_PROD_NAME:"
	nbess7pid=`pidof $NBESS7_SERVICE_NAME`
	if [ -z "$nbess7pid" ]
	then
		echo -n $"$NBESS7_PROD_NAME is not started:"
		failure
		echo 
		exit 1
	fi

	touch $NBESS7_STOPFILE
	$NBESS7_BINARY -stop -base $NSG_INSTALLDIR -conf $NSG_INSTALLDIR/conf -log $NSG_INSTALLDIR/log -run $NSG_INSTALLDIR/run -db $NSG_INSTALLDIR/db -mod $NSG_INSTALLDIR/mod 
	RETVAL=$?
	if [ "$RETVAL" = 0 ]
	then
		tcnt=0;
		# at this point just finish the routine if we stop seeing the process 
		while true
		do
			nbess7pid=`pidof $NBESS7_SERVICE_NAME`
			if [ -z "$nbess7pid" ]
			then
				success
				break
			fi
			usleep $NBESS7_USLEEP
			tcnt=$((tcnt+1))
			if [ $tcnt -gt $NBESS7_USLEEP_TIMEOUT ]; then
				eval "kill -9 $nbess7pid >> /dev/null 2>> /dev/null"
				success
                break
			fi	  
			#echo "Waiting for pid down $tcnt/$NBESS7_USLEEP_TIMEOUT"
		done
		rm -f /var/lock/subsys/$NBESS7_SERVICE_NAME
		nbess7_tmpfs_log_backup
	else
		rm -f $NBESS7_STOPFILE
	fi
	echo 
}

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


