#!/bin/bash
#
# chkconfig: 345 95 05
# description: WAREWULFD watches the status of the slave nodes on the cluster
#              and keeps them configured properly and available to cluster
#              users.
# processname: warewulfd
# pidfile: /var/run/warewulfd.pid

# source function library
. /etc/init.d/functions

RETVAL=0

start() {
   gprintf "Starting Warewulf:\n"
   if [ -x /usr/sbin/warewulfd ]; then
      gprintf "   Warewulfd "
      if [ -f /var/run/warewulfd.pid ]; then
         status warewulfd
      else
         daemon /usr/sbin/warewulfd </dev/null
         RETVAL=$?
         echo
         if [ $RETVAL -eq 0 ]; then
            touch /var/lock/subsys/warewulfd
            echo READY > /.nodestatus
         fi
      fi
   fi
   if [ -x /usr/sbin/wwinit ]; then
      gprintf "   PXE config files "
      /usr/sbin/wwinit --pxe >/dev/null
      success
      echo
   fi
   if [ -x /usr/sbin/wwinit ]; then
      gprintf "   Adding hosts entries "
      /usr/sbin/wwinit --hosts >/dev/null
      success
      echo
   fi
}

stop() {
   if [ -f /var/run/warewulfd.pid ]; then
      gprintf "Shutting down warewulfd: "
      killproc warewulfd
      RETVAL=$?
      echo
   fi
   test -f /var/lock/subsys/warewulfd && 	rm -f /var/lock/subsys/warewulfd
   test -f /.nodestatus && rm -f /.nodestatus
}

stats() {
   if [ -x /usr/sbin/warewulfd ]; then
      status warewulfd
      RETVAL=0
   fi
}

reload() {
   if [ -x /usr/sbin/warewulfd ]; then
      gprintf "Reloading warewulfd: "
      killproc warewulfd -HUP
      retval=$?
      echo
   fi
}

case "$1" in
  start)
   start
   ;;
  stop)
   stop
   ;;
  restart)
   stop
   start
   RETVAL=0
   ;;
#  reload)
 #  reload
 #  ;;
  status)
   stats
   ;;
  *)
   gprintf "Usage: %s {start|stop|restart|status}\n" "$0"
   exit 1
esac

exit $RETVAL
