#! /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

case "$1" in
    start)
		echo "Starting miCASA daemon"
		/sbin/startproc -f  $MICASAD_BIN
		# Remember status and be verbose
		rc_status -v
		;;
    stop)
		echo "Shutting down micasad " 
		#killproc -TERM $MICASAD_BIN
        	pid=`cat /var/run/micasad.pid` >/dev/null 2>&1
        	kill -s TERM $pid >/dev/null 2>&1

		# 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 "Checking micasad service ..."
		/sbin/checkproc micasad
		res=$?
		if [ $res == 0 ]
		then
			echo -n "0 - service up and running"
		elif [ $res == 1 ]
		then
			echo -n "1 - service running under mono, /var/run/pid  file exists"
		elif [ $res == 2 ]
		then
			echo -n "2 - service status unknown, /var/lock/lock file exists"
		elif [ $res == 3 ]
		then
			echo -n "3 - service not running"
		else
			echo -n "service status unknown"
		fi

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