#!/bin/bash
#
# Init file for OWAMP daemon
#
# chkconfig: 2345 60 20
# description: OWAMP daemon
#
# processname: owampd 
#


OWAMPDBINDIR=/usr/bin
CONFDIR=/etc/owamp-server
OWAMPDVARDIR=/var/run
PIDFILE=${OWAMPDVARDIR}/owamp-server.pid

OWAMPD="${OWAMPDBINDIR}/owampd -c ${CONFDIR} -R ${OWAMPDVARDIR}"

ulimit -n 4096

ERROR=0
ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

for ARG in $@ $ARGS
do
    # check for pidfile
    if [ -f $PIDFILE ] ; then
	PID=`cat $PIDFILE`
	if [ "x$PID" != "x" ] && kill -0 $PID 2>/dev/null ; then
	    STATUS="owamp-server (pid $PID) running"
	    RUNNING=1
	else
	    STATUS="owamp-server (pid $PID?) not running"
	    RUNNING=0
	fi
    else
	STATUS="owamp-server (no pid file) not running"
	RUNNING=0
    fi

    case $ARG in
    start)
	if [ $RUNNING -eq 1 ]; then
	    echo "$0 $ARG: owamp-server (pid $PID) already running"
	    continue
	fi

	echo $OWAMPD

	if $OWAMPD ; then
	    echo "$0 $ARG: owamp-server started"
	else
	    echo "$0 $ARG: owamp-server could not be started"
	    ERROR=3
	fi
	;;
    stop)
	if [ $RUNNING -eq 0 ]; then
	    echo "$0 $ARG: $STATUS"
	    continue
	fi
	if kill $PID ; then
	    echo "$0 $ARG: owamp-server stopped"
	else
	    echo "$0 $ARG: owamp-server could not be stopped"
	    ERROR=4
	fi
	;;
    status)
	echo $STATUS
	if [ $RUNNING -eq 0 ]; then
	    ERROR=3
	fi
	;;
    cond-restart)
        if [ $RUNNING -eq 1 ]; then
    	    $0 stop; echo "waiting..."; sleep 10; $0 start;
        fi
	;;
    restart)
		$0 stop; echo "waiting..."; sleep 10; $0 start;
	;;
#	if [ $RUNNING -eq 0 ]; then
#	    echo "$0 $ARG: owampd not running, trying to start"
#	    if $OWAMPD ; then
#		echo "$0 $ARG: owampd started"
#	    else
#		echo "$0 $ARG: owampd could not be started"
#		ERROR=5
#	    fi
#	else
#	    if kill -HUP $PID ; then
#	       echo "$0 $ARG: owampd restarted"
#	    else
#	       echo "$0 $ARG: owampd could not be restarted"
#	       ERROR=6
#	    fi
#	fi
#	;;
    *)
	echo "usage: $0 (start|stop|restart|help)"
	cat <<EOF

start      - start owamp-server
stop       - stop owamp-server
restart    - restart owamp-server if running by sending a SIGHUP or start if 
             not running
status     - report if owamp-server is running
help       - this screen

EOF
	ERROR=2
    ;;

    esac

done

exit $ERROR
