#!/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# 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:
#   XXX this doc is no longer correct now that the interesting
#   XXX functions are handled by httpd
#	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
#
ACMD="$1"
ARGV="$@"
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
# 
# the path to your httpd binary, including options if necessary
HTTPD='/usr/sbin/httpd2'
#
# pick up any necessary environment variables
ENVVARS="/usr/sbin/envvars"
if test -f "$ENVVARS"; then
	. "$ENVVARS"
fi
#
# 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:80/server-status"
#
# Set this variable to a command that increases the maximum
# number of file descriptors allowed per child process. This is
# critical for configurations that use many file descriptors,
# such as mass vhosting, or a multithreaded server.
ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

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

# 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=`echo $HTTPD | sed -r '\@^.*/[^/[:space:]]+[[:space:]]*$@s@^.*/([^/[:space:]]+)[[:space:]]*$@\1@'`
BINARY=/usr/sbin/$name
BASENAME="$(basename "$BINARY")"
moduledir=/usr/lib64/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

# For check vars
numcheck() {
	head -n 1 \
		| grep -q '^[[:space:]]*0*[1-9][0-9]*[[:space:]]*$'
}

# Check vars
if ! echo "$WAITSTOP" | numcheck ; then
	WAITSTOP=300
fi
if ! echo "$WAITGRACEFULSTOP" | numcheck ; then
	WAITGRACEFULSTOP=3000
fi
if ! echo "$USLEEPSTART" | numcheck ; then
	USLEEPSTART=300000
fi
if ! echo "$LOOPSSTART" | numcheck ; then
	LOOPSSTART=10
fi

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

# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
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}
}

configtest() {
	"$BINARY" -t `moduleargs` $OPTIONS $DEFINE
	RETVAL=$?
	return $RETVAL
}

start()
{
	initlog $INITLOG_ARGS -n "$BASENAME" -c "limited -n $BASENAME -- $BINARY -k start $OPTIONS $DEFINE" || :
	poststart
	RETVAL=$?
	return $RETVAL
}

stop()
{
	if initlog $INITLOG_ARGS -n "$BASENAME" -c "limited -n $BASENAME -- $BINARY -k stop $OPTIONS $DEFINE"; then
		usleep 100000
		poststop
		RETVAL=$?
	else
		RETVAL=$?
	fi
	return $RETVAL
}

graceful_stop()
{
	if  initlog $INITLOG_ARGS -n "$BASENAME" -c "limited -n $BASENAME -- $BINARY -k graceful-stop $OPTIONS $DEFINE"; then
		sleep 1
		poststop
		RETVAL=$?
	else
		RETVAL=$?
	fi
	return $RETVAL
}

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

poststart()
{
	for (( i=$LOOPSSTART; $i>0; i=(($i-1)) )); do
		usleep $USLEEPSTART
		if briefstatus >/dev/null 2>&1; then
			touch "$LOCKFILE"
			RETVAL=$?
			break
		else
			RETVAL=$?
		fi
	done
	return $RETVAL
}

poststop()
{
	if ! briefstatus >/dev/null 2>&1; then
		rm -f "$LOCKFILE"
		RETVAL=$?
	else
		RETVAL=255
		printf "Service %s has not stopped.\n" "$name"
	fi
	return $RETVAL
}

fullstatus()
{
	if briefstatus >/dev/null 2>&1; then
	    RETVAL=$?
	    $LYNX $STATUSURL
	else
	    RETVAL=$?
	    msg_not_running "$name"
	    echo
	fi
	return $RETVAL
}

stop_wait()
{
	for (( i=$1; $i>0; i=(($i-1)) )); do
		poststop >/dev/null 2>&1
		RETVAL=$?
		if [ $RETVAL -eq 255 ]; then
			sleep 1
		else
			return $RETVAL
		fi
	done
	poststop >/dev/null 2>&1
	RETVAL=$?
	return $RETVAL
}

ERROR=0
if [ "x$ARGV" = "x" ] ; then 
    ARGV="-h"
fi

case $ACMD in
start)
    start
    ERROR=$?
    ;;
stop)
    stop
    ERROR=$?
    ;;
graceful-stop)
    graceful_stop
    ERROR=$?
    ;;
wait-stop)
    stop
    ERROR=$?
    if [ $RETVAL -eq 255 ]; then
	    stop_wait $WAITSTOP
	    ERROR=$?
    fi
    ;;
wait-graceful-stop)
    graceful_stop
    ERROR=$?
    if [ $RETVAL -eq 255 ]; then
	    stop_wait $WAITGRACEFULSTOP
	    ERROR=$?
    fi
    ;;
restart|graceful)
    initlog $INITLOG_ARGS -n "$BASENAME" -c "limited -n $BASENAME -- $BINARY -k $ARGV $OPTIONS $DEFINE"
    ERROR=$?
    ;;
startssl|sslstart|start-SSL)
    echo The startssl option is no longer supported.
    echo Please edit httpd.conf to include the SSL configuration settings
    echo and then use "apachectl start".
    ERROR=2
    ;;
poststart)
    poststart
    ERROR=$?
    ;;
poststop)
    poststop
    ERROR=$?
    ;;
configtest)
    configtest
    ERROR=$?
    ;;
briefstatus)
    briefstatus
    ERROR=$?
    ;;
status)
    fullstatus | awk ' /process$/ { print; exit } { print } '
    ERROR=$?
    ;;
fullstatus)
    fullstatus
    ERROR=$?
    ;;
*)
    initlog -n "$BASENAME" -c "limited -n $BASENAME -- $BINARY $ARGV $OPTIONS $DEFINE"
    ERROR=$?
esac

exit $ERROR

