#! /bin/sh
# Startup script for dansguardian
#
# description: A web content filtering plugin for web \
#              proxies, developed to filter using lists of \
#              banned phrases, MIME types, filename \
#              extensions and PICS labling.
# processname: dansguardian
# pidfile: /var/run/dansguardian.pid
# config: /etc/dansguardian/dansguardian.conf
### BEGIN INIT INFO
# Provides:          dansguardian
# Required-Start:    $network $remote_fs $syslog $squid
# Required-Stop:     $network $remote_fs $squid
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description: Starts dansguardian content proxy
# short-description: dansguardian configuration
### END INIT INFO

#include lsb functions
. /lib/lsb/init-functions

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
E2_DAEMON=/usr/sbin/e2guardian
DANS_DAEMON=/usr/sbin/dansguardian
DGDIRS="/etc/dansguardian/dansguardian*"
NAME=dansguardian
DAEMON_PATH=/etc/eole/guardian.conf
[ -e $DAEMON_PATH ] && DAEMON=$(cat $DAEMON_PATH) || DAEMON="xxxguardian"
DESC=$(basename $DAEMON)

set -e

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

#
# Function that stops the daemon/service
#
do_stop()
{
    # $1 : DAEMON binary path
    # $2 : DAEMON name
    # $3 : DAEMON PID file
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $3 --name $2 && RETVAL="$?" || RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $1 && RETVAL2="$?" || RETVAL2="$?"
    [ "$RETVAL2" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $3
    return "$RETVAL"
}

start_dansguardians() {
    if [ ! -f $DAEMON_PATH ]; then
        echo "Impossible de déterminer le chemin du démon"
        exit 1
    fi
    for DG in $DGDIRS
    do
        INSTANCE_NAME=`basename $DG`
        log_daemon_msg "Instance #$INSTANCE_NAME"
        start-stop-daemon -b --start --quiet --pidfile /var/run/${INSTANCE_NAME}.pid \
            --exec $DAEMON -- -c $DG/dansguardian.conf > /dev/null 2>&1 &
    done
}

stop_dansguardians() {
    for KDAEMON in $E2_DAEMON $DANS_DAEMON
    do
        proc_list=$(pgrep -f "$KDAEMON") && RET=$? || RET=$?
        if [ $RET -eq 0 ]; then
            for DG in $DGDIRS
            do
                INSTANCE_NAME=`basename $DG`
                do_stop $KDAEMON $DESC /var/run/${INSTANCE_NAME}.pid || log_end_msg 1
            done
        fi
    done
}

reload_dansguardians() {
    for DG in $DGDIRS
    do
        INSTANCE_NAME=`basename $DG`
        log_daemon_msg "Instance #$INSTANCE_NAME"
        $DAEMON -c $DG/dansguardian.conf -g > /dev/null 2>&1 &
    done
}

status_dansguardians() {
    for DG in $DGDIRS
    do
        INSTANCE_NAME=`basename $DG`
        status_of_proc -p /var/run/${INSTANCE_NAME}.pid $DAEMON $DESC
    done
}

case "$1" in
    start)
        log_daemon_msg "Starting $DESC"
        start_dansguardians
        log_end_msg 0
        ;;
    stop)
        log_daemon_msg "Stopping $DESC"
        stop_dansguardians
        log_end_msg 0
        ;;
    reload)
        log_action_begin_msg "Reloading $DESC configuration..."
        reload_dansguardians
        log_action_end_msg 0
          ;;
    restart|force-reload)
        #
        #    If the "reload" option is implemented, move the "force-reload"
        #    option to the "reload" entry above. If not, "force-reload" is
        #    just the same as "restart".
        #
        log_daemon_msg "Restarting $DESC: "
        stop_dansguardians
        start_dansguardians
        log_end_msg 0
        ;;
    status)
        status_dansguardians
        ;;
    *)
    N=/etc/init.d/$NAME
    log_action_msg "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
