#!/bin/bash

# Met à jour la configuration de ${CONF} en fonction du modèle ${TMPL}
# Viariables misent à jour :
#  - __DOMAIN__
# Il est possible de prendre en considération ou d'occulter des blocks.
# Le début du block est repéré par une ligne contenant {{XXX
# La fin du block est repéré par une ligne contenant }}
# L'affiche est fonction de XXX
# XXX = on => affichage systématique
# XXX = off => masquage systématique
# XXX = compose => affichage si la variable d'environnement proxy_compose à la valeur on

KAZ_ROOT=$(cd "$(dirname $0)/.."; pwd)
. "${KAZ_ROOT}/bin/.commonFunctions.sh"
setKazVars

. "${DOCKERS_ENV}"

usage () {
    echo $(basename "$0") " [-h] [-help] [-timestamp] template dst"
    echo "    -h"
    echo "    -help Display this help."
    echo "    -timestamp produce timestamp comment."
}

TIMESTAMP=""

case "$1" in
    '-h' | '-help' )
	usage
	shift
	exit;;
    '-time' | '-timestamp' )
	TIMESTAMP=YES
	shift;;
esac

# no more export in .env
PROXY_VARS=$(set | grep "proxy_.*=")
for var in ${PROXY_VARS}
do
    export ${var}
done

(
    # $1 = template
    # $2 = target
    if [ "${TIMESTAMP}" == "YES" ]; then
	echo "# Generated by $(pwd)$(basename $0)"
	echo "# à partir du modèle $1"
	echo "#" $(date "+%x %X")
	echo
    fi

    sed \
	-e "/^[ \t]*$/d"\
	-e "/^[ ]*#.*$/d"\
	-e "s|__CACHET_HOST__|${cachetHost}|g"\
	-e "s|__CALC_HOST__|${calcHost}|g"\
	-e "s|__CLOUD_HOST__|${cloudHost}|g"\
	-e "s|__DATE_HOST__|${dateHost}|g"\
	-e "s|__DOKUWIKI_HOST__|${dokuwikiHost}|g"\
	-e "s|__DOMAIN__|${domain}|g"\
	-e "s|__FILE_HOST__|${fileHost}|g"\
#	-e "s|__PAHEKO_API_PASSWORD__|${paheko_API_PASSWORD}|g"\
#	-e "s|__PAHEKO_API_USER__|${paheko_API_USER}|g"\
	-e "s|__PAHEKO_HOST__|${pahekoHost}|g"\
	-e "s|__GIT_HOST__|${gitHost}|g"\
	-e "s|__GRAV_HOST__|${gravHost}|g"\
	-e "s|__HTTP_PROTO__|${httpProto}|g"\
	-e "s|__LDAP_HOST__|${ldapHost}|g"\
  -e "s|__LDAPUI_HOST__|${ldapUIHost}|g"\
	-e "s|__MATTER_HOST__|${matterHost}|g"\
	-e "s|__OFFICE_HOST__|${officeHost}|g"\
	-e "s|__PAD_HOST__|${padHost}|g"\
	-e "s|__QUOTAS_HOST__|${quotasHost}|g"\
	-e "s|__SMTP_HOST__|${smtpHost}|g"\
	-e "s|__SYMPADB__|${sympaDBName}|g"\
	-e "s|__SYMPA_HOST__|${sympaHost}|g"\
#	-e "s|__SYMPA_MYSQL_DATABASE__|${sympa_MYSQL_DATABASE}|g"\
#	-e "s|__SYMPA_MYSQL_PASSWORD__|${sympa_MYSQL_PASSWORD}|g"\
#	-e "s|__SYMPA_MYSQL_USER__|${sympa_MYSQL_USER}|g"\
	-e "s|__VIGILO_HOST__|${vigiloHost}|g"\
	-e "s|__WEBMAIL_HOST__|${webmailHost}|g"\
	-e "s|__CASTOPOD_HOST__|${castopodHost}|g"\
	-e "s|__SPIP_HOST__|${spipHost}|g"\
	-e "s|__IMAPSYNC_HOST__|${imapsyncHost}|g"\
	-e "s|__YAKFORMS_HOST__|${yakformsHost}|g"\
	-e "s|__WORDPRESS_HOST__|${wordpressHost}|g"\
	-e "s|__MOBILIZON_HOST__|${mobilizonHost}|g"\
        -e "s|__API_HOST__|${apiHost}|g"\
	-e "s|__VAULTWARDEN_HOST__|${vaultwardenHost}|g"\
	-e "s|__DOMAIN_SYMPA__|${domain_sympa}|g"\
	$1 | awk '
		BEGIN     {cp=1}
		 /}}/     {cp=1 ; next};
		 /{{on/   {cp=1; next};
		 /{{off/  {cp=0; next};
		 match($0, /{{[a-zA-Z0-9_]+/)  {cp=(ENVIRON["proxy_" substr($0,RSTART+2,RLENGTH)] == "on"); next};
		          {if (cp) print $0};'
) > $2
