#!/bin/sh
# ipmidriver init file 
#
# chkconfig: 345 98 02
# description: Server Management System ipmi kcs driver
#

prog=ipmidrv
startdefault=0
CONFIG_FILE=/etc/ipmi_watchdog.config
if [ ! -e $CONFIG_FILE ]; then
    echo -n $"$CONFIG_FILE does not exist,ipmi driver is startting with default WDT setting."
    startdefault=1 
fi

if [ $startdefault -eq 0 ]; then

	TIMERUSE=`cat $CONFIG_FILE 2>/dev/null | grep  TimerUse=`;
	if [ -z "$TIMERUSE" ]; then
	    echo -n $"1Error in $CONFIG_FILE.";
            echo ""
	    startdefault=1
	    exit 1
	fi	

	TIMERACTIONS=`cat $CONFIG_FILE 2>/dev/null | grep  TimerActions=`
	if [ -z "$TIMERACTIONS" ]; then
	    echo -n $"2Error in $CONFIG_FILE.";
            echo ""
	    startdefault=1
	    exit 1
	fi

	PRETIMEOUTINTERVAL=`cat $CONFIG_FILE 2>/dev/null | grep  PreTimeoutInterval=`
	if [ -z "$PRETIMEOUTINTERVAL" ]; then
	    echo -n $"3Error in $CONFIG_FILE.";
            echo ""
	    startdefault=1	
	    exit 1
	fi

	TIMERUSEEXPIRATIONFLAGSCLEAR=`cat $CONFIG_FILE 2>/dev/null | grep  TimerUseExpirationFlagsClear=`
	if [ -z "$TIMERUSEEXPIRATIONFLAGSCLEAR" ]; then
	    echo -n $"4Error in $CONFIG_FILE.";
            echo ""
	    startdefault=1
	    exit 1
	fi
	
	INITIALCOUNTDOWN=`cat $CONFIG_FILE 2>/dev/null | grep  InitialCountDown=`
	if [ -z "$INITIALCOUNTDOWN" ]; then
	    echo -n $"5Error in $CONFIG_FILE.";
            echo ""
	    startdefault=1
	    exit 1
	fi
	
fi

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.

module=/lib/modules/`uname -r`/kernel/drivers/misc/ipmi.o
RETVAL=0
start() {
	if [ ! -e $module ]; then
	    echo -n $"$module do not exist!";
            echo ""
	    exit 1 
	fi
	if [ $startdefault -eq 1 ]; then
            echo -n $"Starting $prog with default value:";	
	    /sbin/insmod ipmi 
            RETVAL=$?
            [ "$RETVAL" = 0 ] && touch /var/lock/subsys/ipmidrv
	    return 0
	fi
	if [ $startdefault -eq 0 ]; then
            echo -n $"Starting $prog:";
            echo ""
	    /sbin/insmod ipmi $TIMERUSE $TIMERACTIONS $PRETIMEOUTINTERVAL $TIMERUSEEXPIRATIONFLAGSCLEAR $INITIALCOUNTDOWN
	    RETVAL=$?
            [ "$RETVAL" = 0 ] && touch /var/lock/subsys/ipmidrv
	fi
        return 0
}
stop() {
        echo -n $"Stopping $prog:"
        echo ""
	/sbin/rmmod ipmi
        RETVAL=$?
        [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/ipmidrv
	return 0 
}

# See how we were called.
case "$1" in

  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|}"
        echo ""
        exit 1
esac
