#!/bin/sh
# start/stop/restart ipmitail daemon:
PROG="ipmitail"
DAEMON="/usr/sbin/$PROG"
LOCKFILE="/var/lock/$PROG"
PIDFILE="/var/run/$PROG.pid"
CONFIG="/boot/config/plugins/ipmitool-plugin/ipmitool-plugin.cfg"
OPTIONS=""

# read our configuration
[ -e "$CONFIG" ] && source "$CONFIG"

# Start ipmitail:
ipmitail_start() {
		# no-op if already running
if [[ ! -r "$PIDFILE" && "$IPMIEVD" == "enable" ]]; then
	echo "starting $PROG..."
  	sleep 1

	nohup $DAEMON >/dev/null 2>&1 | echo $! > $PIDFILE &
	touch $LOCKFILE
  	TIMER=0
	while [ ! -e $PIDFILE ]; do
		sleep 1
  		let TIMER=$TIMER+1
  		if [ $TIMER -gt 5 ]; then
  			echo -n "$PIDFILE not created"
  	   	break
		fi
	done
else
	echo "$PROG is running   "
fi
}

# Stop ipmitail:
ipmitail_stop() {
# no-op if not running
if [ -r $PIDFILE ]; then
	#stop ipmitail
	echo "stopping $PROG..."

	TIMER=0
	while `killall $PROG 2>/dev/null`; do
		sleep 1
		TIMER=$((TIMER+1))
		if [ $TIMER -ge 30 ]; then
			killall -9 $PROG
			sleep 1
			break
		fi
	done
	rm -f $LOCKFILE && rm -f $PIDFILE
else
	echo "$PROG is stopped   "
fi
}

# Restart ipmitail:
ipmitail_restart() {
	ipmitail_stop
	sleep 1
	ipmitail_start
}

case "$1" in
'start')
  ipmitail_start
  ;;
'stop')
  ipmitail_stop
  ;;
'restart')
  ipmitail_restart
  ;;
*)
  echo "usage rc.ipmitail: start|stop|restart"
esac