#!/bin/sh
# chkconfig: 2345 55 45
### BEGIN INIT INFO
# Provides:          lighttpd
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start lighttpd at boot time
# Description:       Enable service provided by lighttpd.
# X-Start-Before:    
# X-Stop-After:      
# X-Timesys-Start-Number:  55
# X-Timesys-Stop-Number:  45
### END INIT INFO

umask 022

prefix=/usr
piddir=/var/run
config=/etc/lighttpd.conf

LIGHTTPD=$prefix/sbin/lighttpd
PIDFILE=$piddir/lighttpd.pid

stop_service() {
    if [  -r $PIDFILE  -a  ! -z ${PIDFILE}  ]; then
	    PID=`cat ${PIDFILE}`
    fi
    if [  ${PID:=0} -gt 1 -a  ! "X$PID" = "X "  ]; then
	    kill ${PID}
    else
	    echo "Unable to read PID file"
    fi
}

start_service() {

    mkdir -p $piddir
    mkdir -p /srv/www/htdocs/
    mkdir -p /var/log/lighttpd/ 
    
    # Start lighttpd
    printf "Starting $LIGHTTPD: "         ; $LIGHTTPD -f $config

    lighttpd_rc=$?
    if [ $lighttpd_rc -ne 0 ]; then
            echo "[FAIL]"
	    echo "$0: Error ${lighttpd_rc} starting ${LIGHTTPD}... bailing."
	    exit $lighttpd_rc
    fi
    echo "[ OK ]"
}

case $1 in

'start')
    start_service
    ;;

'stop')
    stop_service
    ;;

'restart')
    stop_service
    start_service
    ;;

*)
    echo "Usage: $0 [start|stop|restart]"
    ;;
esac
