#!/bin/sh
#
#  $Id: RunCache.in,v 1.3.2.4 1996/05/24 06:40:39 wessels Exp $

prefix=/usr/lib/squid
exec_prefix=${prefix}
[ "$logdir" = "" ] && logdir=$prefix
PIDFILE=/var/run/runcache.pid
PATH=${exec_prefix}:/bin:/sbin:/usr/sbin:/usr/bin
export PATH

#
#	Just to be sure, check if squid is not already running.
#
if [ -f /var/run/squid.pid ]
then
	spid=`cat /var/run/squid.pid`
	kill -CONT $spid 2>/dev/null
	if [ $? = 0 ]
	then
		#echo "RunCache: squid is already running." >&2
		exit 0
	fi
fi

rm -f $PIDFILE
echo $$ > $PIDFILE

L=$logdir/squid.out

conf=""
if test "$1" ; then
	conf="-f $1"
	shift
fi

failcount=0
while : ; do
	echo "`date`: Running: squid -D -s $conf >> $L 2>&1" >>$L
	if [ $failcount -gt 0 ]
	then
	  echo "`date`: Running: squid -D -s $conf >> $L 2>&1"
	fi
	start=`date '+%d%H%M%S'`
	squid -D -s $conf >> $L 2>&1
	stop=`date '+%d%H%M%S'`
	t=`expr $stop - $start`
	if test 0 -le $t -a $t -lt 5 ; then
		failcount=`expr $failcount + 1`
	else
		failcount=0
	fi
	if test $failcount -gt 5 ; then
          echo "`date`: RunCache: EXITING DUE TO REPEATED FREQUENT FAILURES" >&2
          echo "`date`: RunCache: EXITING DUE TO REPEATED FREQUENT FAILURES" >$L
	  exit 1
	fi
	sleep 10
done
