#! /bin/sh
#
# httpd          Start/Stop the Apache Web Server
#
# chkconfig: - 81 14
# 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
LOCKFILE=/var/lock/subsys/httpd
PERLBINARY=/usr/sbin/httpd-perl
PERLLOCK=/var/run/httpd-perl.pid
RETVAL=0

export TMPDIR=/var/spool/apache/tmp

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

# mod_perl related setup; see README.ALT
if [ -x "$PERLBINARY" -a -e "$PERLLOCK" ]; then
	ARGS="-DPERLPROXIED"
else
	ARGS=""
fi

SourceIfExists /etc/sysconfig/apache

# 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() {
	local ssl_create=/usr/sbin/mod_ssl-generate-ssl-certificate
	if [ -x "$ssl_create" ]; then
		"$ssl_create"
	fi
	# TODO: translatable form?
	action "Checking configuration sanity for httpd: " \
		"$BINARY" -t `moduleargs` $ARGS
	RETVAL=$?
	return $RETVAL
}

start()
{
	local ssl_create=/usr/sbin/mod_ssl-generate-ssl-certificate
	if [ -x "$ssl_create" ]; then
		"$ssl_create"
	fi
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name libhttpd.ep -- httpd $ARGS
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name libhttpd.ep -- httpd
	RETVAL=$?
	killall -9q libhttpd.ep	2>/dev/null # due to supposedly apache bug
	return $RETVAL
}

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

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

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)
		conftest || exit $?
		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
