#! /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/guardian/guardian.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
DGDIRS="/etc/guardian/guardian*"
NAME=eole-guardian
DAEMON=/usr/sbin/e2guardian
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_guardians() {
    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"
        touch /var/run/${INSTANCE_NAME}.pid
        ulimit -n 8192
        start-stop-daemon --start --quiet --pidfile /var/run/${INSTANCE_NAME}.pid --exec $DAEMON -- -c $DG/guardian.conf
        RETVAL="$?"
        if [ ! "$RETVAL" = 0 ]; then
            log_end_msg 1
            exit $EXIT_NOK_CODE
        fi
    done
    log_end_msg 0
}

stop_guardians() {
    log_daemon_msg "Stopping $DESC"
    KDAEMON_NAME=`basename $DAEMON`
    proc_list=$(pgrep -x "$KDAEMON_NAME")
    RET=$?
    if [ $RET -eq 0 ]; then
      for DG in $DGDIRS
      do
        INSTANCE_NAME=`basename $DG`
        do_stop $DAEMON $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 $DAEMON
      RETVAL="$?"
      if [ "$RETVAL" = 2 ]; then
        log_end_msg 1
        exit $EXIT_NOK_CODE
      fi
    fi
    log_end_msg 0
}

reload_guardians() {
    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/guardian.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_guardians
        ;;
    stop)
        stop_guardians
        ;;
    reload)
        reload_guardians
          ;;
    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_guardians
        start_guardians
        ;;
    status)
        status_dansguardians
        ;;
    *)
    N=/etc/init.d/$NAME
    log_action_msg "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
