#!/bin/sh
#
# Startup script for the Apache Web Server
#
# chkconfig: 345 80 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/httpd.conf

# Source function library.
. /etc/init.d/functions

### Hacks for the broken MSEC
if [ -e /home/httpd ]; then chmod 0755 /home/httpd;fi
chown apache.apache /var/log/httpd
chmod 750 /var/log/httpd
###
export TMPDIR=/tmp

### Hack for bad hostname configuration
thehost=`hostname`
hostname -i 1>/dev/null 2>/dev/null|| hostname localhost
###

### Hack for Status
LYNX="lynx -dump"
STATUSURL="http://localhost/server-status"

LOCKFILE=/var/lock/subsys/httpd

# Change the major functions into functions.
moduleargs() {
	moduledir=/usr/lib/apache
	moduleargs=
	for module in ${moduledir}/*.so ; do
		if [ -x ${module} ] ; then
			module=`echo ${module} | 
			sed -e 's/.*\///g; s/^mod_//g; s/^lib//g; s/\.so//g;'|
			tr '[:lower:]' '[:upper:]'`
			moduleargs="${moduleargs} -DHAVE_$module"
		fi
	done
	echo ${moduleargs}
}

## Hack for ApacheJServ that takes forever to shut down and restart
snooze() {
	for count in 1 2 3 4 5 6 7 8 9 ; do echo -n "."; sleep 1; done
	echo
}

conftest() {
	rc=0
	if [ -x /usr/sbin/httpd ]; then
		action "Checking configuration sanity for httpd: " \
			/usr/sbin/httpd -t `moduleargs` $DEFINE
		rc=$?
	fi
	return $rc
}

start() {
	for tries in 1 2 3; do
	  if [ -x /usr/sbin/httpd -a ! -e /var/run/httpd.pid ]; then 
		if [ $tries -eq 1 ]; then echo -n "Starting httpd: ";fi
		daemon /usr/sbin/httpd `moduleargs` $DEFINE
		echo
		for wait in 1 2 3 4 5;do
			if [ ! -e /var/run/httpd.pid ]; then sleep 1;fi
		done
	  fi
	done
	touch "$LOCKFILE"
}

stop() {
	if [ -x /usr/sbin/httpd  -a -e /var/run/httpd.pid ]; then 
		echo -n "Shutting down httpd: "
		killproc /usr/sbin/httpd
		echo
		if [ -e /usr/lib/apache/mod_jserv.so ]; then
			snooze
			while [ -e /var/run/httpd.pid ]; do
				echo Trying harder
				snooze
				killproc /usr/sbin/httpd
			done
		fi
		rm -f /var/run/httpd.pid
	fi
	rm -f "$LOCKFILE"
}

restart()
{
	stop
	conftest || exit $?
	start
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  extendedstatus)
	if [ -e /var/run/httpd.pid ]; then
  	    $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '	
	    echo
	    echo -n "#######################################"
	    echo    "#######################################"
	else
	    echo "Apache is *not* running."
	    echo
	    exit 1
	fi
	;;
  status)
	echo
	if [ -e /var/run/httpd.pid ]; then 
		echo "Apache is running."
		echo "httpd: `pidof /usr/sbin/httpd`"
		echo
	else
		echo "Apache is *not* running."
		echo
		exit 1
	fi
	echo "Use $0 extendedstatus for more information."
	echo
	;;
  restart)
	restart
	;;
  reload|graceful)
	if [ -e /usr/lib/apache/mod_jserv.so ]; then
		stop
		start
	else
		if [ -x /usr/sbin/httpd -a -e /var/run/httpd.pid ]; then 
			echo -n "Reloading httpd: "
			killproc /usr/sbin/httpd -USR1
			echo
		fi
	fi
	;;
  configtest)
	conftest
	exit $?
	;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	update|condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
  *)
	echo "Usage: ${0##*/} {start|stop|restart|reload/graceful|update|status|configtest|condstop|condrestart}"
	exit 1
esac

hostname $thehost
exit 0
