#!/bin/sh
#
# Starts lighttpd.
#

start() {
	echo -n "Starting lighttpd: "
	start-stop-daemon -S -q -p /var/run/lighttpd.pid --exec /usr/sbin/lighttpd -- -f /etc/lighttpd/lighttpd.conf
	echo "OK"
}
stop() {
	echo -n "Stopping lighttpd: "
	start-stop-daemon -K -q -p /var/run/lighttpd.pid
	echo "OK"
}

reload() {
        /usr/sbin/lighttpd -t -f /etc/lighttpd/lighttpd.conf || exit $?
        echo "Reloading lighttpd configuration"
        if start-stop-daemon --stop --signal INT --quiet \
            --pidfile /var/run/lighttpd.pid --exec /usr/sbin/lighttpd
        then
            rm /var/run/lighttpd.pid
            if start-stop-daemon --start --quiet  \
                --pidfile /var/run/lighttpd.pid --exec /usr/sbin/lighttpd -- -f /etc/lighttpd/lighttpd.conf ; then
                exit 0
            else
                exit 1
            fi
        else
            exit 1
        fi
}

restart() {
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  reload)
	reload
	;;
  restart)
	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?


