#! /bin/sh
#
# /etc/init.d/netsyslogger
#
### BEGIN INIT INFO
# Provides:            netsyslogger
# Required-Start:      $remote_fs
# Required-Stop:       $network $remote_fs
# Default-Start:       2 3 5
# Default-Stop:        0 1 6
# Short-Description:   Starts netsyslogger 
# Description:         Starts the netsyslogger daemon to follow multiple files
### END INIT INFO

BIN_FILE=/usr/sbin/netsyslogger.py 
CFG_FILE=/etc/netsyslogger.cfg
PID_FILE=/var/run/netsyslogger.pid
DISPLAY_NAME=netsyslogger

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

if [ ! -x $BIN_FILE ]; then 
   echo "FATAL: Cannot find binary $BIN_FILE!"
   rc_failed
   rc_exit 
fi 


# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.


case "$1" in
    start)
	echo -n "Starting $DISPLAY_NAME:"
        $BIN_FILE --config $CFG_FILE &
	rc_status -v
        ps ax -o "%U %p %a" | grep $BIN_FILE | grep -v grep | awk '{print $2}' > $PID_FILE
	rc_reset
	;;
    stop)
	echo -n "Shutting down $DISPLAY_NAME:"
        if [ -f $PID_FILE ]; then
            PID=`cat $PID_FILE`
            kill -TERM $PID
        else 
            rc_failed
	    rc_status -v  
            rc_exit
        fi
	rc_status -v  
        rm $PID_FILE
        rc_reset
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    status)
	echo -n "Checking for service $DISPLAY_NAME:"
	checkproc -p $PID_FILE /usr/bin/python
	rc_status -v
	rc_reset
	;;
    *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
	;;
esac
rc_exit

