#!/bin/sh
# Inicia/para o daemon hlbr.
# Start/stop the hlbr daemon.
# C 2005-2010 Joao Eriberto Mota Filho

### BEGIN INIT INFO
# Provides:             hlbr
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Start HLBR at boot time
# Description:          Used to start/stop/restart HLBR daemon.
#                       HLBR is an IPS.
### END INIT INFO

# Presence check
if [ -x "/usr/sbin/hlbr" ]
then
    echo
else
    echo "ERRO: Nao instalado / ERROR: Not installed"
    exit 1
fi

# Variables
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
CONF="/etc/hlbr/hlbr.config"
RULES="/etc/hlbr/hlbr.rules"
#RULES="/etc/hlbr/empty.rules"
OPTS="-c $CONF -r $RULES"
PIDFILE=$(cat $CONF|grep ^PidFile|cut -d"=" -f2)

# Actions
case "$1" in
start)	if [ -f $PIDFILE ]; then \
	    echo "HLBR:"; \
	    echo; \
	    echo "ERRO: existe um PID ativo / ERROR: already exist an active PID"; \
	    echo "Tente parar o daemon ou remover o arquivo PID \ Try to stop the daemon or remove the PID file"; \
	    echo "$PIDFILE"; \
	    echo; \
	    exit 0; \
	fi
	echo "Inicializando o HLBR / Starting HLBR"
    	cd /etc/hlbr
	hlbr $OPTS &
	sleep 1
	echo "OK"
	;;
stop)	echo "Parando o HLBR / Stopping HLBR"
        if [ -f /sbin/start-stop-daemon ]
        then
    	    start-stop-daemon --stop --quiet --pidfile $PIDFILE --oknodo --name hlbr
    	else
    	    killall hlbr
    	fi
        ;;
restart|force-reload) echo "Reinicializando o HLBR / Restarting HLBR"
	"$0" stop && sleep 3 && "$0" start
        ;;
*)	echo "Uso / Usage: /etc/init.d/hlbr start|stop|restart|force-reload"
        exit 1 
        ;;
esac
exit 0
