#!/bin/sh

#
# Start watchdog
#

NAME="watchdog trigger for development"
PID_FILE=/var/run/watchdog.pid
ENABLED_FILE=/opt/userdata/.wd-enabled
EXE="/sbin/watchdog"

case "$1" in
	start)
		printf "Starting %s ... " "${NAME}"
		if [ -f ${ENABLED_FILE} ]
		then
			if start-stop-daemon --start --quiet --exec ${EXE} --make-pidfile --pidfile ${PID_FILE} -- -t 15 /dev/watchdog
			then
				printf "pid %s.\\n" "$(cat ${PID_FILE})"
			else
				printf "failed.\\n"
			fi
		else
			printf "disabled.\\n"
		fi
	;;
	stop)
		printf "Stopping %s ... " "${NAME}"
		if [ -f ${ENABLED_FILE} ]
		then
			if start-stop-daemon --stop --quiet --pidfile ${PID_FILE}
			then
				printf "done.\\n"
			else
				printf "failed.\\n"
			fi
		else
			printf "disabled.\\n"
		fi
	;;
	restart|reload)
		printf "Restarting %s is not implemented.\\n" "${NAME}"
	;;
	start-once)
		printf "Starting %s once ... " "${NAME}"
		if start-stop-daemon --start --quiet --exec ${EXE} --make-pidfile --pidfile ${PID_FILE} -- -t 15 /dev/watchdog
		then
			printf "pid %s.\\n" "$(cat ${PID_FILE})"
		else
			printf "failed.\\n"
		fi
	;;
	disable)
		printf "Disabling %s ... " "${NAME}"
		rm -f ${ENABLED_FILE}
		if [ -f ${ENABLED_FILE} ]
		then
			printf "failed.\\n"
		else
			printf "done.\\n"
		fi
	;;
	enable)
		printf "Enabling %s ... " "${NAME}"
		touch ${ENABLED_FILE}
		if [ -f ${ENABLED_FILE} ]
		then
			printf "done.\\n"
		else
			printf "failed.\\n"
		fi
	;;
	*)
		printf" Usage: %s {start|stop|restart|start-once|enable|disable}\\n" "$0"
		exit 1
esac

exit $?
