#!/bin/sh
#
#	@(#) squid 15.1 99/09/02 
#
# Copyright (c) 1999 The Santa Cruz Operation, Inc.. All Rights Reserved.
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF THE SANTA CRUZ OPERATION INC.
# The copyright notice above does not evidence any actual or intended
# publication of such source code.
# 
#
# squid start/stop script
# Usage: squid [ start | stop | enable | disable | query | restart | rotate ]
#

# definitions
USAGE="Usage: %s [ start | stop | enable | disable | query | restart | rotate ]"
SQUID_HOME="/usr/lib/squid"
LABEL="UX:`basename $0`"
CAT="squid.cat"

# paths to squid files, and a label
SQUID_LABEL="squid"
SQUID_BINARY="$SQUID_HOME/bin/squid"
SQUID_PIDFILE="$SQUID_HOME/logs/squid.pid"

# more paths, and some wait timeouts
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/etc"; export PATH
SCRIPTDIR="/etc/init.d"
SCRIPT="squid"
SCRIPTNUM="S91"
STOP_TIMEOUT=90
START_TIMEOUT=30

#############################################################################
#
# squid_start
#
# start the server daemon running
#
#############################################################################
squid_start() {
	$SQUID_BINARY -k check 2> /dev/null
	if [ $? -eq 0 ]; then
		dspmsg $CAT 2 "%s is already running.\n" $SQUID_LABEL
	else
		dspmsg $CAT 3 "Starting %s ... " $SQUID_LABEL
		$SQUID_BINARY -sY 
		count=0; while [ $count -lt $START_TIMEOUT ]; do
			sleep 1                 # wait for startup
			$SQUID_BINARY -k check 2> /dev/null
			if [ $? -eq 0 ]; then
				break
			fi
			count=`expr $count + 1`
		done
		if [ $count -lt $START_TIMEOUT ]; then
			dspmsg $CAT 4 "done.\n"
		else
			echo "" #newline
			dspmsg $CAT 5 "%s could not be started\n" $SQUID_LABEL
		fi
	fi
}

#############################################################################
#
# squid_stop
#
# stop the server daemon if it is running
#
#############################################################################
squid_stop() {
	$SQUID_BINARY -k check 2> /dev/null
	if [ $? -eq 0 ]; then
		dspmsg $CAT 6 "Stopping %s ... " $SQUID_LABEL
		PPID=`ps -ef | grep $SQUID_BINARY | awk ' { print $2 } '`
                kill $PPID
		PID=`ps -ef | grep squid | grep sY | awk ' { print $2 } '`
                kill $PID
		$SQUID_BINARY -k shutdown
		count=0; while [ $count -lt $STOP_TIMEOUT ]; do
			sleep 1			# let server stop
			$SQUID_BINARY -k check 2> /dev/null
			if [ $? -ne 0 ]; then
				dspmsg $CAT 7 "done.\n"
				break
			fi
			count=`expr $count + 1`
		done
		if [ $count -ge $STOP_TIMEOUT ]; then
			echo "" #newline
			dspmsg $CAT 8 "stop wait timed out.\n"
		fi
	else
		dspmsg $CAT 9 "%s does not appear to be running.\n" $SQUID_LABEL
	fi
}

#############################################################################
#
# squid_restart
#
# restart the server daemon by sending it a SIGHUP
#
#############################################################################
squid_restart() {
	if [ -r $SQUID_PIDFILE ]; then
		read line < $SQUID_PIDFILE
		set -- junk $line		# protects against empty line
		shift				# get rid of junk
		KILL_PID=$1
		kill -0 $KILL_PID 2>/dev/null
		if [ $? -eq 0 ]; then
			dspmsg $CAT 10 "Restarting %s ... " $SQUID_LABEL
			kill -HUP $KILL_PID 2>/dev/null
			dspmsg $CAT 11 "done.\n"
		else
			dspmsg $CAT 13 "%s does not appear to be running.\n" $SQUID_LABEL
		fi
	else
		dspmsg $CAT 13 "%s does not appear to be running.\n" $SQUID_LABEL
	fi
}

