#!/bin/bash
#
# 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
#
# This script is modified to be used with Server Emulator plugin for JMeter.
# The modification lays id addition of few new case statements of ACMD switch
# (recognized by "netns" suffix) which allows to run apache2 directly in custom
# network namespace (uses newly defined PROCESS_NAME variable) and commenting
# out block which warns about additional command line arguments. If a
# different version of this script should be used, it must be modified in the
# same way 
# 
# The exit codes returned are:
#   XXX this doc is no longer correct now that the interesting
#   XXX functions are handled by apache2
#	0 - operation completed successfully
#	1 - 
#	2 - usage error
#	3 - apache2 could not be started
#	4 - apache2 could not be stopped
#	5 - apache2 could not be started during a restart
#	6 - apache2 could not be restarted during a restart
#	7 - apache2 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="$@"

# source apache variables when using custom namespace
source /etc/apache2/envvars
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
#
NAME_OF_SERVICE="apache2"

# the path to your apache2 binary, including options if necessary
APACHE2="/usr/sbin/${NAME_OF_SERVICE}"
#
# name of the server daemon process
PROCESS_NAME="${NAME_OF_SERVICE}"
#
# 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.  
if [ -x "/usr/bin/links" ]; then
  LYNX="/usr/bin/links -dump"
else
  LYNX=none
fi
#
# 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  ||||||||||||||||||||

# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
fi

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

checklynx() {
if [ "$LYNX" = "none" ]; then
   echo "The 'links' package is required for this functionality."
   exit 8
fi
}

testconfig() {
# httpd is denied terminal access in SELinux, so run in the
# current context to get stdout from $HTTPD -t.
if test -x /usr/sbin/selinuxenabled && /usr/sbin/selinuxenabled; then
  runcon -- `id -Z` /usr/sbin/$NAME_OF_SERVICE $OPTIONS -t
else
  /usr/sbin/$NAME_OF_SERVICE $OPTIONS -t
fi
ERROR=$?
}

#if [ "x$2" != "x" ] ; then
#    echo Passing arguments to httpd using apachectl is no longer supported.
#    echo You can only start/stop/restart httpd using this script.
#    echo If you want to pass extra arguments to httpd, edit the
#    echo /etc/sysconfig/httpd config file.
#fi

if [[ "$ACMD" == "start" && "$2" == "true" ]]; then
  sudo a2enmod ssl
  sudo a2ensite default-ssl.conf
  sudo a2dissite 000-default.conf
elif [[ "$ACMD" == "startnetns" && "$3" == "true" ]]; then
  sudo a2enmod ssl
  sudo a2ensite default-ssl.conf
  sudo a2dissite 000-default.conf
elif [[ "$ACMD" == "stop" && "$2" == "true" ]]; then
  sudo a2dismod ssl
  sudo a2ensite 000-default.conf
  sudo a2dissite default-ssl.conf
elif [[ "$ACMD" == "stopnetns" && "$3" == "true" ]]; then
  sudo a2dismod ssl
  sudo a2ensite 000-default.conf
  sudo a2dissite default-ssl.conf
fi

case $ACMD in
start|stop|restart|status)
    /usr/bin/systemctl $ACMD $NAME_OF_SERVICE.service
    ERROR=$?
    ;;
graceful)
    /usr/bin/systemctl reload $NAME_OF_SERVICE.service
    ERROR=$?
    ;;
graceful-stop)
    /usr/bin/systemctl stop $NAME_OF_SERVICE.service
    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
    ;;
configtest)
    testconfig
    ;;
fullstatus)
    checklynx
    $LYNX $STATUSURL
    ;;
startnetns)
    ip netns exec $2 $APACHE2 $OPTIONS -k start
    ERROR=$?
    ;;
stopnetns)
    ip netns exec $2 $APACHE2 $OPTIONS -k stop
    ERROR=$?
    ;;
statusnetns)
    # Ouput of the following echo commands is processed programatically by Server Emulator to find out if the server is running
    echo $(pgrep $PROCESS_NAME)
    ERROR=$?
    ;;
termnetns)
    killall $PROCESS_NAME
    if [ $? -ne 0 ]; then
	killall -s KILL $PROCESS_NAME
    fi
    ERROR=$?
    ;;
*)
    /usr/sbin/$NAME_OF_SERVICE $OPTIONS "$@"
    ERROR=$?
esac

exit $ERROR

