#! /bin/sh
#
# apache	Start the apache HTTP server.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/apache
NAME=apache

test -f $DAEMON || exit 0

if [ -d /var/run/apache ]
then
    PIDFILE=/var/run/apache/$NAME.pid
else
    PIDFILE=/var/run/$NAME.pid
fi

case "$1" in
  start)
    . /usr/lib/apache/config/scripts/functions.sh
    . /usr/lib/apache/config/scripts/variables.sh

    if [ "$serverroot" != "$wwwhome" -a "$serverroot" != /var/web/webspace ]
    then
    	cfgargs="$cfgargs -d $serverroot"
    fi

    if [ "$httpd" != /etc/apache/httpd.conf ]
    then
    	cfgargs="$cfgargs -f $httpd"
    fi

    if [ ! -z "$cfgargs" ]
    then
    	cfgargs="-- $cfgargs"
    fi

    start-stop-daemon --start --verbose --pidfile $PIDFILE \
	--exec $DAEMON $cfgargs
    ;;

  stop)
    start-stop-daemon --stop --verbose --pidfile $PIDFILE --exec $DAEMON

    # Verify that it was killed, start-stop-daemon sometimes fails without
    # reason...

    if [ -r $PIDFILE ]
    then
	pid=`head -1 $PIDFILE`
	if 2>/dev/null kill -0 $pid
	then
	    kill -9 $pid
	fi
    fi
    ;;

  reload)
    echo "Reloading $NAME configuration files"
    start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE \
	--exec $DAEMON
    ;;

  reload-modules)
    echo "Reloading $NAME configuration files and modules..."
    /etc/init.d/$NAME stop
    echo Sleeping a few seconds...
    sleep 3
    /etc/init.d/$NAME start
    ;;

  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|reload|reload-modules}"
    exit 1
    ;;
esac

exit 0

