#!/usr/bin/openrc-run
# Copyright 1999-2019 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# supervised, nous 2022

SQUID_SVCNAME=$( echo "${RC_SVCNAME}" | tr -cd '[a-zA-Z0-9]' )
SQUID_CONF=/etc/squid/${RC_SVCNAME}.conf
SQUID_USER=proxy
SQUID_GROUP=proxy

supervisor=supervise-daemon
supervise_daemon_args="--respawn-delay 5 --respawn-max 10"
#pidfile="/run/${SQUID_SVCNAME}-supervise.pid"
command="/usr/bin/$SQUID_SVCNAME"
command_args="${SQUID_OPTS} -f $SQUID_CONF -N"

extra_started_commands="reload rotate"

depend() {
	use dns net
}

checkconfig() {
	local CONFFILES="$SQUID_CONF /etc/squid/${RC_SVCNAME}.include /etc/squid/${RC_SVCNAME}.include.*"
	if [ ! -f $SQUID_CONF ]; then
		eerror "You need to create $SQUID_CONF first."
		eerror "The main configuration file and all included file names should have the following format:"
		eerror "${CONFFILES}"
		eerror "An example can be found in /etc/squid/squid.conf.default"
		return 1
	fi

	local PIDFILE=$(cat ${CONFFILES} 2>/dev/null 3>/dev/null | awk '/^[ \t]*pid_filename[ \t]+/ { print $2 }')
	[ -z ${PIDFILE} ] && PIDFILE=/run/squid.pid
	if [ /run/${RC_SVCNAME}.pid != ${PIDFILE} ]; then
		eerror "$SQUID_CONF must set pid_filename to"
		eerror "   /run/${RC_SVCNAME}.pid"
		eerror "CAUTION: http_port, cache_dir and *_log parameters must be different than"
		eerror "         in any other instance of squid."
		eerror "Make sure the main configuration file and all included file names have the following format:"
		eerror "${CONFFILES}"
		return 1
	fi

	# Maximum file descriptors squid can open is determined by:
	# a basic default of N=1024
	#  ... altered by ./configure --with-filedescriptors=N
	#  ... overridden on production by squid.conf max_filedescriptors (if,
	#  and only if, setrlimit() RLIMIT_NOFILE is able to be built+used).
	# Since we do not configure hard coded # of filedescriptors anymore,
	# there is no need for ulimit calls in the init script.
	# Use max_filedescriptors in squid.conf instead.

	local CACHE_SWAP=$(cat ${CONFFILES} 2>/dev/null 3>/dev/null | awk '/^[ \t]*cache_dir[ \t]+/ { if ( $2 == "rock" ) printf "%s/rock ", $3; else if ( $2 == "coss" ) printf "%s/stripe ", $3; else printf "%s/00 ", $3; }')
	[ -z "$CACHE_SWAP" ] && CACHE_SWAP="/var/cache/squid/00"
	
	local x
	for x in $CACHE_SWAP ; do
		if [ ! -e $x ] ; then
			ebegin "Initializing cache directory ${x%/*}"
			local ORIG_UMASK=$(umask)
			umask 027

			if ! (mkdir -p ${x%/*} && chown $SQUID_USER:$SQUID_GROUP ${x%/*}) ; then
				eend 1
				return 1
			fi

			local INIT_CACHE_RESPONSE="$($command -z -N -f $SQUID_CONF 2>&1)"
			if [ $? != 0 ] || echo "$INIT_CACHE_RESPONSE" | grep -q "erminated abnormally" ; then
				umask $ORIG_UMASK
				eend 1
				echo "$INIT_CACHE_RESPONSE"
				return 1
			fi

			umask $ORIG_UMASK
			eend 0
			break
		fi
	done
	
	return 0
}

start_pre() {
	checkconfig || return 1
	checkpath -d -q -m 0750 -o $SQUID_USER:$SQUID_GROUP /run/${RC_SVCNAME}

	# see https://wiki.squid-cache.org/MultipleInstances
	ebegin "Starting ${RC_SVCNAME} (service name ${SQUID_SVCNAME}) with KRB5_KTNAME=\"${SQUID_KEYTAB}\" $command ${SQUID_OPTS} -f $SQUID_CONF"
#	KRB5_KTNAME="${SQUID_KEYTAB}" $command ${SQUID_OPTS} -f $SQUID_CONF
#	eend $? && sleep 1
}

stop_pre() {
	ebegin "Stopping ${RC_SVCNAME} with $command -k shutdown -f $SQUID_CONF $SQUID_OPTS"
	$command -k shutdown -f $SQUID_CONF $SQUID_OPTS &
}
stop_post() {
	# Now we have to wait until squid has _really_ stopped.
	sleep 1
	if [ -f /run/${RC_SVCNAME}.pid ] ; then
		einfon "Waiting for squid to shutdown ."
		cnt=0
		while [ -f /run/${RC_SVCNAME}.pid ]; do
			cnt=$(expr $cnt + 1)
			if [ $cnt -gt 60 ] ; then
				# Waited 120 seconds now. Fail.
				echo
				eend 1 "Failed."
				break
			fi
			sleep 2
			printf "."
		done
		echo
	fi
	[ -f /run/${RC_SVCNAME}.pid ] && eerror "Squid shutdown failed, probably service is already down."
	eend 0
}

reload() {
	checkconfig || return 1
	ebegin "Reloading ${RC_SVCNAME} with $command -k reconfigure -f $SQUID_CONF"
	$command -k reconfigure -f $SQUID_CONF -n ${SQUID_SVCNAME}
	eend $?
}

rotate() {
	service_started ${RC_SVCNAME} || return 1
	ebegin "Rotating ${RC_SVCNAME} logs with $command -k rotate -f $SQUID_CONF"
	$command -k rotate -f $SQUID_CONF -n ${SQUID_SVCNAME}
	eend $?
}
