#!/bin/bash
#
# /etc/init.d/squid
#
# squid		This shell script takes care of starting and stopping
#		Squid Internet Object Cache
#
# chkconfig: - 90 25
# 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.
# pidfile: /var/run/squid.pid
# config: /etc/squid/squid.conf

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

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

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

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# check if the squid conf file is present
[ -f /etc/squid/squid.conf ] || exit 0

# determine the name of the squid binary
[ -f /usr/sbin/squid ] && SQUID=squid
[ -z "$SQUID" ] && exit 0

# default squid options
# -D disables initial dns checks. If you most likely will not to have an
#    internet connection when you start squid, uncomment this
SQUID_OPTS="-D"

RETVAL=0
LOCKFILE=/var/lock/subsys/$SQUID

start()
{
	echo -n "Starting $SQUID: "
	# determine which one is the cache_swap directory
	CACHE_SWAP=`sed -e 's/#.*//g' /etc/squid/squid.conf | \
    		    grep cache_dir | sed -e 's/cache_dir//' | \
		    cut -d ' ' -f 2`
	[ -z "$CACHE_SWAP" ] && CACHE_SWAP=/var/spool/squid
	
        for adir in $CACHE_SWAP; do
         if [ ! -d $adir/00 ]; then 
	     #echo -n "init_cache_dir $adir... "
	     $SQUID -z -F 2>/dev/null
	 fi
	done
	
	# start daemons
	daemon $SQUID $SQUID_OPTS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	echo
}	

stop()
{
	echo -n "Shutting down $SQUID: "
	# stop daemons
	killproc $SQUID
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	echo
}

restart()
{
	stop
	start
}

reload()
{
	# cause the service configuration to be reread, either with kill -HUP:
	echo -n "Reloading $SQUID: "
	killproc $SQUID -HUP
	RETVAL=$?
	echo
	# or by simple restarting the daemons in a manner similar to restart above.
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
        ;;
  reload)
	reload
	;;
  restart)
        restart
        ;;
  condstop)
	# Stop the servce if it is already running, for example:
	if [ -e "$LOCKFILE" ]; then
 	 stop
	fi
	;;
  condrestart)
	# Restart the servce if it is already running, for example:
	if [ -e "$LOCKFILE" ]; then
 	 restart
	fi
	;;
  status)
	# report the status of the daemons in free-form format,
	# perhaps with the status function, for example:
	status $SQUID
	RETVAL=$?
	;;
  *)
	echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
	RETVAL=1
esac

exit $RETVAL
