#! /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/server/monitor/filemonit.txt
		touch /etc/safedog/server/monitor/processmonit.txt
		touch /etc/safedog/server/monitor/cpumonit.txt
		touch /etc/safedog/server/monitor/memorymonit.txt
		touch /etc/safedog/server/monitor/diskvolumemonit.txt
		touch /etc/safedog/server/monitor/bakforsizemonit.txt

		sdmonitor 
		sleep 3
		sdmonitor -r sdsvrd >/dev/null 2>&1
		
        ;;
  xstop)
		sdmonitor -k sdsvrd >/dev/null 2>&1
		echo -n -e "stop sdsvrd server "
		for i in {1..15}
		do
			sleep 2
			printf  "#"
			tmppro="x`netstat -andp| grep -c '/sdsvrd\s*/dev/sdsock'`"
			if [ "$tmppro" = "x0" ] ; then
				echo -e "\nsafedog serivce stopped!"
				break
			else
				if [ "$i" == "15" ] ; then
					echo -e "\nstop safedog service fail!"
				fi
			fi
		done
		;;
  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
