#!/bin/sh
#
# /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

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

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

# Kerberos keytab file
KRB5_KTNAME=/etc/squid/squid.keytab

SQUID=/usr/sbin/squid
SQUID_ULIMIT=4096
SQUID_CONF="/etc/squid/squid.conf"

# Overwrite something
SourceIfNotEmpty /etc/sysconfig/squid

export KRB5_KTNAME

LOCKFILE=/var/lock/subsys/squid
PIDFILE=/var/run/squid.pid
RETVAL=0

start()
{
	is_yes "$NETWORKING" || return 0

	([ -s "$SQUID_CONF" ] && grep '^[[:blank:]]*cache_dir[[:blank:]]' "$SQUID_CONF"; echo "cache_dir type /var/spool/squid") |
	while read tag type adir dummy; do
		if [ ! -d $adir/00 ]; then
			$SQUID -z -F 2>/dev/null
			break
		fi
	done

	ulimit -n $SQUID_ULIMIT 2>/dev/null ||:
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- $SQUID -f "$SQUID_CONF" $SQUID_OPTS
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user squid -- $SQUID
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	action "Reloading squid service:" $SQUID -k reconfigure
	RETVAL=$?
	return $RETVAL
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		[ -e "$LOCKFILE" ] && stop
		;;
	condrestart)
		[ -e "$LOCKFILE" ] && restart
		;;
	condreload)
		[ -e "$LOCKFILE" ] && reload
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user squid squid
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
