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

WITHOUT_RC_COMPAT=1

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

BINARY=/usr/sbin/httpd-perl
APACHEBINARY=/usr/sbin/httpd
#PIDFILE=/var/run/httpd.pid
PERLPIDFILE=/var/run/httpd-perl.pid
LOCKFILE=/var/lock/subsys/httpd-perl
#APACHELOCK=/var/lock/subsys/httpd
RETVAL=0
PERLFILE="-f /etc/httpd/conf/httpd-perl.conf"

export TMPDIR=/var/spool/apache/tmp

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

### Hacks for the apache + proxified apache-mod_perl
# if both packages are installed we consider apache-mod_perl
# as running proxied by apache; if it's not the case then
# either remove apache package or chmod -x /usr/sbin/httpd
# and file a bug with an explanation why it's wrong
if [ -x "$BINARY" -a -x "$APACHEBINARY" ]; then 
	PERLARGS="-DPERLPROXIED"
else
	PERLARGS=""
fi 

SourceIfExists /etc/sysconfig/apache

# Change the major functions into functions.
moduleargs() {
	moduledir=/usr/lib/apache-perl
	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-perl: " \
		"$BINARY" -t $PERLFILE $PERLARGS
	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 "$PERLPIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root -- httpd-perl $PERLFILE $PERLARGS
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PERLPIDFILE" --lockfile "$LOCKFILE" \
		--expect-user root -- httpd-perl
	RETVAL=$?
	return $RETVAL
}

reload()
{
	msg_reloading httpd-perl
	stop_daemon --pidfile "$PERLPIDFILE" --expect-user root -HUP httpd-perl
	RETVAL=$?
	return $RETVAL
} 

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

briefstatus()
{
	status --pidfile "$PERLPIDFILE" --expect-user root \
		--expect-user root -- httpd-perl
	RETVAL=$?
	return $RETVAL
}

extendedstatus()
{
	if briefstatus >/dev/null; then
	    RETVAL=$?
	    if [ -z "$ARGS" ]; then
		$LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '	
	    else
		$LYNX $STATUSURLPERL | awk ' /process$/ { print; exit } { print } '
		echo "** apache-mod_perl is running in PERLPROXIED mode"
	    fi
	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)
		reload
		;;
	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
