#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          micasad
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 1 2 3 5
# Default-Stop:
# Short-Description: miCASA daemon
# Description: miCASA daemon
### END INIT INFO

MICASAD_BIN=/usr/sbin/micasad.sh
test -x $MICASAD_BIN || exit 5

. /etc/rc.status

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

function pid_exists()
{
    test -f /var/run/micasad.pid 2> /dev/null
}

function process_running()
{
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service up and running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running (unused)
	# 4 - service status unknown :-(
	# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
	
	# NOTE: checkproc returns LSB compliant status values.
	# checkproc $MICASAD_BIN
	# NOTE: rc_status knows that we called this init script with
	# "status" option and adapts its messages accordingly.
	MICASAD_PROC=`ps -eo pid,cmd | grep /usr/bin/micasad.exe | grep -v grep | awk '{print $1}'`
	echo $MICASAD_PROC
	if [ -z $MICASAD_PROC ]
	then	  
		MICASAD_PROC=`ps -eo pid,cmd | grep -v grep | grep /usr/bin/micasad.exe | awk '{print $2}'`
		echo $MICASAD_PROC
	fi
	
	if [ -n "$MICASAD_PROC" ]
	then
		return 0
	else
		return 1
	fi
	rc_status -v
}

function is_running()
{
    pid_exists && process_running
}

case "$1" in
    start)
		if ! is_running; then \
		    if [ -f /var/run/micasad.pid ]; then \
		        rm -rf /var/run/micasad.pid 
		    fi
		    echo -n "Starting miCASA daemon"
		    $MICASAD_BIN
		    sleep 2
		fi
		# Remember status and be verbose
		rc_status -v

		;;
    stop)
		if is_running; then \
		    echo -n "Shutting down miCASA daemon" 
		    #killproc -TERM $MICASAD_BIN
		    pid=`cat /var/run/micasad.pid` >/dev/null 2>&1
		    kill -s TERM $pid >/dev/null 2>&1
		    rm -rf /var/run/micasad.pid
		fi

		# Remember status and be verbose
		rc_status -v
		;;
    try-restart|condrestart)
    	    	$0 status
        	if test $? = 0
		then
                	$0 restart
        	else
                	rc_reset        # Not running is not a failure.
        	fi
        	# Remember status and be quiet
        	rc_status
        	;;
    restart)
        	## Stop the service and regardless of whether it was
        	## running or not, start it again.
		echo "Restarting miCASA daemon"
        	$0 stop
        	$0 start

	        # Remember status and be quiet
        	rc_status
        	;;
    reload)
        	## Stop the service and regardless of whether it was
        	## running or not, start it again.
		echo "Restarting miCASA daemon"
        	$0 stop
        	$0 start

	        # Remember status and be quiet
        	rc_status
        	;;
    force-reload)
        	## Stop the service and regardless of whether it was
        	## running or not, start it again.
		echo "Restarting miCASA daemon"
        	$0 stop
        	$0 start

	        # Remember status and be quiet
        	rc_status
        	;;
    status)
		echo -n "Checking miCASA daemon"
		if pid_exists && ! process_running 2> /dev/null; then \
		    rc_failed 1
		elif ! pid_exists && ! process_running 2> /dev/null; then \
		    rc_failed 3
		fi

		rc_status -v
		;;
    *)
		echo "Usage: $0 {start|stop|restart|try-restart|reload|force-reload|status}"
	exit 1
	;;
esac
rc_exit
