#!/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
#
# Updated to MLS-compatible version by OP3P Team, gost@ivk.ru, 2024
# 
# 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'
#
# 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

# 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-${MLS}/httpd-mls-${MLS}.pid
LOCKFILE=/var/lock/subsys/httpd2-mls-$MLS
RETVAL=0
CONFFILE=/etc/httpd2/conf/httpd-mls-$MLS.conf
OPTIONS="-f $CONFFILE"

# 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

# 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()
{
	"$BINARY" -k start $OPTIONS
	RETVAL=$?
	return $RETVAL
}

stop()
{
	"$BINARY" -k stop $OPTIONS
	RETVAL=$?
	return $RETVAL
}

graceful_stop()
{
	if "$BINARY" -k graceful-stop $OPTIONS; 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

if [ -z "$MLS" ]; then
    echo "Value of MLS not defined."
    echo "For manual usage and set MLS to s2, You can do:"
    echo "export MLS=s2; $(basename $0)"
    exit 1
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)
    $BINARY -k $ARGV $OPTIONS $DEFINE
    ERROR=$?
    ;;
poststart)
    poststart
    ERROR=$?
    ;;
poststop)
    poststop
    ERROR=$?
    ;;
configtest)
    configtest
    ERROR=$?
    ;;
briefstatus)
    briefstatus
    ERROR=$?
    ;;
status)
    fullstatus | awk ' /process$/ { print; exit } { print } '
    ERROR=$?
    ;;
fullstatus)
    fullstatus
    ERROR=$?
    ;;
*)
    $BINARY $ARGV $OPTIONS $DEFINE
    ERROR=$?
esac

exit $ERROR

