#! /bin/sh

### BEGIN INIT INFO
# Provides:             sdsvrd
# Required-Start:       
# Required-Stop:        
# Default-Start:        2 3 4 5
# Default-Stop:         
# Short-Description:    safedog server
### END INIT INFO

test -x `which sdsvrd` || exit 0
test -x `which sdstart` || exit 0
test -x `which sdcmd` || exit 0
test -x `which killall` || exit 0
test -x `which netstat` || exit 0
test -x `which grep` || exit 0
umask 022
case "x$1" in
  xstart|xrestart)
		touch /etc/safedog/monitor/filemonit.txt
		touch /etc/safedog/monitor/processmonit.txt
		touch /etc/safedog/monitor/cpumonit.txt
		touch /etc/safedog/monitor/memorymonit.txt
		touch /etc/safedog/monitor/diskvolumemonit.txt
		touch /etc/safedog/monitor/bakforsizemonit.txt
		killall sdsvrd > /dev/null 2>&1
		sdstart
        ;;
  xstop)
		killall sdsvrd > /dev/null 2>&1
		tmppro="x`netstat -andp| grep -c '/sdsvrd\s*/dev/sdsock'`"
		if [ "$tmppro" = "x0" ] ; then
			echo "safedog serivce stopped!"
		else
			echo "stop safedog service fail!"
		fi
        ;;
  xstatus)
		tmppro="x`netstat -andp| grep -c '/sdsvrd\s*/dev/sdsock'`"
		if [ "$tmppro" = "x1" ] ; then
			echo "safedog service is running"
		elif [ "$tmppro" = "x0" ] ; then
			echo "safedog serivce is not running"
		else
			echo "error! more than one sdsvrd running!"
		fi
        ;;
  *)
		echo "safedog: not support command \"$1\""
		echo "usage: service safedog {start|restart|stop|status}"
        exit 1
esac

exit 0
