#! /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)

EXIT_NOK_CODE=7

#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" = 2 ] && return 2
    rm -f $3
    return "$RETVAL"
}

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

stop_dansguardians() {
    log_daemon_msg "Stopping $DESC"
    for KDAEMON in $E2_DAEMON $DANS_DAEMON
    do
        KDAEMON_NAME=`basename $KDAEMON`
        proc_list=$(pgrep -x "$KDAEMON_NAME")
        RET=$?
        if [ $RET -eq 0 ]; then
            for DG in $DGDIRS
            do
                INSTANCE_NAME=`basename $DG`
                do_stop $KDAEMON $DESC /var/run/${INSTANCE_NAME}.pid
                RETVAL="$?"
                if [ "$RETVAL" = 2 ]; then
                    log_end_msg 1
                    exit $EXIT_NOK_CODE
                fi
            done
            # 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 $KDAEMON
            RETVAL="$?"
            if [ "$RETVAL" = 2 ]; then
                log_end_msg 1
                exit $EXIT_NOK_CODE
            fi
        fi
    done
    log_end_msg 0
}

reload_dansguardians() {
    log_action_begin_msg "Reloading $DESC configuration..."
    for DG in $DGDIRS
    do
        INSTANCE_NAME=`basename $DG`
        log_daemon_msg "Instance #$INSTANCE_NAME"
        $DAEMON -c $DG/dansguardian.conf -g
        RETVAL="$?"
        if [ ! "$RETVAL" = 0 ]; then
            log_end_msg 1
            exit $EXIT_NOK_CODE
        fi
    done
    log_action_end_msg 0
}

status_dansguardians() {
    exit_ret=0
    for DG in $DGDIRS
    do
        INSTANCE_NAME=`basename $DG`
        status_of_proc -p /var/run/${INSTANCE_NAME}.pid $DAEMON $DESC
        ret=$?
        [ $ret != 0 ] && exit_ret=$ret
    done
    exit $exit_ret
}

case "$1" in
    start)
        start_dansguardians
        ;;
    stop)
        stop_dansguardians
        ;;
    reload)
        reload_dansguardians
          ;;
    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".
        #
        stop_dansguardians
        start_dansguardians
        ;;
    status)
        status_dansguardians
        ;;
    *)
    N=/etc/init.d/$NAME
    log_action_msg "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
