#!/sbin/sh

# Squid Internet Object Cache startup
# AUTHOR: Markus Gyger

# HP-UX 10:      this is file /sbin/init.d/squid
#                (ln -s ../init.d/squid /sbin/rc2.d/S900squid)
#                (ln -s ../init.d/squid /sbin/rc1.d/K100squid)
#                edit file /etc/rc.config.d/squid to enable Squid

# SUN Solaris 2: this is file /etc/init.d/squid
#                (ln /etc/init.d/squid /etc/rc2.d/S90squid)
#                (ln /etc/init.d/squid /etc/rc1.d/K10squid)
#                (ln /etc/init.d/squid /etc/rc0.d/K10squid)

# SGI IRIX:      this is file /etc/init.d/squid
#                (ln -s ../init.d/squid /etc/rc2.d/S99squid)
#                (ln -s ../init.d/squid /etc/rc0.d/K01squid)
#                edit file /var/config/squid.options to set options
#                use "chkconfig -f squid on" to enable Squid

# Digital UNIX:  this is file /sbin/init.d/squid
#                (ln -s ../init.d/squid /sbin/rc?.d/S??squid)
#                (ln -s ../init.d/squid /sbin/rc?.d/K??squid)
#                use "rcmgr set SQUID 1" to enable Squid

# some? Linux:   this is file /sbin/init.d/squid
#                (ln -s ../squid /sbin/init.d/rc3.d/S90squid)
#                (ln -s ../squid /sbin/init.d/rc3.d/K10squid)
#                add SQUID=1 to file /etc/rc.config to enable Squid


PATH=/opt/squid/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH


config()
{
    # SGI IRIX
    if [ -f /sbin/chkconfig ]
    then if /sbin/chkconfig squid
         then SQUID=1
              if [ -f /var/config/squid.options ]
              then . /var/config/squid.options
              fi
         else SQUID=0
         fi

    # Digital UNIX
    elif [ -f /usr/sbin/rcmgr ]
    then SQUID=`/usr/sbin/rcmgr get SQUID 0`
         SQUID_OPTIONS=`/usr/sbin/rcmgr get SQUID_OPTIONS "-s"`
         SQUID_RESPAWN=`/usr/sbin/rcmgr get SQUID_RESPAWN 1`

    # HP-UX 10 / Linux
    elif [ -f /etc/rc.config ]
    then . /etc/rc.config

    # SUN Solaris 2
    else SQUID=1
         SQUID_OPTIONS="-s"
         SQUID_RESPAWN=1
    fi

    [ 1 = "${SQUID-}" ]
}


respawn()
{
    trap "" 1
    fails=0
    while [ $fails -le 5 ]
    do  start=`date +%d%H%M%S`
        if "$@"
        then logger -t "$1" -p local4.notice \
                 "respawn[$$]: Exiting due to shutdown"
             return 0
        fi
        stop=`date +%d%H%M%S`
        time=`expr $stop - $start`
        [ "$time" -gt 10 ] && fails=0
        fails=`expr $fails + 1`
    done

    logger -t "$1" -p local4.alert \
        "respawn[$$]: Exiting due to repeated, frequent failures"
    return 1
}


case $* in
start_msg)
    echo "Start Squid Internet Object Cache"
    ;;

stop_msg)
    echo "Stopping Squid Internet Object Cache"
    ;;

start)
    config || exit 2  # Squid not enabled

    if whence=`type squid 2>&1`
    then trap "" 1
         if [ 0 = "${SQUID_RESPAWN-}" ]
         then         squid ${SQUID_OPTIONS-} &
         else respawn squid ${SQUID_OPTIONS-} &
         fi

    else echo "ERROR: $whence" >&2
         exit 1
    fi
    ;;

stop)
    config || exit 2  # Squid not enabled

    squid ${SQUID_OPTIONS-} -k shutdown || exit 1
    ;;

reconf*|rotate|int*|debug|check|kill)
    config

    squid ${SQUID_OPTIONS-} -k "$1"
    ;;

*)
    echo "usage: $0 {start|stop|reconfigure|rotate|interrupt|debug|check|kill}" >&2
    echo "    start        start squid" >&2
    echo "    stop         clean shutdown" >&2
    echo "    reconfigure  reread configuration files" >&2
    echo "    rotate       rotate log files" >&2
    echo "    interrupt    quick clean shutdown " >&2
    echo "    debug        toggle debug logging" >&2
    echo "    check        check for running squid" >&2
    echo "    kill         terminate squid by brute force" >&2

    exit 1
    ;;
esac

[ $? -eq 0 ]
exit
