#! /bin/sh
#
# httpd          Start/Stop 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

WITHOUT_RC_COMPAT=1

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

BINARY=/usr/sbin/httpd
PIDFILE=/var/run/httpd.pid
PERLPIDFILE=/var/run/httpd-perl.pid
LOCKFILE=/var/lock/subsys/httpd
RETVAL=0

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"

# 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}
}

conftest() {
	# TODO: translatable form?
	action "Checking configuration sanity for httpd: " \
		"$BINARY" -t `moduleargs` $DEFINE
	RETVAL=$?
	return $RETVAL
}

start()
{
	if [ "$1" != "again" ]; then
		if status --pidfile "$PERLPIDFILE" --expect-user root \
			  --expect-user root -- httpd-perl >&/dev/null; then
			echo "Warning: httpd-perl is already running, check 'service httpd status' now"
			passed "httpd-perl already running"
		fi
	fi
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name libhttpd.ep -- httpd
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name libhttpd.ep -- httpd
	if [ "$1" != "nocheck" -a -f "$PERLPIDFILE" ]; then
		echo "Warning: httpd-perl is running, check 'service httpd-perl extendedstatus'"
		passed "httpd-perl already running"
	fi
	RETVAL=$?
	return $RETVAL
}

reload()
{
	msg_reloading httpd
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name libhttpd.ep -HUP -- httpd
	RETVAL=$?
	return $RETVAL
} 

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

briefstatus()
{
	status --pidfile "$PIDFILE" --expect-user root \
		--expect-user root --name libhttpd.ep -- httpd
	RETVAL=$?
	return $RETVAL
}

extendedstatus()
{
	if briefstatus >/dev/null; then
	    RETVAL=$?
	    $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '	
	else
	    RETVAL=$?
	    msg_not_running "httpd"
	    echo
	fi
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload|graceful)
		if [ -e /usr/lib/apache/mod_jserv.so ]; then
			restart
		else
			reload
		fi
		;;
	check|configtest)
		conftest
		exit $?
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	update|condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	extendedstatus)
		extendedstatus
		;;
	status)
		briefstatus
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|check|configtest|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
