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

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

RETVAL=0
PROCNAME=varnishd
PIDFILE=/var/run/varnish.pid
LOCKFILE=/var/lock/subsys/varnish
DAEMON="/usr/sbin/varnishd"

# Include varnish defaults
. /etc/sysconfig/varnish

mkdir -p /var/run/varnish 2>/dev/null

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

start()
{
	if [ "$DAEMON_OPTS" = "" ]; then
	    echo "\$DAEMON_OPTS empty."
	    echo -n "Please put configuration options in /etc/sysconfig/varnish"
	    RETVAL=2
	    return $RETVAL
	else
	    start_daemon --pidfile ${PIDFILE} --lockfile "$LOCKFILE" --expect-user root -- ${DAEMON} "${DAEMON_OPTS}" -P ${PIDFILE}
	    RETVAL=$?
	    return $RETVAL
	fi
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- ${DAEMON}
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	stop_daemon --pidfile "$PIDFILE" --expect-user root -HUP -- ${DAEMON}
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user root -- ${DAEMON}
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
