module: add apache
diff --git a/MANIFEST b/MANIFEST
index 7d93cc6..2cb4b02 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -33,3 +33,12 @@
 #file=
 #method={git-archive|git-clone|wget-file}
 # @@MIRROR_ENDED@@
+
+# @@MIRROR_BEGIN@@
+modname=apache
+mirror=$mod_authnz_external_mirror
+protocol=https
+branch=$mod_authnz_external_branch
+host=github.com/phokz
+method=git-clone
+# @@MIRROR_ENDED@@
diff --git a/module/apache/holder.sh b/module/apache/holder.sh
new file mode 100644
index 0000000..4c4f77f
--- /dev/null
+++ b/module/apache/holder.sh
@@ -0,0 +1,98 @@
+#!/bin/bash
+#
+# apache placeholders
+#
+# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
+
+# 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 (including the next
+# paragraph) 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
+# ITS SUPPLIERS 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.
+
+_APACHE_DKRC_SERVICE_="${module}"
+_APACHE_DKRC_IMAGE_="${host_name}-img-${_APACHE_DKRC_SERVICE_}"
+_APACHE_DKRC_CONTEXT_="${mod_docker_d}"
+_APACHE_DKRC_CONTAINER_="${host_name}-con-${_APACHE_DKRC_SERVICE_}"
+_APACHE_DKRC_FRONTEND_IP_="172.28.0.2"
+_APACHE_DKRC_BACKEND_IP_="172.27.0.3"
+_APACHE_DKRC_PORTS_F_="${ports_f}"
+_APACHE_DKRC_CONF_F_="${apache2_f}"
+_APACHE_DKRC_AVAILABLE_D_="${available_d}"
+_APACHE_DKRC_ENABLED_D_="${enabled_d}"
+_APACHE_DKRC_CERT_L_="${certificates_l}"
+_APACHE_DKRC_LOG_D_="${log_d}"
+_APACHE_DKRC_WWW_D_="${www_d}"
+if [ "$apache_has_media" -eq 1 ]; then
+	_APACHE_HAS_MEDIA_="1"
+	_APACHE_HTML_PHOTOS_D_="${photos_d}"
+	_APACHE_HTML_VIDEOS_D_="${videos_d}"
+else
+	_APACHE_HAS_MEDIA_="0"
+	_APACHE_HTML_PHOTOS_D_=""
+	_APACHE_HTML_VIDEOS_D_=""
+fi
+_APACHE_DKRC_ROOTFS_="${mod_rootfs_d}"
+_APACHE_DKRC_DOCKERFILE_="Dockerfile"
+_APACHE_VHOST_HTTP_LOG_F_="/var/log/apache2/vhost-http-log.txt"
+_APACHE_SSL_LOG_F_="/var/log/apache2/vhost-https-log.txt"
+_APACHE_SSL_LOG_LEVEL_REL_="info ssl:info"
+_APACHE_SSL_LOG_LEVEL_DBG_="trace8 ssl:trace8"
+_APACHE_CON_SSL_D_="/etc/letsencrypt"
+if [ "$apache_has_dummy_certs" -eq 1 ]; then
+	_APACHE_CON_SSL_CONF_F_="${_APACHE_CON_SSL_D_}/${host_name}.conf"
+	_APACHE_CON_SSL_CERT_F_="${_APACHE_CON_SSL_D_}/${host_name}.crt"
+	_APACHE_CON_SSL_KEY_F_="${_APACHE_CON_SSL_D_}/${host_name}.key"
+else
+	_APACHE_CON_SSL_CONF_F_="${_APACHE_CON_SSL_D_}/options-ssl-apache.conf"
+	_APACHE_CON_SSL_CERT_F_="${_APACHE_CON_SSL_D_}/live/${host_name}/fullchain.pem"
+	_APACHE_CON_SSL_KEY_F_="${_APACHE_CON_SSL_D_}/live/${host_name}/privkey.pem"
+fi
+_APACHE_VHOST_GERRIT_LOG_F_="/var/log/apache2/vhost-gerrit-log.txt"
+_APACHE_VHOST_GERRIT_LOG_LEVEL_REL_="info"
+_APACHE_VHOST_GERRIT_LOG_LEVEL_DBG_="trace8"
+_APACHE_DOCUMENT_ROOT_D_="/var/www/html"
+_APACHE_PRIVATE_PATH_="/private"
+_APACHE_HTTP_PORT_="80"
+_APACHE_HTTPS_PORT_="443"
+_APACHE_GERRIT_PROXY_PORT_="8081"
+_APACHE_SERVER_NAME_="${host_name}"
+_APACHE_SERVER_ALIAS_="www.${_APACHE_SERVER_NAME_}"
+_APACHE_CON_SSL_MYSQL_D_="/etc/mysql_ssl"
+_APACHE_CON_SSL_MYSQL_CERT_F_="${_APACHE_CON_SSL_MYSQL_D_}/"
+_APACHE_CON_SSL_MYSQL_CERT_F_+="$(basename "${_MYSQL_SSL_CLIENT_CERT_F_}")"
+_APACHE_CON_SSL_MYSQL_KEY_F_="${_APACHE_CON_SSL_MYSQL_D_}/"
+_APACHE_CON_SSL_MYSQL_KEY_F_+="$(basename "${_MYSQL_SSL_CLIENT_KEY_F_}")"
+_APACHE_CON_SSL_MYSQL_CA_F_="${_APACHE_CON_SSL_MYSQL_D_}/"
+_APACHE_CON_SSL_MYSQL_CA_F_+="$(basename "${_MYSQL_SSL_CLIENT_CA_F_}")"
+_APACHE_EXT_AUTH_F_="${http_authentication_bang_f}"
+_APACHE_EXT_AUTH_KEYWORD_="bash_auth"
+_APACHE_CON_EXT_AUTH_F_="/var/www/http_authentication"
+if [ "${mod_mode}" = "${release}" ]; then
+	_APACHE_SSL_LOG_LEVEL_="${_APACHE_SSL_LOG_LEVEL_REL_}"
+	_APACHE_VHOST_GERRIT_LOG_LEVEL_="${_APACHE_VHOST_GERRIT_LOG_LEVEL_REL_}"
+	_APACHE_MOD_AUTHNZ_DEBUG_FLAG_="/dev/null"
+	_APACHE_CODE_IGNITER_ENV_="production"
+else
+	_APACHE_SSL_LOG_LEVEL_="${_APACHE_SSL_LOG_LEVEL_DBG_}"
+	_APACHE_VHOST_GERRIT_LOG_LEVEL_="${_APACHE_VHOST_GERRIT_LOG_LEVEL_DBG_}"
+	_APACHE_MOD_AUTHNZ_DEBUG_FLAG_="/dev/stderr --verbose"
+	_APACHE_CODE_IGNITER_ENV_="development"
+fi
+_APACHE_MOD_AUTHNZ_EXTERNAL_D_="$(basename "${mod_authnz_external_d}")"
+_APACHE_MOD_AUTHNZ_SUCCESS_CODE_="204"
+_APACHE_MOD_AUTHNZ_H_ACCEPT_="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
+_APACHE_MOD_AUTHNZ_H_CONTENT_="Content-Type: application/x-www-form-urlencoded"
+_APACHE_MOD_AUTHNZ_LOCALHOST_="127.0.0.1"
diff --git a/module/apache/module.sh b/module/apache/module.sh
new file mode 100644
index 0000000..69161ac
--- /dev/null
+++ b/module/apache/module.sh
@@ -0,0 +1,485 @@
+#!/bin/bash
+#
+# module.sh - apache
+#
+# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
+
+# 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 (including the next
+# paragraph) 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
+# ITS SUPPLIERS 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.
+
+declare -r module="apache"
+source $common_sh
+
+module_enable $module
+declare -ar depmod=( mysql gerrit )
+
+declare -r etc_d="$mod_rootfs_d/etc"
+declare -r www_d="$mod_rootfs_d/var/www"
+declare -r log_d="$mod_rootfs_d/var/log/apache2"
+
+declare -r application_d="$www_d/application"
+declare -r html_d="$www_d/html"
+
+declare -r conf_d="$etc_d/apache2"
+declare -r available_d="$conf_d/sites-available"
+declare -r enabled_d="$conf_d/sites-enabled"
+declare -r views_d="$application_d/views"
+declare -r controllers_d="$application_d/controllers"
+
+# Docker context directory
+declare -r mod_authnz_external_mirror="mod-auth-external.git"
+declare -r mod_authnz_external_branch="mod_authnz_external-3.3.3"
+declare -r mod_authnz_external_d="${mod_docker_d}/mod_authnz_external"
+
+# Output files
+declare -r ports_f="$conf_d/ports.conf"
+declare -r apache2_f="$conf_d/apache2.conf"
+declare -r dockerfile_f="$mod_docker_d/Dockerfile"
+declare -r vhost_http_f="$available_d/vhost-http.conf"
+declare -r vhost_https_f="$available_d/vhost-https.conf"
+declare -r vhost_gerrit_f="$available_d/vhost-gerrit.conf"
+declare -r http_authentication_bang_f="${www_d}/http_authentication"
+
+# Staged resources
+declare -r backend_d="$mod_staging_d/code-igniter-v3-giggi.git"
+declare -r frontend_d="$mod_staging_d/pelican-subtle-giggi.git"
+declare -r content_d="$mod_staging_d/mdcon.git"
+declare -r pythonenv_d="$mod_staging_d/pelican-venv"
+
+declare -r pelican_version="4.0.1"
+declare -r markdown_version="3.1.1"
+declare -r setuptools_version="45"
+
+declare -ir apache_has_dummy_certs=1
+if [ $apache_has_dummy_certs -eq 1 ]; then
+	declare -r letsencrypt_d="$mod_staging_d/dummy_cert"
+else
+	declare -r letsencrypt_d="/etc/letsencrypt"
+fi
+declare -r certificates_l="$etc_d/certificates"
+
+declare -ar PELICAN_CLEAN_TARGETS=( pelican-all )
+declare -r apache_has_media=0
+if [ $apache_has_media -eq 1 ]; then
+	declare -r clean_skiptag_sh="clean_skiptag.sh"
+	declare -r skiptag="_.${host_name}."
+	declare -r media_d="$HOME/media"
+	declare -r photos_d="$media_d/photos"
+	declare -r videos_d="$media_d/videos"
+	declare -ar PELICAN_MAKE_TARGETS=( pelican-all )
+	declare -ar PELICAN_INSTALL_TARGETS=( pelican-all )
+else
+	declare -ar PELICAN_MAKE_TARGETS=( pelican-html )
+	declare -ar PELICAN_INSTALL_TARGETS=( pelican-html pelican-theme )
+fi
+declare -ar CODEIGN_INSTALL_TARGETS=( codeign-all )
+
+declare -ar mod_more_dirs=( \
+	$www_d $html_d $etc_d $conf_d $log_d $available_d $enabled_d \
+	$application_d $views_d $controllers_d $mod_authnz_external_d \
+)
+
+declare -ar mod_more_files=( \
+	$ports_f $apache2_f $vhost_http_f $vhost_https_f $vhost_gerrit_f \
+	$dockerfile_f $http_authentication_bang_f \
+)
+
+declare -ar mod_more_trefs=( \
+	ports_t apache2_t vhost_http_t vhost_https_t vhost_gerrit_t \
+	dockerfile_t http_authentication_bang_t \
+)
+
+function tod_watch
+{
+	__watch_module_common || return $s_err
+}
+
+function tod_doins
+{
+	__doins_module_common || return $s_err
+	__apache_do_sites     || return $s_err
+	__apache_do_links     || return $s_err
+}
+
+function tod_fetch
+{
+	__fetch_module_common || return $s_err
+}
+
+function tod_upins
+{
+	__upins_module_common || return $s_err
+}
+
+function tod_upmod
+{
+	__fetch_module_common   || return $s_err
+	__apache_do_ext_modules || return $s_err
+	__apache_do_dummy_cert  || return $s_err
+	__apache_do_backend     || return $s_err
+	__apache_do_frontend    || return $s_err
+}
+
+function tod_clmod
+{
+	__clmod_module_common || return $s_err
+}
+
+function tod_clins
+{
+	__clins_module_common || return $s_err
+}
+
+function tod_doall
+{
+	# upmod must come after
+	tod_doins && tod_upmod || return $s_err
+}
+
+function tod_upall
+{
+	# upmod must come after
+	tod_upins && tod_upmod || return $s_err
+}
+
+function __apache_do_ext_modules
+{
+	local origin_d="${mod_staging_d}/${mod_authnz_external_mirror}"
+	rm -rf ${mod_authnz_external_d}/* || return $s_err
+	cp -ar ${origin_d}/* ${mod_authnz_external_d}
+}
+
+function __apache_do_dummy_cert_helper
+{
+	local -r digital_sign="digitalSignature\nextendedKeyUsage=serverAuth"
+	local -r dn_dns="DNS:${host_name}\nkeyUsage=$digital_sign"
+	local -r dn_lhs="[dn]\nCN=${host_name}\n[req]\ndistinguished_name"
+	local -r dn_rhs="dn\n[EXT]\nsubjectAltName"
+
+	openssl req -x509 -out ${host_name}.crt -keyout ${host_name}.key \
+			-newkey rsa:2048 -nodes -sha256 \
+			-subj "/CN=${host_name}" -extensions EXT \
+			-config <(printf "$dn_lhs = $dn_rhs=$dn_dns") 2>&1 | lets -l -x "openssl"
+
+	return ${PIPESTATUS[0]}
+}
+
+function __apache_do_dummy_cert
+{
+	if [ $apache_has_dummy_certs -eq 1 ]; then
+		if [ ! -d $letsencrypt_d ]; then
+			mkdir -p $letsencrypt_d && \
+				pushd $letsencrypt_d >/dev/null || return $s_err
+			__apache_do_dummy_cert_helper && \
+				echo "# Dummy $host_name conf" > $host_name.conf && \
+				popd >/dev/null || {
+					lets -l -e "failed to make dummy certificates"
+					popd >/dev/null; return $s_err
+				}
+		fi
+	fi
+
+	lets -l -i "using $module SSL certificates from: $letsencrypt_d"
+	return $s_ok
+}
+
+function __apache_do_links
+{
+	__apache_do_dummy_cert || return $s_err
+
+	# NOTE: container path must match localhost path
+	#
+	# i.e.
+	#    localhost:/etc container:/etc                 OK
+	#    localhost:/home/luigi container:/home/???     NOT OK
+	#
+	ln -sf "${letsencrypt_d}" "${certificates_l}"
+}
+
+function __apache_do_sites
+{
+	if [ ! -d "$enabled_d" ]; then
+		lets -l -e "invalid: $enabled_d"
+		return 1
+	fi
+
+	# This list controls what site to enable
+	local -a sites=( $vhost_http_f $vhost_https_f $vhost_gerrit_f )
+
+	pushd $enabled_d &>/dev/null
+
+	local file
+	for file in ${sites[@]}; do
+		local link_name="$(basename "$file")"
+		file="../sites-available/$link_name"
+
+		if [ -f "$file" ]; then
+			ln -sf $file $link_name
+		else
+			lets -l -e "failed link $file"
+			popd &>/dev/null
+			return 1
+		fi
+	done
+
+	popd &>/dev/null
+}
+
+# @1      : absolute path to the target directory
+# @2      : (optional) requirements txt file
+function __apache_do_venv
+{
+	local -ar deps=( python3-dev python3-pip python3-venv python3-wheel )
+
+	if [ -z "$1" ] || [ ! -f "$2" ]; then
+		lets -l -e "virtual environment: invalid params"
+		return $s_err
+	fi
+
+	if [ ! -e "$1/bin/activate" ]; then
+		local install="python3 -m pip install --no-cache-dir --upgrade"
+		local -a global_packages=( "virtualenv" "pip" )
+		local -a local_packages=(
+			"wheel"
+			"setuptools==$setuptools_version"
+			"pelican==$pelican_version"
+			"markdown==$markdown_version"
+		)
+
+		util_install_dependency ${deps[@]} || {
+			lets -l -e "install python dependencies"
+			return $s_err
+		}
+
+		mkdir -p "$1" || {
+			lets -l -e "virtual envirnment: mkdir failed"
+			return $s_err
+		}
+
+		$install --user ${global_packages[@]} 2>&1 | lets -l -x "pip3"
+
+		python3 -m venv "$1" 2>&1 | lets -l -x "python"
+		[ "${PIPESTATUS[0]}" -eq 0 ] || {
+			lets -l -e "virtual environment: create failed"
+			return $s_err
+		}
+
+		source "$1/bin/activate" || return $s_err
+		(
+			$install ${local_packages[@]} 2>&1 | lets -l -x "pip3"
+			[ "${PIPESTATUS[0]}" -eq 0 ] || {
+				deactivate
+				return $s_err2
+			}
+
+			while read -r pkg; do
+				$install $pkg 2>&1 | lets -l -x "pip3"
+				[ "${PIPESTATUS[0]}" -eq 0 ] || {
+					deactivate
+					return $s_err3
+				}
+			done < "$2"
+
+			deactivate
+		)
+		case "$?" in
+			$s_err2) lets -l -e "install local packages"; return $s_err ;;
+			$s_err3) lets -l -e "install $(basename $2)"; return $s_err ;;
+		esac
+	else
+		lets -l -w "virtual environment: exists already"
+	fi
+
+	return $s_ok
+}
+
+function __apache_frontend_clean
+{
+	local target
+
+	for target in ${PELICAN_CLEAN_TARGETS[@]}; do
+		case "$target" in
+			pelican-html      ) make clean-html   || return $s_err ;;
+			pelican-theme     ) make clean-theme  || return $s_err ;;
+			pelican-photos    ) make clean-photos || return $s_err ;;
+			pelican-videos    ) make clean-videos || return $s_err ;;
+			pelican-distclean ) make distclean    || return $s_err ;;
+			pelican-all       ) make clean        || return $s_err ;;
+		esac
+	done
+}
+
+function __apache_frontend_make
+{
+	local target
+
+	for target in ${PELICAN_MAKE_TARGETS[@]}; do
+		case "$target" in
+			pelican-html)
+				PELICAN_PHOTO_LIBRARY=$photos_d \
+				PELICAN_PHOTO_EXCLUDEALL="1" \
+				PELICAN_VIDEO_LIBRARY=$videos_d \
+				PELICAN_VIDEO_EXCLUDEALL="1" \
+				PELICAN_SITEURL=$host_name \
+				PELICAN_CONTENT=$content_d \
+				make html || return $s_err ;;
+			pelican-all)
+				PELICAN_PHOTO_LIBRARY=$photos_d \
+				PELICAN_PHOTO_EXCLUDE=${EXCLUDED_PHOTOS[@]} \
+				PELICAN_PHOTO_SKIPTAG=$skiptag \
+				PELICAN_VIDEO_LIBRARY=$videos_d \
+				PELICAN_VIDEO_EXCLUDE=${EXCLUDED_VIDEOS[@]} \
+				PELICAN_SITEURL=$host_name \
+				PELICAN_VIDEO_SKIPTAG=$skiptag \
+				PELICAN_CONTENT=$content_d \
+				PELICAN="pelican" \
+				make html || return $s_err ;;
+		esac
+	done
+}
+
+
+# FIXME: improve integration with skiptag
+function __finalise_install
+{
+	[ ! -f "$1/$clean_skiptag_sh" ] || {
+		pushd "$1" &>/dev/null && \
+			source "$1/$clean_skiptag_sh" && popd &>/dev/null
+	}
+}
+
+function __apache_frontend_install
+{
+	local target
+
+	[ -d "$views_d" ] && [ -d "$html_d" ] || {
+		lets -l -w "not installing frontend"
+		return $s_ok
+	}
+
+	for target in ${PELICAN_INSTALL_TARGETS[@]}; do
+		case "$target" in
+			pelican-html)
+				INSTALLDIR=$views_d \
+				make install-html   || return $s_err ;;
+			pelican-theme)
+				INSTALLDIR_THEME=$html_d \
+				make install-theme  || return $s_err ;;
+			pelican-photos)
+				INSTALLDIR_PHOTOS=$photos_d \
+				make install-photos || return $s_err ;;
+			pelican-videos)
+				INSTALLDIR_VIDEOS=$videos_d \
+				make install-videos || return $s_err ;;
+			pelican-all)
+				INSTALLDIR=$views_d \
+				INSTALLDIR_THEME=$html_d \
+				INSTALLDIR_PHOTOS=$photos_d \
+				INSTALLDIR_VIDEOS=$videos_d \
+				make install || return $s_err
+
+				# FIXME: improve integration with skiptag
+				__finalise_install $photos_d || \
+					lets -l -e "Failed to finalise photos"
+				__finalise_install $videos_d || \
+					lets -l -e "Failed to finalise videos" ;;
+		esac
+	done
+}
+
+function __create_apache_frontend
+{
+	if [ $apache_has_media -eq 1 ]; then
+		[ -d "$photos_d" ] || mkdir -p "$photos_d" || return $s_err
+		[ -d "$videos_d" ] || mkdir -p "$videos_d" || return $s_err
+	fi
+
+	set -e; source $1/bin/activate; set +e || return $s_err3
+
+	__apache_frontend_clean   || { deactivate; return $s_err4; }
+	__apache_frontend_make    || { deactivate; return $s_err5; }
+	__apache_frontend_install || { deactivate; return $s_err6; }
+	__apache_frontend_clean   || { deactivate; return $s_err4; }
+
+	deactivate; true
+}
+
+function __apache_do_frontend
+{
+	local req="requirements.txt"
+	local file="$(realpath "$(find "$frontend_d" -name $req)")"
+
+	# Is this running in a container? If so this script assumes all global
+	# system wise dependencies to be provided at build time, only care about
+	# local packages taht can vary on the runtime.
+	if [ -z "$instance_d" ]; then
+		lets -l -i "virutal environment: container"
+		local venv_d=$pythonenv_con_d
+	else
+		local venv_d=$pythonenv_d
+	fi
+
+	__apache_do_venv $venv_d $file || return $s_err
+	lets -l -d "virtual environment: complete"
+
+	pushd $frontend_d &>/dev/null
+	__create_apache_frontend $pythonenv_d 2>&1 | lets -l -x "make"
+	case "${PIPESTATUS[0]}" in
+		$s_ok   ) popd &>/dev/null; return $s_ok          ;;
+		$s_err3 ) lets -l -e "failed to activate python"  ;;
+		$s_err4 ) lets -l -e "failed to clean frontend"   ;;
+		$s_err5 ) lets -l -e "failed to make frontend"    ;;
+		$s_err6 ) lets -l -e "failed to install frontend" ;;
+		*       ) lets -l -e "failed unknown error"       ;;
+	esac; popd &>/dev/null; return $s_err
+}
+
+function __apache_backend_install
+{
+	local target
+
+	[ -d "$backend_d/application" ] && \
+		[ -d "$backend_d/system" ] && [ -f "$backend_d/index.php" ] || {
+		lets -l -w "not installing backend"
+		return $s_ok
+	}
+
+	for target in ${CODEIGN_INSTALL_TARGETS[@]}; do
+		case "$target" in
+			codeign-all)
+				cp -ra $backend_d/application $www_d || return $s_err2
+				cp -ra $backend_d/system $www_d      || return $s_err3
+				cp -a $backend_d/index.php $html_d   || return $s_err4 ;;
+		esac
+	done
+}
+
+function __create_apache_backend
+{
+	__apache_backend_install
+}
+
+function __apache_do_backend
+{
+	__create_apache_backend 2>&1 | lets -l -x "copy"
+	case "$?" in
+		$s_err2 ) lets -l -e "failed to install application code" ;;
+		$s_err3 ) lets -l -e "failed to install system code"      ;;
+		$s_err4 ) lets -l -e "failed to install index.html"       ;;
+	esac
+}
diff --git a/module/apache/scheme.sh b/module/apache/scheme.sh
new file mode 100644
index 0000000..a9eb412
--- /dev/null
+++ b/module/apache/scheme.sh
@@ -0,0 +1,577 @@
+#!/bin/bash
+#
+# apache templates
+#
+# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
+
+# 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 (including the next
+# paragraph) 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
+# ITS SUPPLIERS 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.
+
+declare -r vhost_http_t="\
+<VirtualHost *:${_APACHE_HTTP_PORT_}>
+
+	ServerName ${_APACHE_SERVER_NAME_}
+	ServerAlias ${_APACHE_SERVER_ALIAS_}
+
+	ErrorLog \"${_APACHE_VHOST_HTTP_LOG_F_}\"
+	RewriteEngine on
+
+	# NOTE: any POST or PUT send with http (:80) will be redirected
+	# with the side effect of dropping any data sent. We don't care
+	# nothing should transit over http anyway.
+
+	# Enforce https and www.
+	RewriteCond %{REQUEST_SCHEME} =http [NC]
+	RewriteCond %{SERVER_NAME} =${_APACHE_SERVER_NAME_} [NC]
+	RewriteRule ^ https://${_APACHE_SERVER_ALIAS_}%{REQUEST_URI} [NE,R=permanent,L]
+
+	# Enforce https
+	RewriteCond %{REQUEST_SCHEME} =http [NC]
+	RewriteCond %{SERVER_NAME} =${_APACHE_SERVER_ALIAS_} [NC]
+	RewriteRule ^ https://${_APACHE_SERVER_ALIAS_}%{REQUEST_URI} [NE,R=permanent,L]
+
+</VirtualHost>"
+
+declare -r vhost_https_t="\
+<IfModule mod_ssl.c>
+	<VirtualHost *:${_APACHE_HTTPS_PORT_}>
+
+		ServerName ${_APACHE_SERVER_NAME_}
+		ServerAlias ${_APACHE_SERVER_ALIAS_}
+
+		# DocumentRoot is the only publicly accessible data:
+		#
+		#   - index.php, for dispatching requested URIs
+		#   - theme/css, style
+		#   - theme/js, style
+
+		DocumentRoot \"${_APACHE_DOCUMENT_ROOT_D_}\"
+		ErrorLog \"${_APACHE_SSL_LOG_F_}\"
+		LogLevel ${_APACHE_SSL_LOG_LEVEL_}
+
+		# Flags:
+		#
+		#   NE, not escape, as keep chars such as & and ?
+		#
+		#   R=status, redirect, if a valid URI is generated in the rewrite then
+		#   issue a request to the browser. It always prepends the rewrite with
+		#   [this protocol]://[thishost][:thisport]
+		#
+		#   L, last, do not feed the rewrite result to the next rule
+		#
+		#   NC, non case sensitive
+		#
+		# NOTE: This is to enforce 'www.' and *DO NOT* rediret! Skip POST and PUT
+		# because apache does an internal redirect 301 or 302 and drops the data
+		# attached to the request.
+
+		RewriteEngine on
+		RewriteCond %{REQUEST_METHOD} !^(POST|PUT) [NC]
+		RewriteCond %{SERVER_NAME} =${_APACHE_SERVER_NAME_} [NC]
+		RewriteRule ^ https://${_APACHE_SERVER_ALIAS_}%{REQUEST_URI} [NE]
+
+		# Proxy configuration
+		AllowEncodedSlashes on
+
+		# Route to vhost-gerrit.conf
+		#
+		# NOTE: 127.0.0.1 relative to container network
+
+		ProxyPassMatch /gerrit(/?)(.*) http://127.0.0.1:${_APACHE_GERRIT_PROXY_PORT_}/\$2 nocanon
+		ProxyPassReverse /gerrit(/?)(.*) http://127.0.0.1:${_APACHE_GERRIT_PROXY_PORT_}/\$2
+
+		# Within this context, it uses a file system path instead of URL path
+		#
+		#   i.e.
+		#   out %{REQUEST_URI} expands to https://servername.domain/whatever/
+		#   in  %{REQUEST_URI} expands to var/www/html/whatever/
+		#
+		# NOTE: %{REQUEST_URI} in directory context expands with a leading slash
+		# and a trailing slash
+
+		<Directory \"${_APACHE_DOCUMENT_ROOT_D_}\">
+
+			DirectorySlash Off
+			Require all granted
+
+			# This assumes to be calling a php handler, so:
+			#
+			# NOTE: Do not redirect or it will break the request.
+
+			RewriteCond %{REQUEST_FILENAME} !.*\.(css|js|mp4|jpg)$ [NC]
+			RewriteCond %{REQUEST_URI} !^/index.php [NC]
+			RewriteRule ^(.*)$ index.php/\$1 [NC,L]
+
+		</Directory>
+
+		# Lock out undesired auth requests
+
+		<Location \"/login/auth\">
+
+			Order Deny,Allow
+			Deny from all
+			Allow from ${_APACHE_MOD_AUTHNZ_LOCALHOST_}
+
+		</Location>
+
+		<LocationMatch \"^(/invite|/page/invite|/page/invite\\.html)$\">
+
+			AuthType Basic
+			AuthName \"Invite @ ${_APACHE_SERVER_NAME_}\"
+
+			# authnz_external
+			AuthBasicProvider external
+
+			# Call into php again
+			AuthExternal ${_APACHE_EXT_AUTH_KEYWORD_}
+
+			# mod_authz_core configuration
+			Require valid-user
+
+		</LocationMatch>
+
+		# NOTE: path relative to container rootfs
+		Include ${_APACHE_CON_SSL_CONF_F_}
+		SSLCertificateFile ${_APACHE_CON_SSL_CERT_F_}
+		SSLCertificateKeyFile ${_APACHE_CON_SSL_KEY_F_}
+
+		# Configure external authentication module
+		<IfModule mod_authnz_external.c>
+			DefineExternalAuth ${_APACHE_EXT_AUTH_KEYWORD_} pipe ${_APACHE_CON_EXT_AUTH_F_}
+		</IfModule>
+
+		# Configure set default environment
+		<IfModule mod_env.c>
+			SetEnv CI_ENV ${_APACHE_CODE_IGNITER_ENV_}
+		</IfModule>
+
+	</VirtualHost>
+</IfModule>"
+
+if [ "${_GERRIT_HAS_HTTPS_}" -eq 1 ]; then
+	declare -r vhost_gerrit_t_has_ssl="\
+	SSLProxyEngine on
+	SSLProxyCheckPeerCN off
+	SSLProxyCheckPeerName off"
+else
+	declare -r vhost_gerrit_t_has_ssl=""
+fi
+
+declare -r vhost_gerrit_t="\
+<VirtualHost 127.0.0.1:${_APACHE_GERRIT_PROXY_PORT_}>
+
+	# Restrict to only requests from 127.0.0.1
+
+	<LocationMatch \".*\">
+
+			Order Deny,Allow
+			Deny from all
+			Allow from 127.0.0.1
+
+	</LocationMatch>
+
+	ErrorLog \"${_APACHE_VHOST_GERRIT_LOG_F_}\"
+	LogLevel ${_APACHE_VHOST_GERRIT_LOG_LEVEL_}
+
+${vhost_gerrit_t_has_ssl}
+
+	ProxyVia off
+	ProxyRequests off
+	ProxyPreserveHost on
+	ProxyErrorOverride on
+
+	AllowEncodedSlashes on
+	RewriteEngine on
+	# Proxy incoming requests towards Gerrit Code Review
+	RewriteRule ^(/?)(.*)$ ${_GERRIT_PROXY_PROTOCOL_}://${_GERRIT_DKRC_FRONTEND_IP_}:${_GERRIT_PROXY_PORT_}/gerrit/\$2 [NE,P]
+
+	<LocationMatch \"(/gerrit/login(/?)|/login(/?))\">
+
+		AuthType Basic
+		AuthName \"Gerrit @ ${_APACHE_SERVER_NAME_}\"
+
+		# authnz_external
+		AuthBasicProvider external
+
+		# Call into php again
+		AuthExternal ${_APACHE_EXT_AUTH_KEYWORD_}
+
+		# mod_authz_core configuration
+		Require valid-user
+
+	</LocationMatch>
+
+	# Configure external authentication module
+	<IfModule mod_authnz_external.c>
+		DefineExternalAuth ${_APACHE_EXT_AUTH_KEYWORD_} pipe ${_APACHE_CON_EXT_AUTH_F_}
+	</IfModule>
+
+</VirtualHost>"
+
+declare -r ports_t="\
+# If you just change the port or add more ports here, you will likely also
+# have to change the VirtualHost statement in
+# /etc/apache2/sites-enabled/000-default.conf
+
+#
+# Port mapped to the host (only one)
+#
+Listen ${_APACHE_HTTP_PORT_}
+
+#
+# Gerrit, vhost, proxied
+#
+Listen ${_APACHE_GERRIT_PROXY_PORT_}
+
+<IfModule ssl_module>
+	Listen ${_APACHE_HTTPS_PORT_}
+</IfModule>
+
+<IfModule mod_gnutls.c>
+	Listen ${_APACHE_HTTPS_PORT_}
+</IfModule>"
+
+declare -r apache2_t="\
+# This is the main Apache server configuration file.  It contains the
+# configuration directives that give the server its instructions.
+# See http://httpd.apache.org/docs/2.4/ for detailed information about
+# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
+# hints.
+#
+#
+# Summary of how the Apache 2 configuration works in Debian:
+# The Apache 2 web server configuration in Debian is quite different to
+# upstream's suggested way to configure the web server. This is because Debian's
+# default Apache2 installation attempts to make adding and removing modules,
+# virtual hosts, and extra configuration directives as flexible as possible, in
+# order to make automating the changes and administering the server as easy as
+# possible.
+
+# It is split into several files forming the configuration hierarchy outlined
+# below, all located in the /etc/apache2/ directory:
+#
+#	/etc/apache2/
+#	|-- apache2.conf
+#	|	|--  ports.conf
+#	|-- mods-enabled
+#	|	|-- *.load
+#	|	|-- *.conf
+#	|-- conf-enabled
+#	|	|-- *.conf
+#	+-- sites-enabled
+#		|-- *.conf
+#
+#
+# * apache2.conf is the main configuration file (this file). It puts the pieces
+#   together by including all remaining configuration files when starting up the
+#   web server.
+#
+# * ports.conf is always included from the main configuration file. It is
+#   supposed to determine listening ports for incoming connections which can be
+#   customized anytime.
+#
+# * Configuration files in the mods-enabled/, conf-enabled/ and sites-enabled/
+#   directories contain particular configuration snippets which manage modules,
+#   global configuration fragments, or virtual host configurations,
+#   respectively.
+#
+#   They are activated by symlinking available configuration files from their
+#   respective *-available/ counterparts. These should be managed by using our
+#   helpers a2enmod/a2dismod, a2ensite/a2dissite and a2enconf/a2disconf. See
+#   their respective man pages for detailed information.
+#
+# * The binary is called apache2. Due to the use of environment variables, in
+#   the default configuration, apache2 needs to be started/stopped with
+#   /etc/init.d/apache2 or apache2ctl. Calling /usr/bin/apache2 directly will not
+#   work with the default configuration.
+
+#
+# Global configuration
+#
+
+ServerName ${_APACHE_SERVER_NAME_}
+
+#
+# ServerRoot: The top of the directory tree under which the server's
+# configuration, error, and log files are kept.
+#
+# NOTE!  If you intend to place this on an NFS (or otherwise network)
+# mounted filesystem then please read the Mutex documentation (available
+# at <URL:http://httpd.apache.org/docs/2.4/mod/core.html#mutex>);
+# you will save yourself a lot of trouble.
+#
+# Do NOT add a slash at the end of the directory path.
+#
+#ServerRoot \"/etc/apache2\"
+
+#
+# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
+#
+#Mutex file:\${APACHE_LOCK_DIR} default
+
+#
+# The directory where shm and other runtime files will be stored.
+#
+
+DefaultRuntimeDir \${APACHE_RUN_DIR}
+
+#
+# PidFile: The file in which the server should record its process
+# identification number when it starts.
+# This needs to be set in /etc/apache2/envvars
+#
+PidFile \${APACHE_PID_FILE}
+
+#
+# Timeout: The number of seconds before receives and sends time out.
+#
+Timeout 300
+
+#
+# KeepAlive: Whether or not to allow persistent connections (more than
+# one request per connection). Set to \"Off\" to deactivate.
+#
+KeepAlive On
+
+#
+# MaxKeepAliveRequests: The maximum number of requests to allow
+# during a persistent connection. Set to 0 to allow an unlimited amount.
+# We recommend you leave this number high, for maximum performance.
+#
+MaxKeepAliveRequests 100
+
+#
+# KeepAliveTimeout: Number of seconds to wait for the next request from the
+# same client on the same connection.
+#
+KeepAliveTimeout 5
+
+
+# These need to be set in /etc/apache2/envvars
+User \${APACHE_RUN_USER}
+Group \${APACHE_RUN_GROUP}
+
+#
+# HostnameLookups: Log the names of clients or just their IP addresses
+# e.g., www.apache.org (on) or 204.62.129.132 (off).
+# The default is off because it'd be overall better for the net if people
+# had to knowingly turn this feature on, since enabling it means that
+# each client request will result in AT LEAST one lookup request to the
+# nameserver.
+#
+HostnameLookups Off
+
+# ErrorLog: The location of the error log file.
+# If you do not specify an ErrorLog directive within a <VirtualHost>
+# container, error messages relating to that virtual host will be
+# logged here.  If you *do* define an error logfile for a <VirtualHost>
+# container, that host's errors will be logged there and not here.
+#
+ErrorLog \${APACHE_LOG_DIR}/error.log
+
+#
+# LogLevel: Control the severity of messages logged to the error_log.
+# Available values: trace8, ..., trace1, debug, info, notice, warn,
+# error, crit, alert, emerg.
+# It is also possible to configure the log level for particular modules, e.g.
+# \"LogLevel info ssl:warn\"
+#
+LogLevel trace1
+
+# Include module configuration:
+IncludeOptional mods-enabled/*.load
+IncludeOptional mods-enabled/*.conf
+
+# Include list of ports to listen on
+Include ports.conf
+
+
+# Sets the default security model of the Apache2 HTTPD server. It does
+# not allow access to the root filesystem outside of /usr/share and /var/www.
+# The former is used by web applications packaged in Debian,
+# the latter may be used for local directories served by the web server. If
+# your system is serving content from a sub-directory in /srv you must allow
+# access here, or in any related virtual host.
+<Directory />
+	Options FollowSymLinks
+	AllowOverride None
+	Require all denied
+</Directory>
+
+<Directory /usr/share>
+	AllowOverride None
+	Require all granted
+</Directory>
+
+<Directory /var/www/>
+	Options Indexes FollowSymLinks
+	AllowOverride None
+	Require all granted
+</Directory>
+
+#<Directory /srv/>
+#	Options Indexes FollowSymLinks
+#	AllowOverride None
+#	Require all granted
+#</Directory>
+
+# AccessFileName: The name of the file to look for in each directory
+# for additional configuration directives.  See also the AllowOverride
+# directive.
+#
+AccessFileName .htaccess
+
+#
+# The following lines prevent .htaccess and .htpasswd files from being
+# viewed by Web clients.
+#
+<FilesMatch \"^\.ht\">
+	Require all denied
+</FilesMatch>
+
+#
+# The following directives define some format nicknames for use with
+# a CustomLog directive.
+#
+# These deviate from the Common Log Format definitions in that they use %O
+# (the actual bytes sent including headers) instead of %b (the size of the
+# requested file), because the latter makes it impossible to detect partial
+# requests.
+#
+# Note that the use of %{X-Forwarded-For}i instead of %h is not recommended.
+# Use mod_remoteip instead.
+#
+LogFormat \"%v:%p %h %l %u %t \\\"%r\\\" %>s %O \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\" vhost_combined
+LogFormat \"%h %l %u %t \\\"%r\\\" %>s %O \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\" combined
+LogFormat \"%h %l %u %t \\\"%r\\\" %>s %O\" common
+LogFormat \"%{Referer}i -> %U\" referer
+LogFormat \"%{User-agent}i\" agent
+
+# Include of directories ignores editors' and dpkg's backup files,
+# see README.Debian for details.
+
+# Include generic snippets of statements
+IncludeOptional conf-enabled/*.conf
+
+# Include the virtual host configurations:
+IncludeOptional sites-enabled/*.conf
+
+# vim: syntax=apache ts=4 sw=4 sts=4 sr noet"
+
+if [ "${mod_mode}" = "${release}" ]; then
+	declare -rg dockerfile_debug_tools_t=""
+else
+	declare -rg dockerfile_debug_tools_t="\
+# debug only
+RUN apt-get -y install nmap
+RUN apt-get -y install net-tools"
+fi
+
+declare -rg dockerfile_t="\
+# Ref https://hub.docker.com/_/php/
+# php:7.3-apache-stretch
+# Apache/2.4.25 (Debian)
+# PHP 7.3 Cli
+ARG VERSION=7.3.9-apache-stretch
+FROM php:\$VERSION AS base
+
+RUN apt-get update
+RUN apt-get -y install sudo
+
+${dockerfile_debug_tools_t}
+
+# install apxs tools
+RUN apt-get -y install apache2-dev
+
+# mysqli driver
+RUN docker-php-ext-install mysqli
+
+#  mysql dbd
+#RUN apt-get -y install libaprutil1-dbd-mysql
+
+# serivce and a2* need root access
+USER root
+
+# import mod_authnz_external source
+COPY ${_APACHE_MOD_AUTHNZ_EXTERNAL_D_} /${_APACHE_MOD_AUTHNZ_EXTERNAL_D_}
+
+# build and install mod_authnz_external
+WORKDIR /${_APACHE_MOD_AUTHNZ_EXTERNAL_D_}
+RUN	apxs -c mod_authnz_external.c
+RUN	sudo apxs -i -a mod_authnz_external.la
+
+# stop apache, it starts with docker-compose
+RUN [\"/bin/bash\", \"-c\", \"service apache2 stop\"]
+
+# disable default conf
+RUN [\"/bin/bash\", \"-c\", \"a2dissite 000-default\"]
+
+# enable proxy
+RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_http\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_ajp\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod rewrite\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod deflate\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod headers\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_balancer\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_connect\"]
+RUN [\"/bin/bash\", \"-c\", \"a2enmod proxy_html\"]
+
+# enable mod_authnz_external
+RUN [\"/bin/bash\", \"-c\", \"a2enmod authnz_external\"]
+
+# enable dbd
+#RUN [\"/bin/bash\", \"-c\", \"a2enmod dbd\"]
+#RUN [\"/bin/bash\", \"-c\", \"a2enmod authn_dbd\"]
+#RUN [\"/bin/bash\", \"-c\", \"a2enmod authn_socache\"]
+
+# enable session
+#RUN [\"/bin/bash\", \"-c\", \"a2enmod session\"]
+#RUN [\"/bin/bash\", \"-c\", \"a2enmod session_dbd\"]
+
+# enable auth form
+#RUN [\"/bin/bash\", \"-c\", \"a2enmod request\"]
+#RUN [\"/bin/bash\", \"-c\", \"a2enmod auth_form\"]
+
+# enable SSL
+RUN [\"/bin/bash\", \"-c\", \"a2enmod ssl\"]"
+
+declare -r http_authentication_bang_t="\
+#!/bin/bash
+
+for key in username password; do
+	read -r value; eval \"declare -r \$key=\\\"\$value\\\"\"
+done
+
+declare -ir http_expected_status=\"${_APACHE_MOD_AUTHNZ_SUCCESS_CODE_}\"
+declare -r php_api=\"https://${_APACHE_MOD_AUTHNZ_LOCALHOST_}/login/auth\"
+
+declare curl_flags
+curl_flags+=\" -k -L --post301 --post302 --post303\"
+curl_flags+=\" -w %{http_code}\"
+curl_flags+=\" -o ${_APACHE_MOD_AUTHNZ_DEBUG_FLAG_}\"
+curl_flags+=\" -H \\\"${_APACHE_MOD_AUTHNZ_H_ACCEPT_}\\\"\"
+curl_flags+=\" -H \\\"${_APACHE_MOD_AUTHNZ_H_CONTENT_}\\\"\"
+curl_flags+=\" --data \\\"username=\$username&password=\$password\\\"\"
+
+declare -ir http_status=\"\$(eval \"curl \$curl_flags \$php_api\")\"
+[ \"\$http_status\" -eq \"\$http_expected_status\" ]
+declare -ir _CODE=\"\$?\"
+
+echo \"\$(date +'%d/%m/%Y - %H:%M:%S'): \${BASH_SOURCE[0]}: HTTP_STATUS: \$http_status\" >&2
+exit \$_CODE"