#############################################################################
#
# squid_enable
#
# add a link to this script into the /etc/rc2.d/ directory, then start
# the server
#
#############################################################################
squid_enable() {
	if [ -f /etc/rc2.d/$SCRIPTNUM$SCRIPT ]; then
		dspmsg $CAT 14 "%s is already enabled.\n" $SQUID_LABEL
	else
		dspmsg $CAT 15 "Enabling %s ... " $SQUID_LABEL
		ln $SCRIPTDIR/$SCRIPT /etc/rc2.d/$SCRIPTNUM$SCRIPT
		dspmsg $CAT 16 "done.\n"
	fi
	squid_start
}

#############################################################################
#
# squid_disable
#
# stop the server, then remove any links to this script in /etc/rc2.d/
#
#############################################################################
squid_disable() {
	squid_stop
	if [ -f /etc/rc2.d/$SCRIPTNUM$SCRIPT ]; then
		dspmsg $CAT 17 "Disabling %s ... " $SQUID_LABEL
		rm -f /etc/rc2.d/$SCRIPTNUM$SCRIPT
		dspmsg $CAT 18 "done.\n"
	else
		dspmsg $CAT 19 "%s is already disabled.\n" $SQUID_LABEL
	fi
}

#############################################################################
#
# squid_query 
#
# query the server to see if it is running
#
#############################################################################
squid_query() {
	$SQUID_BINARY -k check 2> /dev/null
	if [ $? -eq 0 ]; then
		dspmsg $CAT 20 "%s is currently running.\n" $SQUID_LABEL
	else
		dspmsg $CAT 21 "%s is not running.\n" $SQUID_LABEL
	fi
	if [ -f /etc/rc2.d/$SCRIPTNUM$SCRIPT ]; then
		dspmsg $CAT 22 "%s is enabled.\n" $SQUID_LABEL
	else
		dspmsg $CAT 23 "%s is disabled.\n" $SQUID_LABEL
	fi
}

#############################################################################
#
# squid_rotate
#
# Rotates the logfiles by using squid's -k rotate command line option.
# If server is not running, this starts the server, rotates the logs, and
# then stops the server.
# 
#############################################################################
squid_rotate() {
	$SQUID_BINARY -k check 2> /dev/null
	#If it is running, rotate the logs
	if [ $? -eq 0 ]; then
		dspmsg $CAT 12 "Rotating logs ... "
		if $SQUID_BINARY -k rotate ; then 
			dspmsg $CAT 18 "done.\n"
		else
			echo "" #newline
			dspmsg $CAT 24 "Unable to rotate logs.\n"	
		fi
	#if it is not running, start it, rotate logs, and then stop it
	else
		squid_start
		$SQUID_BINARY -k check 2> /dev/null
		if [ $? -eq 0 ]; then
                	dspmsg $CAT 12 "Rotating logs ... "
                	if $SQUID_BINARY -k rotate ; then
	                        dspmsg $CAT 18 "done.\n"
        	        else
                	        echo "" #newline
                        	dspmsg $CAT 24 "Unable to rotate logs.\n"      
			fi
			squid_stop
		else
			dspmsg $CAT 24 "Unable to rotate logs.\n"
                fi
	fi
}


#############################################################################
# main()
#############################################################################

# squid creates cached entries on disk using existing umask
# uw7's default of 0022 allows world readability, a security issue when
# dealing with cookies.
umask 0027

# Check usage
if [ $# -gt 1 ]; then
        dspmsg $CAT 1 "$USAGE\n" $0
        exit 1
fi

# function to perform is in $1
FUNCTION="$1"

# execute the routine for the requested function
case $FUNCTION in
start)
	squid_start	;;
stop)
	squid_stop	;;
enable)
	squid_enable	;;
disable)
	squid_disable	;;
query)
	squid_query	;;
status)
	squid_query	;;
restart)
	squid_restart	;;
rotate)
	squid_rotate	;;
*)
	dspmsg $CAT 1 "$USAGE\n" $0
	exit 1	;;
esac

#done
exit 0
