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

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions
SourceIfExists  /etc/sysconfig/httpd2

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

# Set HTTPD=httpd2.worker in /etc/sysconfig/httpd2 to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
name=${HTTPD-httpd2}
BINARY=/usr/sbin/$name
moduledir=/usr/lib/apache2/modules
moduleargs=
PIDFILE=/var/run/httpd2/httpd.pid
PIDFILE_A1=/var/run/httpd.pid
PIDFILE_A1_PERL=/var/run/httpd-perl.pid
LOCKFILE=/var/lock/subsys/httpd2
RETVAL=0

export TMPDIR=/var/spool/apache2/tmp

### Hack for bad hostname configuration
thehost=`hostname`
hostname -i 1>/dev/null 2>/dev/null|| echo "WARNING: hostname is not set correctly, thus Apache may not run correctly either." 

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

### Hacks for the apache1 + proxified apache2
if [ -e "$PIDFILE_A1" -o -e "$PIDFILE_A1_PERL" ]; then
    DEFINE="-DA1PROXIED"
fi

# Change the major functions into functions.
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 $name: " \
		"$BINARY" -t `moduleargs` $OPTIONS $DEFINE
	RETVAL=$?
	return $RETVAL
}

start()
{
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name $name -- $BINARY $OPTIONS $DEFINE
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root --name $name -- $BINARY
	RETVAL=$?
	return $RETVAL
}

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

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

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

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

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload|graceful)
		if [ -e $moduledir/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
