#!/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_CONF="/etc/squid/squid.conf"

# Overwrite something
SourceIfNotEmpty /etc/sysconfig/squid

GetConfVal()
{
	[ -n "$1" ] || return
	local d="$2"
	set -- $(grep "^[[:blank:]]*$1[[:blank:]]" $SQUID_CONF | tail -1)
	if [ -n "$2" ]; then
		echo "$2"
	elif [ -n "$d" ]; then
		echo "$d"
	fi
}

export KRB5_KTNAME

LOCKFILE=/var/lock/subsys/squid
PIDFILE="$(GetConfVal pid_filename /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 --foreground -z -F 2>/dev/null
			break
		fi
	done

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

stop()
{
	local t=${SQUID_SHUTDOWN_TIMEOUT:-$(GetConfVal shutdown_lifetime 30)}
	local T=$((t + ${SQUID_EXTRA_SHUTDOWN_TIMEOUT:-5}))
	msg_stopping squid
	printf $"Wait about %d...%d seconds..." $t $T
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--retry $T \
		--no-announce \
		--expect-user root -- $SQUID
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	action "Reloading squid service:" $SQUID -f "$SQUID_CONF" -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 root squid
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
