#!/bin/sh
#
# varnish	Control the varnish HTTP accelerator
#
# chkconfig: 345 90 10
# description: HTTP accelerator
# processname: varnishd
# config: /etc/varnish.conf
# pidfile: /var/run/varnish/varnishd.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config - may override defaults
[ -f /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "Varnish HTTP accelerator"
		exit 1
	fi
else
	exit 0
fi

PIDFILE=/var/run/varnishd.pid

# import some env from parent process
if [ -f /proc/$PPID/environ ]; then
	eval $(tr '\0' '\n' < /proc/$PPID/environ | grep -E '^(USER|SUDO_USER|LOGNAME)=')
	LOGNAME=${SUDO_USER:-${LOGNAME:-$USER}}
fi

# Generate a label, prefixed with the caller's username, from the
# kernel random uuid generator, fallback to timestamp
if [ -f /proc/sys/kernel/random/uuid ]; then
	read uuid < /proc/sys/kernel/random/uuid
	vcl_label="$(date +${LOGNAME}${LOGNAME:+:}%Y-%m-%d:%H.%M:${uuid})"
else
	vcl_label="$(date +${LOGNAME}${LOGNAME:+:}%s.%N)"
fi

varnishadm() {
	/usr/bin/varnishadm -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} "$@"
}
# for simplier code
vcl_load() {
	varnishadm vcl.load "$@"
}
vcl_use() {
	varnishadm vcl.use "$@"
}
vcl_discard() {
	varnishadm vcl.discard "$@"
}

# configtest itself
# must return non-zero if check failed
# output is discarded if checkconfig is ran without details
configtest() {
	local rc=0

	if [ -f /var/lock/subsys/varnish ]; then
		vcl_load $vcl_label ${VARNISH_VCL_CONF}; rc=$?
		# discard only if above succeeded
		if [ $rc = 0 ]; then
			vcl_discard $vcl_label || rc=$?
		else
			# this will display a bit more verbose error
			/usr/sbin/varnishd -C -f ${VARNISH_VCL_CONF} >/dev/null
		fi

	else
		echo "Varnish is not running, config cannot be tested"
	fi
	return $rc
}

# wrapper for configtest
checkconfig() {
	local details=${1:-0}

	if [ $details = 1 ]; then
		# run config test and display report (status action)
		show "Checking %s configuration" "Varnish HTTP accelerator"; busy
		local out
		out=$(configtest 2>&1)
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			ok
		else
			fail
		fi
		[ "$out" ] && echo >&2 "$out"
	else
		# run config test and abort with nice message if failed
		# (for actions checking status before action).
		configtest >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL != 0 ]; then
			show "Checking %s configuration" "Varnish HTTP accelerator"; fail
			nls 'Configuration test failed. See details with %s "checkconfig"' $0
			exit $RETVAL
		fi
	fi
}

start() {
	if [ -f /var/lock/subsys/varnish ]; then
		msg_already_running "Varnish HTTP accelerator"
		return
	fi

	# default limits
	SERVICE_LIMITS="${SERVICE_LIMIS:--n $NFILES -l $MEMLOCK}"

	# DAEMON_OPTS is used by the init script. If you add or remove options, make
	# sure you update this section, too.
	DAEMON_OPTS="
		-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT}
		-f ${VARNISH_VCL_CONF}
		-T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT}
		-t ${VARNISH_TTL}
		-u ${VARNISH_USER} -g ${VARNISH_GROUP}
		-w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT}
		-n ${VARNISH_NAME}
		-s ${VARNISH_STORAGE}
		-P ${PIDFILE}
	"

	# iterate over $VARNISH_OPTS
	for param in $VARNISH_PARAMS; do
		DAEMON_OPTS="$DAEMON_OPTS -p $param"
	done

	msg_starting "Varnish HTTP accelerator"
	daemon /usr/sbin/varnishd $DAEMON_OPTS $VARNISH_OPTS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/varnish
}

stop() {
	if [ ! -f /var/lock/subsys/varnish ]; then
		msg_not_running "Varnish HTTP accelerator"
		return
	fi

	msg_stopping "Varnish HTTP accelerator"
	killproc --pidfile $PIDFILE /usr/sbin/varnishd
	rm -f /var/lock/subsys/varnish
}

condrestart() {
	if [ ! -f /var/lock/subsys/varnish ]; then
		msg_not_running "Varnish HTTP accelerator"
		RETVAL=$1
		return
	fi

	stop
	start
}

restart() {
	if [ -f /var/lock/subsys/varnish ]; then
		# checkconfig needs running varnish
		checkconfig
	fi
	stop
	start
}

reload() {
	if [ ! -f /var/lock/subsys/varnish ]; then
		msg_not_running "Varnish HTTP accelerator"
		RETVAL=7
		return
	fi

	checkconfig
	msg_reloading "Varnish HTTP accelerator"; busy
	out=$(vcl_load $vcl_label ${VARNISH_VCL_CONF} && vcl_use $vcl_label 2>&1)
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
		ok
	else
		echo >&2 "$out"
		fail
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload)
  	reload
	;;
  checkconfig|configtest)
	checkconfig 1
	;;
  varnishadm)
	shift
	varnishadm "$@"
	;;
  status)
	status --pidfile $PIDFILE varnishd
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|checkconfig|varnishadm|status}"
	exit 3
esac

exit $RETVAL
