#!/bin/sh
# Copyright 2009-2013 NetSTAR Inc. All rights reserved.
#

###
# chkconfig: 345 85 15
# description: GCFSDK daemon
# processname: gcf1d
###

### BEGIN INIT INFO
# Provides:          gcf1d
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: GCFSDK daemon
### END INIT INFO

prefix=/usr/local/gcf1

conf=${prefix}/etc

pidfile=${prefix}/var/gcf1d.pid
outfile=${prefix}/var/gcf1d.out
etcfile=${prefix}/etc/gcf1.conf
propertiesfile=${prefix}/etc/properties.conf
name=gcf1d
scriptname=/etc/init.d/$name

[ -f ${etcfile} ] || exit 1
[ -x ${prefix}/sbin/gcf1d ] || exit 1
[ -f ${propertiesfile} ] || exit 1

LINE_ULIMIT_N_COUNT=`grep -m1 ^ULIMIT_N_COUNT= ${propertiesfile} 2>/dev/null`
RET=$?

if [ ${RET} -ne 0 ]; then
    ULIMIT_N_COUNT=4096
else
    export "${LINE_ULIMIT_N_COUNT}"
fi

ulimit -n ${ULIMIT_N_COUNT} > /dev/null 2>&1

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${prefix}/lib

is_running()
{
	if [ -f ${pidfile} ]; then
		PID=`cat ${pidfile} 2>/dev/null`
		
		if [ -d /proc/$PID ]; then
			if [ -f /proc/$PID/cmdline ]; then
				CMDLINE=`cat /proc/$PID/cmdline 2>/dev/null | tr -d '\0'`;
				
				case ${CMDLINE} in
					*${name}*)
						return 0;
						;;
					*)
						;;
				esac
			fi
		fi
	fi
	
	return 1
}

wait_stop()
{
	i=0
	while [ $i -lt 30 ];
	do
	 		
		if [ $i = '0' ]; then
    		echo -n "..."
		else
      		echo -n "."
	 	fi
		
		i=$(($i+1))
		sleep 1
		
		is_running
		
		if [ ! $? -eq 0 ]; then
			return 0
		fi
	done
	
	return 1
}

wait_start()
{
	i=0
	while [ $i -lt 30 ];
	do
	 		
		if [ $i = '0' ]; then
    		echo -n "..."
		else
      		echo -n "."
	 	fi
		
		i=$(($i+1))
		sleep 1
		
		is_running
		
		if [ $? -eq 0 ]; then
			return 0
		fi
	
	done
	
	return 1
}

case "$1" in
start)
	
	is_running
	
	if [ $? -eq 0 ]; then
		echo "Warning: $name is already running."
		exit 1
	fi
	
	echo -n "Starting $name services: "
	nohup ${prefix}/sbin/gcf1d ${conf} ${pidfile} > ${outfile} 2>&1 &
	
	wait_start
	
	if [ ! $? -eq 0 ]; then
		echo "failed"
		echo "Error: $name could not start. error message:"
		echo 
		cat ${outfile}
		
		exit 1
	fi
	
	echo "succeeded"
	
	;;
	
stop)
	
	is_running
	
	if [ ! $? -eq 0 ]; then
		echo "Warning: $name is not running."
		exit 1
	fi

	echo -n "Stopping $name services: "
	
	if [ -f ${pidfile} ]; then
		kill `cat ${pidfile} 2>/dev/null` 2>/dev/null
		
		wait_stop
		
		if [ ! $? -eq 0 ]; then
			
			echo -n " retry ... "
			
			kill -9 `cat ${pidfile} 2>/dev/null` 2>/dev/null
			
			wait_stop
			
			if [ ! $? -eq 0 ]; then
				echo "Error: $name could not stop."
				exit 1
			fi
			
		fi
		
	fi
	
	rm -f ${pidfile} > /dev/null 2>&1
	echo "stopped"
	
	;;
	
kill)

	is_running
	
	if [ ! $? -eq 0 ]; then
		echo "Warning: $name is not running."
		exit 1
	fi
	
	echo -n "Shutting down $name services: "
	
	if [ -f ${pidfile} ]; then
	
		kill -9 `cat ${pidfile} 2>/dev/null` 2>/dev/null
		
		wait_stop
		
		if [ ! $? -eq 0 ]; then
			echo "Error: $name could not stop."
			exit 1
		fi
	fi
	
	rm -f ${pidfile} > /dev/null 2>&1
	echo "killed"
	
	;;
	
restart)
	$0 stop
	$0 start
	
	;;
	
status)

	is_running
	
	if [ ! $? -eq 0 ]; then
		echo "$name is not running."
		exit 1
	fi
	
	PID=`cat ${pidfile} 2>/dev/null`
	echo "$name is running (pid $PID)."
	
	;;
	
*)
	echo "Usage: $scriptname {start|stop|restart|kill|status}"
	exit 1
	;;
esac

exit 0
