#! /bin/sh
#
# squid		Daily cron script for squid.
#
# Version:	@(#)squid.cron.daily  1.40  06-Jul-1998  miquels@cistron.nl
#

LOG_HISTORY=1

LOGDIR=/var/log/squid
cd $LOGDIR || exit 1

#
#	Gzip rotated logfiles, extensions 1 .. $LOG_HISTORY
#
zipit () {
	i=$LOG_HISTORY
	while [ $i -ge 1 ]
	do
		if [ -f $1.$i ]
		then
			gzip -9f $1.$i
		fi
		i=$(($i - 1))
	done
}

#
#	Rotate logfile(s).
#
rotate () {
	i=$((LOG_HISTORY - 1))
	while [ $i -ge 0 ]
	do
		if [ -f $1.$i ]
		then
			mv -f $1.$i $1.$(($i + 1))
		fi
		if [ -f $1.$i.gz ]
		then
			mv -f $1.$i.gz $1.$(($i + 1)).gz
		fi
		i=$(($i - 1))
	done
	[ -f $1 ] && mv $1 $1.0
}
 
#
#	See if squid rotates the logfile for us.
#	Default for debian and squid 1.2 is "logfile_rotate 0"
#
R=`grep '^logfile_rotate' /etc/squid.conf | sed 's/^.* //'`
if [ "$R" = 0 -o "$R" = "" ]
then
	#
	#	Do rotate ourself.
	#
	for f in access.log cache.log hierarchy.log store.log useragent.log
	do
		[ -f $f ] && rotate $f
	done
fi

#
#	Now, let squid rotate or at least just flush the logfiles.
#
/usr/sbin/squid -k rotate
sleep 5

#
#	Gzip all logfiles with extension > .0
#
for f in access.log cache.log hierarchy.log store.log useragent.log
do
	zipit $f
done

#
#	Rotate the squid.out logfile
#
savelog -c 3 squid.out > /dev/null 2>&1

