#!/bin/sh

APACHE2="/usr/sbin/apache2"
APACHE2_OPTS=""
APACHE_RC_CONF="/etc/conf.d/apache2"
# List of init script verbs that should be passed forward
RC_VERBS="start stop restart checkconfd configtest modules virtualhosts configdump fullstatus graceful gracefulstop reload"


is_systemd() {
        [ $(ps -o comm= 1) = "systemd" ] && return 0
        return 1
}

load_rc_config() {
        [ -f "${APACHE_RC_CONF}" ] || return 1
        if ! grep -q '^[[:space:]]*APACHE2_OPTS' ${APACHE_RC_CONF} ; then
                echo "Cannot find APACHE2_OPTS in ${APACHE_RC_CONF}"
                exit 1
        fi
        . ${APACHE_RC_CONF}
        export APACHE2_OPTS
        export SERVERROOT="${SERVERROOT:-/usr/lib/apache2}"
        export CONFIGFILE="${CONFIGFILE:-/etc/apache2/httpd.conf}"
}

# Basically the code from '/etc/init.d/apache2::reload()', updated to run without open-rc
reload() {
        RELOAD_TYPE="${RELOAD_TYPE:-graceful}"

        if [ "${RELOAD_TYPE}" = "restart" ]; then
                ${APACHE2} ${APACHE2_OPTS} -k restart
        elif [ "${RELOAD_TYPE}" = "graceful" ]; then
                ${APACHE2} ${APACHE2_OPTS} -k graceful
        else
                echo "${RELOAD_TYPE} is not a valid RELOAD_TYPE. Please edit ${APACHE_RC_CONF}"
        fi
}

# Basically the code from '/etc/init.d/apache2::fullstatus()', updated to run without open-rc
fullstatus() {
        LYNX="${LYNX:-lynx -dump}"
        STATUSURL="${STATUSURL:-http://localhost/server-status}"

        if ! command -v $(set -- ${LYNX}; echo $1) 2>&1 >/dev/null; then
                echo 'lynx not found! you need to emerge www-client/lynx'
        else
                ${LYNX} ${STATUSURL}
        fi
        return $?
}

# Basically the code from '/etc/init.d/apache2::checkconfd()', updated to run without open-rc
checkconfd() {
        if [ ! -d "${SERVERROOT}" ]; then
                echo "SERVERROOT does not exist: ${SERVERROOT}"
                return 1
        fi
}

# Basically the code from '/etc/init.d/apache2::checkconfig()', updated to run without open-rc
configtest() {
        checkconfd || return 1

        local ret
        OUTPUT="$(${APACHE2} ${APACHE2_OPTS} -t 2>&1)"
        ret=$?
        if [ ${ret} -ne 0 ]; then
                echo "apache2 has detected an error in your setup:"
                printf "%s\n" "${OUTPUT}"
        fi

        return ${ret}
}

# Basically the code from '/etc/init.d/apache2::configdump()', updated to run without open-rc
configdump() {
        INFOURL="${INFOURL:-http://localhost/server-info}"

        if ! command -v $(set -- ${LYNX}; echo ${1}) 2>&1 >/dev/null; then
                echo "lynx not found! you need to emerge www-client/lynx"
        else
                echo "${APACHE2} started with '${APACHE2_OPTS}'"
                local i
                for i in config server list; do
                        ${LYNX} "${INFOURL}/?${i}" | sed '/Apache Server Information/d;/^[[:space:]]\+[_]\+$/Q'
                done
        fi
}


if ! is_systemd; then
	# If systemd IS NOT detected, run the legacy apache2ctl code

	# If first parameter is a verb defined in $RC_VERBS, pass the command to init script.
	# In other cases, compile command line and run the command on apache binary.
	if echo "${RC_VERBS}" | grep -q -- "${1}" ; then
		exec /etc/init.d/apache2 "${@}"
	else
		load_rc_config || exit 1
		${APACHE2} ${APACHE2_OPTS} -d ${SERVERROOT} -f ${CONFIGFILE} "${@}"
	fi
else
	# If systemd IS detected, load the config and parse the argument

	# Yes, we load config from apache's openrc conf.d file.
	# Feel free to provide a more suitable solution.
	load_rc_config || exit 1

	# Append the server root and configuration file parameters to the
	# user's APACHE2_OPTS.
	APACHE2_OPTS="${APACHE2_OPTS} -d ${SERVERROOT}"
	APACHE2_OPTS="${APACHE2_OPTS} -f ${CONFIGFILE}"

	apache_service="apache2.service"

	case ${1} in
		# Original apachectl options
		# See: https://httpd.apache.org/docs/2.4/programs/apachectl.html
		start|stop|restart|status)
			systemctl ${1} ${apache_service}
			retval=$?
		;;
		reload)
			reload
			retval=$?
		;;
		fullstatus)
			fullstatus
			retval=$?
		;;
		graceful)
			configtest || exit 1
			systemctl reload ${apache_service}
			retval=$?
		;;
		gracefulstop|graceful-stop)
			configtest || exit 1
			systemctl stop ${apache_service}
			retval=$?
		;;
		configtest)
			configtest
			retval=$?
		;;
		checkconfd)
			checkconfd
			retval=$?
		;;
		configdump)
			configtest || exit 1
			configdump
			retval=$?
		;;
		virtualhosts)
			configtest || exit 1
			${APACHE2} ${APACHE2_OPTS} -S
			retval=$?
		;;
		modules)
			configtest || exit 1
			${APACHE2} ${APACHE2_OPTS} -M 2>&1
			retval=$?
		;;
		# For all other options fall back to the legacy way of handling them
		*)
			${APACHE2} ${APACHE2_OPTS} "${@}"
			retval=$?
		;;
	esac
	exit ${retval}
fi
