#!/bin/bash
#
# chkconfig: 345 95 05
# description: wwproxy is an aggregating proxy service for the Warewulf
#              daemon.
# processname: wwproxy
# pidfile: /var/run/wwproxy.pid

if [ ! -f "/etc/sysconfig/wwproxy" ]; then
   exit
fi

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

RETVAL=0

if [ -z $WAREWULFD_HOSTS ]; then
   gprintf "You need to define some Warewulfd hosts in /etc/sysconfig/wwproxy!\n"
   exit
else
   HOSTS="--hosts=$WAREWULFD_HOSTS"
fi

if [ -z $WAREWULFD_PORT ]; then
   WPORT="--wport=none"
else
   WPORT="--wport=$WAREWULFD_PORT"
fi

if [ -z $GANGLIA_PORT ]; then
   GPORT="--gport=none"
else
   GPORT="--gport=$GANGLIA_PORT"
fi

if [ -z $WAREWULFD_REFRESH ]; then
   REFRESH=
else
   REFRESH="--refresh=$WAREWULFD_REFRESH"
fi

if [ $LEGACY_SUPPORT -gt 0 ]; then
   LEGACY="--legacy"
fi

start() {
   if [ -x /usr/sbin/wwproxy ]; then
      gprintf "Starting wwproxy: "
      if [ -f /var/run/wproxy.pid ]; then
         status wwproxy
         exit 1
      else
         daemon /usr/sbin/wwproxy $LEGACY $HOSTS $WPORT $GPORT $REFRESH </dev/null
         RETVAL=$?
         echo
      fi
   fi
}

stop() {
   if [ -f /var/run/wwproxy.pid ]; then
      gprintf "Shutting down wwproxy: "
      killproc wwproxy
      RETVAL=$?
      echo
   fi
}

stats() {
   if [ -x /usr/sbin/wwproxy ]; then
      status wwproxy
      RETVAL=0
   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
