#!/bin/bash
#
# Init file for Software Watchdog daemon.
#
# Written by Dag Wieers <dag@wieers.com>.
#
# chkconfig: 2345 02 98
# description: Software Watchdog Daemon
#
# processname: watchdog
# config: /etc/watchdog.conf
# pidfile: /var/run/watchdog

source /etc/rc.d/init.d/functions

[ -x /usr/sbin/watchdog ] || exit 1
[ -r /etc/watchdog.conf ] || exit 1

### Default variables
SYSCONFIG="/etc/sysconfig/watchdog"
OPTIONS="-v"

### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"

RETVAL=0
prog="watchdog"
desc="Software Watchdog daemon"

start() {
	echo -n $"Starting $desc ($prog): "

	### For some people it is a module, for others not. We force it because
	### for kernels < 2.1, we need kerneld, and it's not running yet.
	modprobe softdog &>/dev/null
	modprobe pcwd &>/dev/null
	modprobe acquirewdt &>/dev/null

	daemon $prog $OPTIONS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
	return $RETVAL
}

stop() {
	echo -n $"Shutting down $desc ($prog): "
	killproc $prog

	### If you compiled your kernel with CONFIG_WATCHDOG_NOWAYOUT, you may
	### not want  to remove the module  as sometimes /etc/rc.d/init.d/halt
	### will hang on umounting some remote nfs partition or for some other
	### reason, and you may then want the kernel to reboot by itself.
	### However, this means that if you stop watchdog, your system has one
	### minute to reboot cleanly, or it will be rebooted by the kernel. If
	### this behavior  isn't what you  want, just uncomment  the following
	### lines
	#rmmod softdog &>/dev/null
	#rmmod pcwd &>/dev/null
	#rmmod acquirewdt &>/dev/null

	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
	return $RETVAL
}

restart() {
        stop
        start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  condrestart)
	[ -e /var/lock/subsys/$prog ] && restart
	RETVAL=$?
	;;
  status)
	status $prog
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	RETVAL=1
	;;
esac

exit $RETVAL
