#!/usr/bin/env bash
#
# lighttpd     Startup script for the lighttpd server
#
# chkconfig: 35 21 80
#
# description: Lightning fast webserver with light system requirements
#
# processname: vami-lighttpd
#
# config: /etc/lighttpd/lighttpd.conf
# config: /etc/sysconfig/lighttpd
# pidfile: /var/run/lighttpd.pid
#
# Note: pidfile is assumed to be created
# by lighttpd (config: server.pid-file).
# If not, uncomment 'pidof' line.
#
### BEGIN INIT INFO
# Provides:     vami-lighttpd
# Required-Start:       vaos
# Required-Stop:
# Default-Start:        3 5
# Default-Stop:
### END INIT INFO

export TEXTDOMAINDIR=/opt/vmware/lib/locale
export TEXTDOMAIN=vami-lighttp

#if the following is true, then we can expect this script to work using
# the redhat style init function.  Otherwise we have to use the generic
# version of the code
prog="vami-lighttpd"
lighttpd="/opt/vmware/sbin/$prog"

# Set location of optional pre-start script for vami-lighttpd
LIGHTTPD_PRE_START_SCRIPT="/opt/vmware/share/lighttpd/pre-start.sh"

if [ -f /etc/rc.d/init.d/functions ]; then
  # Source function library
  . /etc/rc.d/init.d/functions

  echo $PATH | grep -q /opt/vmware/sbin ||PATH=/opt/vmware/sbin:$PATH

  LIGHTTPD_CONF_PATH="/opt/vmware/etc/lighttpd/lighttpd.conf"

  RETVAL=0

  start() {
	echo -n `gettext "Starting"` $prog:
    if [ "`pidof $prog`" = "" ]
    then
		## Run pre-start scripts if it exists and is executable
		[ -x "$LIGHTTPD_PRE_START_SCRIPT" ] && "$LIGHTTPD_PRE_START_SCRIPT"
		daemon $lighttpd -f $LIGHTTPD_CONF_PATH
		RETVAL=$?
		echo
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
		return $RETVAL
    else
        echo `gettext "done."` && return 0
    fi
		
  }

  stop() {
	echo -n `gettext "Stopping"` $prog:
    if [ "`pidof $prog`" != "" ]
    then
		killall $lighttpd > /dev/null 2>&1 && sleep 5
		RETVAL=$?
                killall -9 $lighttpd > /dev/null 2>&1
		echo
		[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
		return $RETVAL
    else
        echo `gettext "done."` && return 0
    fi
  }

  reload() {
	echo -n `gettext "Reloading"` $prog:
	killproc $lighttpd -HUP
	RETVAL=$?
	echo
	return $RETVAL
  }

  case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	condrestart)
		if [ -f /var/lock/subsys/$prog ]; then
			stop
			start
		fi
		;;
	reload)
		reload
		;;
	status)
		status $lighttpd
		RETVAL=$?
		;;
	*)
		echo `gettext "Usage:"` "$0 {start|stop|restart|condrestart|reload|status}"
		RETVAL=1
  esac

  exit $RETVAL

else #end of redhat style script
     # this block is the generic script

  echo $PATH | grep -q /opt/vmware/sbin ||PATH=/opt/vmware/sbin:$PATH

	start() {
	    echo -n `gettext "Starting"` $prog:
	    if [ "`pidof $prog`" = "" ]
	    then        
	        ## Run pre-start scripts if it exists and is executable
	        [ -x "$LIGHTTPD_PRE_START_SCRIPT" ] && "$LIGHTTPD_PRE_START_SCRIPT"
	        /opt/vmware/sbin/$prog -f /opt/vmware/etc/lighttpd/lighttpd.conf && echo `gettext "done."` && return 0 ||
	        echo `gettext "failed."`
	        return 1
	    else
	        echo `gettext "done."`
	        return 0        
	    fi
	}   
	
	stop() {
	    echo -n `gettext "Shutting down"` $prog:
            if [ "`pidof $prog`" != "" ]
            then 
	        killall $prog > /dev/null 2>&1 && sleep 5
	        RET=$?
	        killall -9 $prog > /dev/null 2>&1
	        if [ $RET -eq 0 ]; then
	            echo `gettext "done."`
	        else
	            echo `gettext "failed."`
	        fi
	        return $RET
            else
                echo `gettext "done."` && return 0
            fi
        }
	
  reload() {
	echo -n `gettext "Reloading"` $prog `gettext "configuration: "`
	killall -HUP $prog && echo `gettext "done."` && return 0 ||
	echo `gettext "failed."`
	return 1
  }

  case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	echo -n `gettext "Checking"` $prog `gettext "status: "`
	ps --noheaders -C $prog || echo `gettext "stopped."`
	;;
    restart)
    	stop
	start
	;;
    reload)
	reload
	;;
    *)
	echo `gettext "Usage:"` "$prog {start|stop|status|reload|restart}"
	exit 1
	;;
  esac
  exit $?
fi # end of generic style script

