#!/bin/sh 
#
# $Id: ndt 206 2008-09-09 13:05:01Z throck $
#
# chkconfig: 2345 55 25
# description: Starts the NDT Web server
# original by Peter Bertoncini <pjb@anl.gov>
# improvements by Tom Throckmorton <throck@mcnc.org>
# 
# added extra logic to handle cases where fakewww isn't used

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

# get local NDT configuration
if [ -f /etc/sysconfig/ndt ];then
        . /etc/sysconfig/ndt
fi

# defaults, if not specified in local config

[ "$WEB100_DATADIR" = "" ] && WEB100_DATADIR="/var/lib/ndt"
[ "$WEB100SRV_OPTIONS" = "" ] && WEB100SRV_OPTIONS="--ipv4 -s --tcpdump --snaplog -l /var/log/web100srv.log"
[ "$FAKEWWW_OPTIONS" = "" ] && = FAKEWWW_OPTIONS="-f /index.html"

path=/usr/sbin

[ -f $path/web100srv ] || exit 0
[ -f $path/fakewww ] || exit 0

RETVAL=0

start_web100srv ()
{
   cnt=`ps auxw | grep web100srv | grep -v grep | wc -l`
   if [ $cnt = 0 ]
   then
      echo -n "Starting web100srv:"
      cd $WEB100_DATADIR
      $path/web100srv $WEB100SRV_OPTIONS 2>/dev/null &
      RETVAL=$?
      if [ $RETVAL = 0 ]
      then 
	success
        touch /var/lock/subsys/web100srv
      else
	failure
      fi
      echo
   fi
}

start_fakewww ()
{
if [ ! $USE_FAKEWWW = 0 ]
then
   cnt=`ps auxw | grep fakewww | grep -v grep | wc -l`
   if [ $cnt = 0 ]
   then
      echo -n "Starting fakewww:"
      $path/fakewww $FAKEWWW_OPTIONS 2>/dev/null &
      RETVAL=$?
      if [ $RETVAL = 0 ]
      then 
	success
        touch /var/lock/subsys/fakewww
      else
	failure
      fi
      echo
   fi
else
    warning
    echo "fakewww disabled - see /etc/sysconfig/ndt"
fi
}

stop_web100srv ()
{
   echo -n "Stopping web100srv:"
   killproc web100srv -TERM
   RETVAL=$?
   echo
   [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/web100srv
}

stop_fakewww ()
{
    if [ ! $USE_FAKEWWW = 0 ]
    then
        echo -n "Stopping fakewww:"
        killproc fakewww -TERM
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/fakewww
    else
        status fakewww 2>/dev/null
        RETVAL=$?
        if [ $RETVAL -eq 0 ] 
        then
            warning
            echo "fakewww disabled, but is running"
        else
            success
            echo "fakewww disabled, not running"
         fi
    fi
}


rhstatus() {
	status web100srv
	status fakewww
}

stop() {
	stop_web100srv
	stop_fakewww
}

start () {
	start_web100srv
	start_fakewww
}

restart() {
	stop
	start
}

case "$1" in
start)
   start
   ;;
stop)
   stop
   ;;
status)
   rhstatus
   ;;
restart|reload)
   restart
   ;;
*)
   echo $"Usage: $0 {start|stop|status|restart}"
   exit 1
esac

exit $?
