#!/bin/sh
#
# Startup script for the Apache Web Server
#
# chkconfig: 345 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#	       HTML files and CGI.
# processname: httpd-admin
# pidfile: /var/run/httpd-admin.pid
# config: /etc/httpd/conf/access.conf
# config: /etc/httpd/conf/httpd.conf
# config: /etc/httpd/conf/srm.conf

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

# Path to the httpd-admin binary.
httpd="/usr/sbin/httpd-admin -f /etc/httpd/admin-conf/httpd.conf"
RETVAL=0

# Until glibc's locale support is working right again, work around it.
LANG=C

# 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} | awk '{\
				gsub(".*/","");\
				gsub("^mod_","");\
				gsub("^lib","");\
				gsub("\.so$","");\
				print toupper($0)}'`
			moduleargs="${moduleargs} -D HAVE_$module"
		fi
	done
	echo ${moduleargs}
}
start() {
	if [ ! -f /usr/sbin/httpd-admin ]; then
		if [ -e /usr/sbin/httpd ]; then
			ln -s /usr/sbin/httpd /usr/sbin/httpd-admin
		fi
	fi
	echo -n "Starting httpd-admin: "
	daemon ${httpd} `moduleargs`
	RETVAL=$?
	echo
	# Wait for pid file to appear - it can take some time
	[ -f /var/run/httpd-admin.pid ] || sleep 2
	[ -f /var/run/httpd-admin.pid ] || sleep 2
	[ -f /var/run/httpd-admin.pid ] || sleep 2
	[ -f /var/run/httpd-admin.pid ] || sleep 2
	[ -f /var/run/httpd-admin.pid ] || RETVAL=1
	return $RETVAL
}
stop() {
	echo -n "Shutting down http-admin: "
	killproc httpd-admin
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f /var/run/httpd-admin.pid
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status httpd-admin
	;;
  restart)
	stop
	start
	;;
  reload)
	echo -n "Reloading httpd-admin: "
	killproc httpd-admin -HUP
	RETVAL=$?
	echo
	;;
  graceful)
	echo -n "Gracefully reloading e-smith httpd-admin: "
	killproc httpd-admin -USR1
	echo
	;;
  condrestart)
	if [ -f /var/run/httpd-admin.pid ] ; then
		stop
		start
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|graceful|condrestart|status}"
	exit 1
esac

exit $RETVAL
