#!/bin/sh
#
# apache	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
# pidfile:	/var/run/httpd.pid
# config:	/etc/httpd/apache.conf

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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/httpd ] && . /etc/sysconfig/httpd

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "Apache 2.4 Web Server"
		exit 1
	fi
else
	exit 0
fi

SVC_NAME="Apache 2.4 Web Server"

if [ -n "${HTTPD_CONF}" ]; then
	if [ -d "${HTTPD_CONF}" ] || [ -f "${HTTPD_CONF}" ]; then
		CFG="-f ${HTTPD_CONF}"
	else
		echo "error: HTTPD_CONF='$HTTPD_CONF': not a file, not a directory"
		exit 1
	fi
fi

# configtest itself
configtest() {
	/usr/sbin/httpd -t $CFG $HTTPD_OPTS 2>&1
	return $?
}

# wrapper for configtest:
checkconfig() {
	local details=${1:-0}

	if [ $details -eq 1 ]; then
		# run config test and display report (status action)
		show "Checking %s configuration" "$SVC_NAME"; busy
		local out
		out=`configtest 2>&1`
		RETVAL=$?
		[ $RETVAL -eq 0 ] && ok || fail
		[ "$out" ] && echo >&2 "$out"
	else
		# run config test and abort with nice message if failed
		# (for actions checking status before action).
		show "Checking %s configuration" "$SVC_NAME"; busy
		configtest >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			ok
		else
			fail
			nls 'Configuration test failed. See details with %s "checkconfig"' $0
			exit $RETVAL
		fi
	fi
}

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/httpd ]; then
		msg_already_running "$SVC_NAME"
		return
	fi

	[ "$1" -eq 0 ] || checkconfig
	msg_starting "$SVC_NAME"
	# remove ssl_scache on startup, otherwise httpd may go into
	# infinite loop if there are db transaction logs laying around
	rm -f /var/cache/httpd/*ssl_scache*
	daemon --pidfile /var/run/httpd.pid /usr/sbin/httpd $CFG $HTTPD_OPTS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/httpd
}

stop() {
	# Stop daemons.
	if [ ! -f /var/lock/subsys/httpd ]; then
		msg_not_running "$SVC_NAME"
		return
	fi

	msg_stopping "$SVC_NAME"
	killproc --pidfile /var/run/httpd.pid httpd
	rm -f /var/lock/subsys/httpd /var/run/httpd.pid /var/run/httpd.loc* >/dev/null 2>&1
}


reload() {
	if [ ! -f /var/lock/subsys/httpd ]; then
		msg_not_running "$SVC_NAME"
		RETVAL=7
		return
	fi

	checkconfig
	msg_reloading "$SVC_NAME"
	busy
	/usr/sbin/httpd $CFG $HTTPD_OPTS -k graceful
	RETVAL=$?
	[ $RETVAL -eq 0 ] && ok || fail
}

condrestart() {
	if [ ! -f /var/lock/subsys/httpd ]; then
		msg_not_running "$SVC_NAME"
		RETVAL=$1
		return
	fi

	checkconfig
	stop
	[ "$RESTART_DELAY" ] && usleep $RESTART_DELAY
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	checkconfig
	stop
	[ "$RESTART_DELAY" ] && usleep $RESTART_DELAY
	start 0
	;;
  try-restart)
	condrestart 0
	;;
  reload|force-reload|graceful|flush-logs)
	reload
	;;
  checkconfig|configtest)
	checkconfig 1
	;;
  status)
	status httpd
	RETVAL=$?
	/usr/sbin/httpd $CFG $HTTPD_OPTS -S
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|reload|force-reload|graceful|configtest|status}"
	exit 3
	;;
esac

exit $RETVAL
