#! /bin/sh
#
# varnish       Control the varnish HTTP accelerator

### BEGIN INIT INFO
# Provides:          varnish
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start HTTP accelerator
# Description:       This script provides a server-side cache
#                    to be run in front of a httpd and should
#                    listen on port 80 on a properly configured
#                    system
### END INIT INFO

# Source function library
. /lib/lsb/init-functions

NAME=varnishd
DESC="HTTP accelerator"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/varnishd
PIDFILE=/var/run/$NAME.pid

test -x $DAEMON || exit 0

# Include varnish defaults if available
if [ -f /etc/default/varnish ] ; then
        . /etc/default/varnish
fi

# Open files (usually 1024, which is way too small for varnish)
ulimit -n ${NFILES:-131072}

# If $DAEMON_OPTS is not set at all in /etc/default/varnish, use minimal useful
# defaults (Backend at localhost:8080, a common place to put a locally
# installed application server.)
DAEMON_OPTS=${DAEMON_OPTS:--b localhost}

case "$1" in
	start)
		output=$(/bin/tempfile -s.varnish)
		log_daemon_msg "Starting $DESC" 
		log_progress_msg $NAME
                if start-stop-daemon \
		   --start --quiet --pidfile ${PIDFILE} --exec ${DAEMON} -- \
                   -P ${PIDFILE} ${DAEMON_OPTS} > ${output} 2>&1; then
			log_end_msg 0
		else
			log_end_msg 1
			cat $output
			exit 1
		fi
		rm $output
		;;
	stop)
		log_daemon_msg "Stopping $DESC"
		log_progress_msg $NAME
		if start-stop-daemon \
		   --stop --quiet --pidfile $PIDFILE --retry 10 \
		   --exec $DAEMON; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
        	;;
	restart|force-reload)
		$0 stop
		$0 start
        	;;
	*)
        	log_success_msg "Usage: $0 {start|stop|restart|force-reload}"
        	exit 1
        ;;
esac

exit 0
