#! /bin/sh
# Copyright (c) 2011 Author: Carsten Schoene <cs@linux-administrator.com>
#
# /etc/init.d/packetbl
#
#   and symbolic its link
#
# /usr/sbin/rcpacketbl
#
# System startup script for the packetbl daemon
#
### BEGIN INIT INFO
# Provides: packetbl
# Required-Start: $remote_fs
# Required-Stop:  $null
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Start packetbl
# Description: starts the packetbl daemon. 
### END INIT INFO

PACKETBL_BIN=/usr/bin/packetbl
PACKETBL_PID=/var/run/packetbl/packetbl.pid
test -x $PACKETBL_BIN || { echo "$PACKETBL_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; }

. /etc/rc.d/init.d/functions


# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#
# 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 packetbl"
	daemon --pidfile $PACKETBL_PID $PACKETBL_BIN $PACKETBL_OPTIONS $OPTIONS
	echo
	;;
    stop)
	echo -n "Shutting down packetbl"
	killproc -p $PACKETBL_PID -TERM $PACKETBL_BIN
	echo
	;;
    try-restart|condrestart)
        ## Do a restart only if the service was active before.
        ## Note: try-restart is now part of LSB (as of 1.9).
        ## RH has a similar command named condrestart.
        if test "$1" = "condrestart"; then
                echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
	$0 status
        if test $? = 0; then
                $0 restart
        else
                failed        # Not running is not a failure.
        fi
	;;
    restart|force-reload|reload)
	$0 stop
	sleep 1
	$0 start
	;;
    status)
	echo -n "Checking for packetbl: "
	status -p $PACKETBL_PID $PACKETBL_BIN
	;;
    probe)
	if [ /etc/packetbl.conf -nt $PACKETBL_PID ]; then
	  echo restart
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
