#!/bin/sh -efu

SYSCONFDIR="${SYSCONFDIR:-/etc}"
APACHE2DIR="${APACHE2DIR:-$SYSCONFDIR/httpd2}"
APACHE2_MODS_ENABLED="${APACHE2_MODS_ENABLED:-$APACHE2DIR/conf/mods-enabled}"
PHPBASEDIR="${PHPBASEDIR:-$SYSCONFDIR/php}"

. shell-error
. shell-config
. alterator-service-functions

# Returns the version the PHP module enabled in the current Apache2
# configuration.
get_apache2_mod_php_version() {
	local n=0
	ls -1 "$APACHE2_MODS_ENABLED" \
	| grep "^mod_php[0-9]\+.load" \
	| while read m; do
		n=$(( n + 1 ))
		[ $n -eq 1 ] || fatal "Can't determine the PHP version -- more than one PHP module enabled in Apache2 configuration"
		local mf="$(cat "$APACHE2_MODS_ENABLED/$m" | sed -n -e '/^LoadModule/ s|LoadModule[[:space:]]\+php[0-9]\+_module[[:space:]]\(.*\.so\).*$|\1|p; q')"
		local p="$(rpm -qf "$mf")"
		[ -n "$p" ] || fatal "Package with file $mf not found"
		rpm -q --requires "$p" | sed -n -e '/^php5[[:space:]]*=/ s/^php5[[:space:]]*=[[:space:]]*\([0-9]\+\.[0-9]\+\.[0-9]*\).*$/\1/p; q'
	done
}

# Returns the name of the configuration file for a PHP module enabled
# in the current Apache2 configuration.
get_apache2_mod_php_ini() {
	local v="$(get_apache2_mod_php_version)"
	[ -n "$v" ] || fatal "Unable to get Apache2 PHP module version"
	if [ -d "$PHPBASEDIR/$v" ]; then
		echo "$PHPBASEDIR/$v/apache2-mod_php/php.ini"
	elif [ -d "$PHPBASEDIR/${v%.*}" ]; then
		echo "$PHPBASEDIR/${v%.*}/apache2-mod_php/php.ini"
	fi
}

# Reads a named value from a named PHP configuration file.
# args: filename val-name
read_php_ini() {
	local __shell_config_comment=';'
	shell_config_get "$1" "$2" '[[:space:]]\+=[[:space:]]'
}

# Reads a named value from the configuration file for a PHP module
# enabled in the current Apache2 configuration.
# args: val-name
read_apache2_mod_php_ini() {
	local phpini="$(get_apache2_mod_php_ini)"
	[ -n "$phpini" ] || fatal "Unable to get the name of the Apache2 PHP module configuration file"
	read_php_ini "$phpini" "$1"
}

# Writes a named value to a named PHP configuration file.
# args: filename val-name val
write_php_ini() {
	local __shell_config_comment=';'
	shell_config_set "$1" "$2" "$3" '[[:space:]]\+=[[:space:]]' ' = '
}

# Writes a named value to the configuration file for a PHP module
# enabled in the current Apache2 configuration.
# args: val-name val
write_apache2_mod_php_ini() {
	local phpini="$(get_apache2_mod_php_ini)"
	[ -n "$phpini" ] || fatal "Unable to get the name of the Apache2 PHP module configuration file"
	write_php_ini "$phpini" "$1" "$2"
}

# Comments a named value in a named PHP configuration file.
# args: filename val-name val
comment_php_ini() {
	local __shell_config_comment=';'
	shell_config_comment "$1" "$2" '[[:space:]]\+=[[:space:]]'
}

# Comments a named value in the configuration file for a PHP module
# enabled in the current Apache2 configuration.
# args: val-name val
comment_apache2_mod_php_ini() {
	local phpini="$(get_apache2_mod_php_ini)"
	[ -n "$phpini" ] || fatal "Unable to get the name of the Apache2 PHP module configuration file"
	comment_php_ini "$phpini" "$1" "$2"
}

# Applies the changes for Apache2.
apache2_mod_php_apply() {
	service_control 'httpd2' reload
}

