#! /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

# Include varnish defaults
. /etc/sysconfig/varnish

DAEMON="/usr/sbin/varnishd"

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

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

# See how we were called.
case "$1" in
  start)
	echo -n "Starting varnish HTTP accelerator: "

        # $DAEMON_OPTS is set in /etc/sysconfig/varnish. At least, one
        # has to set up a backend, or /tmp will be used, which is a bad idea.
	if [ "$DAEMON_OPTS" = "" ]; then
	    echo "\$DAEMON_OPTS empty."
	    echo -n "Please put configuration options in /etc/sysconfig/varnish"
	    echo_failure
	else
	    daemon ${DAEMON} "$DAEMON_OPTS" -P ${PIDFILE} > /dev/null 2>&1
	    sleep 1
	    pkill -0 $PROCNAME
	    RETVAL=$?
	    if [ $RETVAL -eq 0 ]
		then
		echo_success
		touch $LOCKFILE
	    else
		echo_failure
	    fi	
	fi
	echo
	;;
  stop)
	echo -n "Stopping varnish HTTP accelerator: "
	killproc $DAEMON
	RETVAL=$?
	if [ $RETVAL -eq 0 ]
	then
		echo_success
		rm -f $LOCKFILE $PIDFILE
	else
		echo_failure
	fi
	echo
	;;
  status)
	status $PROCNAME
	RETVAL=$?
	;;
  restart|reload)
  	$0 stop
	$0 start
	RETVAL=$?
	;;
  condrestart)
        if [ -f $PIDFILE ]; then
            $0 stop
            $0 start
            RETVAL=$?
        fi
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart|condrestart}"
	exit 1
esac

exit $RETVAL
