#!/bin/sh
#
# chkconfig: - 99 01
# description: bmc-watchdog startup script
#
### BEGIN INIT INFO
# Provides: bmc-watchdog
# Required-Start: $network $remote_fs $syslog
# Required-Stop:  $network $remote_fs $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Start and stop bmc-watchdog
# Description: BMC watchdog timer daemon
### END INIT INFO

DAEMON=/opt/local/sbin/bmc-watchdog
PIDFILE=/opt/local/var/run/bmc-watchdog.pid
LOCKFILE=/opt/local/var/lock/subsys/bmc-watchdog
CONFFILE=/opt/local/etc/sysconfig/bmc-watchdog

[ -f $DAEMON ] || exit 5

if [ -r $CONFFILE ]; then
        . $CONFFILE
fi

# Load Redhat or Suse appropriate libs
if [ -f /opt/local/etc/rc.d/init.d/functions ] ; then
    . /opt/local/etc/rc.d/init.d/functions
    Xstart() {

        # Default daemon mode
        #
        # timer use = SMS/OS
        # pre-timeout interrupt = none
        # action = reset
        # -F, -P, -L, -S, -O - clear flags
        # initial-countdown = 900 seconds (15 mins)
        # reset-period = 60 seconds

        daemon -20 $DAEMON $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $LOCKFILE
    }
    Xstop() {
        killproc bmc-watchdog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
    }
    Xstatus() {
        status bmc-watchdog
        RETVAL=$?
        return $RETVAL
    }
    Xcondrestart() {
        if test -e $LOCKFILE; then
            $0 stop
            $0 start
            RETVAL=$?
        fi
    }
    Xexit() {
        exit $RETVAL
    }
elif [ -f /opt/local/etc/rc.status ] ; then
    . /opt/local/etc/rc.status
    Xstart() {
        startproc -20 $DAEMON $OPTIONS
        rc_status -v
        RETVAL=$?
        return $RETVAL
    }
    Xstop() {
        killproc -TERM $DAEMON
        rc_status -v
        RETVAL=$?
        return $RETVAL
    }
    Xstatus() {
        echo -n "Checking for bmc-watchdog: "
        checkproc $DAEMON
        rc_status -v
        RETVAL=$?
        return $RETVAL
    }
    Xcondrestart() {
        $0 status
        if test $? = 0; then
            $0 restart
        else
            rc_reset
        fi
        rc_status
        RETVAL=$?
        return $RETVAL
    }
    Xexit() {
        rc_exit
    }
else
    echo "Unknown distribution type"
    exit 1
fi

RETVAL=0

case "$1" in
   start)
        echo -n "Starting bmc-watchdog: "
        Xstatus >/dev/null 2>&1 && exit 0
        Xstart
        ;;

  stop)
        echo -n "Shutting down bmc-watchdog: "
        Xstop
        ;;

  restart)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
  status)
        Xstatus
        ;;
  condrestart|try-restart|force-reload)
        Xcondrestart
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status|condrestart|try-restart|force-reload}"
        exit 2
esac

exit $RETVAL
