#!/bin/sh
#
# Startup script for e2guardian
#
# chkconfig: 35 92 8
# description: A web content filtering plugin for web \
#              proxies, developed to filter using lists of \
#              banned phrases, MIME types, filename \
#              extensions and PICS labelling.
# processname: e2guardian
# pidfile: @E2PIDDIR@/e2guardian.pid
# config: @E2CONFDIR@/e2guardian.conf

### BEGIN INIT INFO
# Provides:     e2guardian
# Required-Start:       squid
# Should-Start:
# Required-Stop:        squid
# Should-Stop:
# Default-Start:        3 5
# Default-Stop: 0 1 2 6
# Short-Description:    E2guardian web content filter
# Description:  E2guardian web content filter
### END INIT INFO

# File includes changes by Thomas Jarosch
function wait_for_pid()
{
    local PID=$1
    local RET=0
    
    if [ $PID -eq 0 ] ; then
        return $RET
    fi
    
    # give 60 secs then KILL
    local COUNTDOWN=60

    while [ -d /proc/${PID} ] && [ $COUNTDOWN -gt 0 ] ; do
        sleep 1
        COUNTDOWN=$[$COUNTDOWN-1]
    done

    if [ -d /proc/${PID} ]; then
        COMMAND=`ps h -o command ${PID}`
        logger "e2guardian: timeout waiting for PID ${PID}: ${COMMAND}; sending SIGKILL"
        kill -KILL $PID >/dev/null 2>&1
        RET=1
    fi
    
    return $RET
}

# See how we were called.

case "$1" in
start)
        if [ -f @E2BINDIR@/e2guardian ] &&
           [ -f @E2CONFDIR@/e2guardian.conf ]; then
                echo -n "Starting e2guardian: "
                if @E2BINDIR@/e2guardian 2> /dev/null; then
                        echo -e "\\033[60G\c"
                        echo -e "[ \\033[1;32m\c"
                        echo -e "OK\c"
                        echo -e "\\033[0;39m\c"
                        echo " ]"
                        [ -d /var/lock/subsys ] && touch /var/lock/subsys/e2guardian
                else
                        echo -e "\\033[60G\c"
                        echo -e "[ \\033[1;31m\c"
                        echo -e "FAILED\c"
                        echo -e "\\033[0;39m\c"
                        echo " ]"
                fi
        fi
        ;;
stop)
        echo -n "Shutting down e2guardian: "
        WAITPID=0
        if [ -f @E2PIDDIR@/e2guardian.pid ] ; then
            WAITPID=`cat @E2PIDDIR@/e2guardian.pid`
        fi
        if @E2BINDIR@/e2guardian -q 2> /dev/null; then
                if wait_for_pid $WAITPID ; then
                    echo -e "\\033[60G\c"
                    echo -e "[ \\033[1;32m\c"
                    echo -e "OK\c"
                    echo -e "\\033[0;39m\c"
                    echo " ]"
                else
                    echo -e "\\033[60G\c"
                    echo -e "[ \\033[1;31m\c"
                    echo -e "FAILED\c"
                    echo -e "\\033[0;39m\c"
                    echo " ]"
                fi
                /bin/rm -f @E2PIDDIR@/e2guardian.pid
                /bin/rm -f /tmp/.dguardianipc
                [ -d /var/lock/subsys ] && /bin/rm -f /var/lock/subsys/e2guardian
        else
                echo -e "\\033[60G\c"
                echo -e "[ \\033[1;31m\c"
                echo -e "FAILED\c"
                echo -e "\\033[0;39m\c"
                echo " ]"
        fi
        ;;
restart)
        $0 stop
        $0 start
        ;;
status)
        if [ -f @E2BINDIR@/e2guardian ]; then
                @E2BINDIR@/e2guardian -s
        fi
        ;;
        
*)

        echo "Usage: $0 {start|stop|restart|status}" >&2
        ;;
esac

exit 0
