# apache2-maintscript-helper - Apache2 helper function for maintainer scripts
# Copyright (C) 2012 Arno Töll <debian@toell.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

if [ -n "${EXPORT_APACHE2_MAINTSCRIPT_HELPER:-}" ] ; then
	return
fi
EXPORT_APACHE2_MAINTSCRIPT_HELPER=1

#
# Function apache2_has_module
# 	checks whether a supplied module is enabled in the current Apache server
# 	configuration
# Parameters:
#	module - the module name which should be checked. Can be a regular
#		string or a Perl compatible regular expression e.g. cgi(d|)
# Returns:
#	0 if the module(s) was/were found
#	1 otherwise
# Since: 2.4.1-1
apache2_has_module()
{
	[ -x /usr/sbin/a2query ] || return 1
	local MODULE="$1"
	if a2query -m $MODULE > /dev/null ; then
		return 0
	fi

	return 1
}

#
# Function apache2_switch_mpm
#	switches the MPM enabled on the web server. This function switches the
#	MPM unconditionally but does careful checks to make sure the web server
#	is left back with a working MPM.
#	It checks whether the supplied MPM exists and enables it on purpose.
# Parameters:
#	mpm - change the MPM to the supplied argument. It should be given
#	without "mpm_" prefix, e.g. "worker", "prefork", and so on.
# Returns:
#	0 if the MPM could be changed
#	1 otherwise
# Since: 2.4.1-1
apache2_switch_mpm()
{
	[ -x /usr/sbin/a2query ] || return 1
	[ -x /usr/sbin/a2dismod ] || return 1
	[ -x /usr/sbin/a2enmod ] || return 1

	local MPM="$1"
	MPM=$(echo "$MPM" | sed -e 's/^mpm_//')

	if [ ! -e "/etc/apache2/mods-available/mpm_$MPM.load" ] ; then
		return 1
	fi

	local CUR_MPM=$(a2query -M) || return 1

	if [ $CUR_MPM != $MPM ] ; then
		a2dismod -q "mpm_$CUR_MPM";
		a2enmod -q "mpm_$MPM";
	fi

	if ! apache2_has_module "mpm_$MPM" ; then
		# rollback
		a2enmod -q "mpm_$CUR_MPM"
		return 1
	fi

}

#
# Function apache2_invoke
#	invokes an Apache 2 configuration helper to enable or disable a
#	particular piece of configuration, a site or a module. It carefully
#	checks whether the supplied configuration snippet exists and reloads the
#	web server if the site administrator desires that by calling the
#	apache2_reload function.
# Parameters:
#	command - The command to invoke. Recognized commands are "enconf",
#		"enmod", "ensite", "disconf", "dismod", "dissite"
#	arguments - A variable number or arguments (e.g. modules) which shall be
#		enabled or disabled respectively. Do not enable module
#		dependencies that way, instead use module dependencies as
#		documented in </usr/share/doc/apache2/PACKAGING>.
# Returns:
#	0 if the changes could be activated
#	1 otherwise
# Since: 2.4.1-1
apache2_invoke()
{
	local CMD=$1
	shift
	local invoke_rcd=0
	local check_switch=""

	[ -x "/usr/sbin/a2$CMD" ] || return 1
	[ -x "/usr/sbin/a2query" ] || return 1

	case "$CMD" in
		*conf)
			check_switch="-c"
			;;
		*mod)
			check_switch="-m"
			;;
		*site)
			check_switch="-s"
			;;
		*)
			;;
	esac

	for CONF in $@ ; do
		case "$CMD" in
			enconf|enmod|ensite)
				if a2query $check_switch $CONF > /dev/null 2>&1 ; then
					continue
				fi
				invoke_rcd=1
				a2$CMD -q "$CONF" || return 1
				;;
			disconf|dismod|dissite)
				if ! a2query $check_switch $CONF > /dev/null 2>&1 ; then
					continue
				fi
				invoke_rcd=1
				a2$CMD -q "$CONF" || return 1
				;;
			*)
				return 1
				;;
		esac
	done

	if [ $invoke_rcd -eq 1 ] ; then
		apache2_reload
	fi

}

#
# Function apache2_reload
#	reloads the web server to activate a changed configuration. It does not
#	actually reload the web server if the current configuration fails to
#	parse.
# Parameters:
#	This function does not take any arguments
# Returns:
#	0 if the changes could be activated
#	1 otherwise
# Since: 2.4.1-1
apache2_reload()
{
	if apache2ctl configtest 2>/dev/null; then
		invoke-rc.d apache2 force-reload || true
	else
		echo "Your configuration is broken. Not restarting Apache 2."
	fi
}
