#!/bin/bash
# chkconfig: - 90 25
# pidfile: /var/run/squid.pid
# config: /etc/squid/squid.conf
#
### BEGIN INIT INFO
# Provides: squid
# Short-Description: starting and stopping Squid Internet Object Cache
# Description: Squid - Internet Object Cache. Internet object caching is \
#       a way to store requested Internet objects (i.e., data available \
#       via the HTTP, FTP, and gopher protocols) on a system closer to the \
#       requesting site than to the source. Web browsers can then use the \
#       local Squid cache as a proxy HTTP server, reducing access time as \
#       well as bandwidth consumption.
### END INIT INFO


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

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

if [ -f /etc/sysconfig/squid ]; then
    . /etc/sysconfig/squid
fi

# don't raise an error if the config file is incomplete
# set defaults instead:
SQUID_OPTS=${SQUID_OPTS:-""}
SQUID_PIDFILE_TIMEOUT=${SQUID_PIDFILE_TIMEOUT:-20}
SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
SQUID_FORCETERM_TIMEOUT=${SQUID_FORCETERM_TIMEOUT:-20}
SQUID_FORCEKILL_TIMEOUT=${SQUID_FORCEKILL_TIMEOUT:-80}
SQUID_CONF=${SQUID_CONF:-"/etc/squid/squid.conf"}

# determine the name of the squid binary
[ -f /usr/sbin/squid ] && SQUID=squid

prog="$SQUID"

# determine which one is the cache_swap directory
CACHE_SWAP=`sed -e 's/#.*//g' $SQUID_CONF | \
    grep cache_dir | awk '{ print $3 }'`

# determine if rock store is used
grep -qEe "cache_dir.*rock" $SQUID_CONF &>/dev/null
ROCK_STORE=$?

RETVAL=0

getmaxfd(){
    FILE_MAX=`expr $(cat /proc/sys/fs/file-max) / 4`
    NR_OPEN=`cat /proc/sys/fs/nr_open`
    MIN_FDS=1024
    if [ $FILE_MAX -gt $NR_OPEN ]; then
        FDS=$NR_OPEN
    else
        FDS=$FILE_MAX
    fi
    if [ $FDS -lt $MIN_FDS ]; then
        FDS=$MIN_FDS
    fi
    echo $FDS 
}

probe() {
    # Check that networking is up.
    [ ${NETWORKING} = "no" ] && exit 1

    [ `id -u` -ne 0 ] && exit 4

    # check if the squid conf file is present
    [ -f $SQUID_CONF ] || exit 6
}

start() {
    probe

    parse=`$SQUID -k parse -f $SQUID_CONF 2>&1`
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
        echo -n $"Starting $prog: "
        echo_failure
        echo
        echo "$parse"
        return 1
    fi
    for adir in $CACHE_SWAP; do
        if [ $ROCK_STORE -ne 0 ] && [ ! -d $adir/00 ]; then
            echo -n "init_cache_dir $adir... "
            $SQUID -z -F -N -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
        fi
        if [ $ROCK_STORE -eq 0 ] && [ ! -f $adir/rock ]; then
            echo -n "init_cache_db $adir/rock... "
            $SQUID -z -F -N -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
        fi
    done
        ulimit -n $(getmaxfd)
    echo -n $"Starting $prog: "
    $SQUID $SQUID_OPTS -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        timeout=0;
        while : ; do
            [ ! -f /var/run/squid.pid ] || break
            if [ $timeout -ge $SQUID_PIDFILE_TIMEOUT ]; then
                RETVAL=1
                break
            fi
            sleep 1 && echo -n "."
            timeout=$((timeout+1))
        done
    fi
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SQUID
    [ $RETVAL -eq 0 ] && echo_success
    [ $RETVAL -ne 0 ] && echo_failure
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    
    killproc /usr/sbin/squid
    RETVAL=$?
    rm -f /var/lock/subsys/$SQUID
    timeout=0
    while : ; do
        # squid does not wait until all children are gone before removing pid file,
        # so we wait until all processes are gone.
        $(ps -U squid -u squid | grep -q squid) || break
        if [ $timeout -ge $SQUID_SHUTDOWN_TIMEOUT ]; then
            echo
            return 1
        fi

        # during start of squid parent process does not wait until all children are started
        # and operational before creating the pid file. If in that case squid should be stopped
        # while children are in startup phase, those children will not be stopped and remain alive
        # We kill them away after a while
        if [ $timeout -ge $SQUID_FORCEKILL_TIMEOUT ]; then
            echo "K"
            ps -u squid -U squid | grep squid | awk '{ print $1'} | xargs -i kill -9 {}
        elif [ $timeout -ge $SQUID_FORCETERM_TIMEOUT ]; then
            echo "k"
            ps -u squid -U squid | grep squid | awk '{ print $1'} | xargs -i kill {}
        fi
        sleep 2 && echo -n "."
        timeout=$((timeout+2))
    done
    echo_success
    echo
    return $RETVAL
}

flush() {
    status squid &>/dev/null
    running=$?
    condstop

    for adir in $CACHE_SWAP; do
    rm -Rf $adir/*
    done

    [ $running -eq 0 ] && start
}

reload() {
    $SQUID $SQUID_OPTS -k reconfigure -f $SQUID_CONF
}

restart() {
    stop
    start
}

condrestart() {
    status squid &>/dev/null && restart || :
}

condstop() {
    status squid &>/dev/null && stop || :
}

rhstatus() {
    status $SQUID && $SQUID -k check -f $SQUID_CONF
}


case "$1" in
start)
    start
    ;;

stop)
    stop
    ;;

reload|force-reload)
    reload
    ;;

restart)
    restart
    ;;

condrestart|try-restart)
    condrestart
    ;;

condstop)
    condstop
    ;;

status)
    rhstatus
    ;;

probe)
    probe
    ;;
flush)
    flush
    ;;
*)
    echo $"Usage: $0 {start|stop|status|reload|force-reload|restart|try-restart|condstop|condrestart|probe|flush}"
    exit 2
esac

exit $?
