# squid - SQUID HTTP proxy-cache
#

description	"HTTP proxy-cache"
author		"Chuck Short <zulcss@ubuntu.com>"

# The second "or" condition is to start squid in case it failed to start
# because no real interface was there.
start on runlevel [2345]
stop on runlevel [!2345]

respawn
normal exit 0

kill signal SIGINT
kill timeout 40

env CONFIG="/etc/squid3/squid.conf"
env SQUID_ARGS="-YC"

pre-start script
	/lib/init/apparmor-profile-load usr.sbin.squid3
	if [ -f /etc/default/squid3 ]; then
		. /etc/default/squid3
	fi

	find_cache_dir () {
		w="     " # space tab
		res=`sed -ne '
			s/^'$1'['"$w"']\+[^'"$w"']\+['"$w"']\+\([^'"$w"']\+\).*$/\1/p;
			t end;
			d;
			:end q' < $CONFIG`
		[ -n "$res" ] || res=$2
		echo "$res"
	}

	find_cache_type () {
	  w="   " # space tab
	  res=`sed -ne '
	    s/^'$1'['"$w"']\+\([^'"$w"']\+\).*$/\1/p;
	    t end;
	    d;
	    :end q' < $CONFIG`
	  [ -n "$res" ] || res=$2
	  echo "$res"
	}

	cache_dir=`find_cache_dir cache_dir`
	cache_type=`find_cache_type cache_dir`

	if [ "$cache_type" = "coss" -a -d "$cache_dir" -a ! -f "$cache_dir/stripe" ] ||
	   [ "$cache_type" != "coss" -a -d "$cache_dir" -a ! -d "$cache_dir/00" ]
	then
		/usr/sbin/squid3 $SQUID_ARGS -z -f $CONFIG
	fi
end script

script
	if [ -f /etc/default/squid3 ]; then
		. /etc/default/squid3
	fi

	umask 027
	ulimit -n 65535
	exec /usr/sbin/squid3 -N $SQUID_ARGS -f $CONFIG
end script
