#!/bin/bash
#
# Apache control script designed to allow an easy COMMAND line interface
# to controlling Apache.  Written by Marc Slemko, 1997/08/23
# 
# The exit codes returned are:
#	0 - operation completed successfully
#	1 - 
#	2 - usage error
#	3 - httpd could not be started
#	4 - httpd could not be stopped
#	5 - httpd could not be started during a restart
#	6 - httpd could not be restarted during a restart
#	7 - httpd could not be restarted during a graceful restart
#	8 - configuration syntax error
#
# When multiple arguments are given, only the error from the _last_
# one is reported.  Run "apachectl help" for usage info
#
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
# 
# the path to your PID file
PIDFILE=/usr/local/apache/logs/httpd.pid
#
# the path to your httpd binary, including options if necessary
HTTPD=/usr/local/apache/bin/httpd
#
# a COMMAND that outputs a formatted text version of the HTML at the
# url given on the COMMAND line.  Designed for lynx, however other
# programs may work.  
LYNX="lynx -dump"
#
# the URL to your server's mod_status status page.  If you do not
# have one, then status and fullstatus will not work.
STATUSURL="http://localhost/server-status"
#
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

SERVICE="apache"
OSNAME=`uname | awk -F_ '{printf $1}'`
RESULT=0
PID=""
STATUS=""
COMMAND="$1"

function print_help {
	echo "Usage: $0 [COMMAND]"
	echo
	echo commands:
	printf -- " start      - start httpd\n"
	printf -- " startssl   - start httpd with SSL enabled\n"
	printf -- " stop       - stop httpd\n"
	printf -- " restart    - restart httpd if running by sending a SIGHUP or start if \n"
	printf -- "              not running\n"
	printf -- " fullstatus - dump a full status screen; requires lynx and mod_status enabled\n"
	printf -- " status     - dump a short status screen; requires lynx and mod_status enabled\n"
	printf -- " graceful   - do a graceful restart by sending a SIGUSR1 or start if not running\n"
	printf -- " configtest - do a configuration syntax test\n"
	printf -- " help       - this screen\n"
}
function is_running {
	PID=`cat $PIDFILE 2>/dev/null`
	result=1
	if ! [ $PID ]; then
		STATUS="httpd (no pid file) not running"
	elif kill -0 $PID 2>/dev/null; then
		STATUS="httpd (pid $PID) running"
		result=0
	else
		STATUS="httpd (pid $PID?) not running"
	fi
	return $result
}
function start_cygwin_service {
	if /usr/bin/cygrunsrv -Q "cyg_$SERVICE" >/dev/null 2>&1; then
		rm -f /var/log/cyg_$SERVICE.log
		/usr/bin/cygrunsrv -S cyg_$SERVICE 2>/dev/null
		result=$?
		cat /var/log/cyg_$SERVICE.log 2>/dev/null
		return $result
	else
		$HTTPD
	fi
}
function start_service {
	if [ $OSNAME == CYGWIN ]; then
		start_cygwin_service
	else
		$HTTPD
	fi
}
function print_status {
	echo "$0 $COMMAND: $1"
}
function restart_service {
	signal=$1
	if ! is_running; then
		print_status "httpd not running, trying to start"
		start_service
	elif $HTTPD -t >/dev/null 2>&1; then
		kill -$signal $PID
	else
		print_status "configuration broken, ignoring restart"
		print_status "(run '$SERVICE configtest' for details)"
		return 1
	fi
	if [ $? -eq 0 ]; then
		print_status "httpd started"
	else
		print_status "httpd could not be started"
	fi
}
function install_cygwin_service {
	if /usr/bin/win-svc -i "$HTTPD -F" $SERVICE >/dev/null; then
		STATUS="$SERVICE service installed"
	fi
}
function install_service {
	if [ -f /etc/init.d/$SERVICE ]; then
		STATUS="$SERVICE service already installed"
	else
		cp $0 /etc/init.d/$SERVICE
		STATUS="$SERVICE service installed"
	fi
	if [ $OSNAME == CYGWIN ]; then
		install_cygwin_service
	fi
}

if ! [ "$COMMAND" ]; then
	echo "Usage: $0 {help|start|startssl|stop|restart|status|fullstatus|graceful|configtest|install}"
	exit 0
fi

case "$COMMAND" in
	help)
		print_help
		;;
	start)
		if is_running; then
			print_status "$STATUS"
		elif start_service; then
			print_status "httpd started"
		else
			print_status "httpd could not be started"
			RESULT=3
		fi
		;;
	startssl)
		if [ $OSNAME == CYGWIN ]; then
			print_status "Not implemented. Change conf/httpd.conf instead"
		elif is_running; then
			print_status "httpd (pid $PID) already running"
		elif $HTTPD -DSSL; then
			print_status "httpd started"
		else
			print_status "httpd could not be started"
			RESULT=1
		fi
		;;
	stop)
		if ! is_running; then
			print_status "$STATUS"
		elif kill $PID; then
			print_status "httpd stopped"
		else
			print_status "httpd could not be stopped"
			RESULT=4
		fi
		;;
	restart)
		restart_service HUP
		;;
	status)
		if ! is_running; then
			print_status "$STATUS"
			RESULT=1
		else
			$LYNX $STATUSURL | awk '/process$/ { print; exit } { print }'
		fi
		;;
	fullstatus)
		if ! is_running; then
			print_status "$STATUS"
			RESULT=1
		else
			$LYNX $STATUSURL
		fi
		;;
	graceful)
		restart_service USR1
		;;
	configtest)
		$HTTPD -t
		RESULT=$?
		;;
	install)
		install_service
		print_status "$STATUS"
		;;
	*)
		print_status "No such command"
		RESULT=1
		;;
esac

exit $RESULT

