#!/bin/sh
#
# Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#              HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /etc/httpd/conf/access.conf
# config: /etc/httpd/conf/httpd.conf
# config: /etc/httpd/conf/srm.conf

# Source function library.
. /usr/local/etc/rc.d/functions

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=/usr/sbin/httpd
prog=httpd
RETVAL=0
PIDFILE=/var/run/httpd.pid

# Find the installed modules and convert their names into arguments httpd
# can use.
moduleargs() {
        moduledir=/usr/lib/apache
        moduleargs=`
        /usr/bin/find ${moduledir} -type f -perm -0100 -name "*.so" | env -i tr '[:lower:]' '[:upper:]' | awk '{\
        gsub(/.*\//,"");\
        gsub(/^MOD_/,"");\
        gsub(/^LIB/,"");\
        gsub(/\.SO$/,"");\
        print "-DHAVE_" $0}'`
        echo ${moduleargs}
}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        ulimit -n 8192
        ulimit -n 16384
        echo -n "Starting $prog: "
        daemon $httpd `moduleargs` $OPTIONS
        RETVAL=$?
        if [ $RETVAL = 0 ] && touch /var/spool/lock/httpd
        then
                echo -e "\t[ OK ]";
        else
                echo -e "\t[ FAILED ]";
        fi
        return $RETVAL
}
waitforexit() {
        count=${2:-30}
        while [ 0$count -gt 0 ]
        do
                PIDS=`ps -ax | grep -v grep | grep -c /usr/sbin/httpd` || break
                echo Remaining processes: $PIDS
                stop
                sleep 2
                count=`expr $count - 1`
        done
        if [ 0$count -eq 0 ];
        then
                echo Remaining processes: $PIDS
                return 1
        fi
        return 0
}
stop() {
        echo -n "Stopping $prog: "
        killall $prog 2> /dev/null
        RETVAL=$?
        if [ $RETVAL = 0 ] && rm -f /var/spool/lock/httpd $PIDFILE
        then
                echo -e "\t[ OK ]";
        else
                echo -e "\t[ FAILED ]";
        fi
}
reload() {
        echo -n "Reloading $prog: "
        killall -HUP $prog
        RETVAL=$?
        if [ $RETVAL = 0 ]
        then
                echo -e "\t[ OK ]";
        else
                echo -e "\t[ FAILED ]";
        fi
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status $httpd
        RETVAL=$?
        ;;
  restart)
        stop
	waitforexit "httpd" 20
        start
        ;;
  condrestart)
        if [ -f $PIDFILE ] ; then
                stop
                start
        fi
        ;;
  reload)
        reload
        ;;
  oldgraceful)
        stop
        start
        ;;
  graceful|help|configtest)
        $apachectl $@
        RETVAL=$?
        ;;
  test)
        echo `moduleargs`;
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
        exit 1
esac

exit $RETVAL

