#!/bin/bash

set -e

PROGNAME="`basename $0`"
BOOTSTRAPPED=0
KEYCLOAK_RELOAD=0
ADMIN_PASSWORD=''
TMP_ADMIN_PASSWORD=''
LISTEN_HOST=''
LISTEN_PORT=''

. /usr/share/grommunio-pkg-bits/scripts/setup-include.sh
SHARED_TEMPLATES='
shared/grommunio/db-host
shared/grommunio/db-name
shared/grommunio/db-user
shared/grommunio/db-password
shared/grommunio/db-password-again
shared/grommunio/db-password-mismatch
shared/grommunio/db-password-must-enter
shared/grommunio/db-always-update-settings'
PKG_TEMPLATES='
grommunio-keycloak-grommuniodb/internal/db-purge
'

configure_keycloak_grommuniodb () {
        # Create and intialize DB
        . /usr/share/dbconfig-common/dpkg/frontend.postinst.mysql

	# There is very subtle interplay going on here setting UCF_FORCE_CONFFNEW,
	# or having it unset as this is how ucf evaluates this env var
	# ithe following is to keep things readable and understandable
	db_get shared/grommunio/db-always-update-settings
	if [ "$RET" = 'true' ]; then
        	UCF_FORCE_CONFFNEW='something' dbc_go grommunio-keycloak-grommuniodb "$@"
	else
        	dbc_go grommunio-keycloak-grommuniodb "$@"
	fi

        # Source db config
        local SOURCED_GDB_CONFIG=0
        if [ -r /etc/dbconfig-common/grommunio-keycloak-grommuniodb.conf ]; then
                . /etc/dbconfig-common/grommunio-keycloak-grommuniodb.conf && SOURCED_GDB_CONFIG=1

		# Frontends don't purge DBs
		# Fix purge flag - this is created by dbconfig-mysql
		#local DB_PURGE=''
                #if db_get grommunio-keycloak-grommuniodb/internal/db-purge; then 
                #	DB_PURGE="$RET"
		#fi
               	#[ -z "$DB_PURGE" ] && DB_PURGE="true"
               	#db_set grommunio-keycloak-grommuniodb/purge "$DB_PURGE"
        fi

	local TMPL_DIR="/usr/share/grommunio-keycloak/templates"
	local tmpdir=`mktemp -d`
	# manipulate umask so passwords are never exposed to users
	local OLD_UMASK=`umask`
	umask 0027

	# Process grommunio-properties
	local G_PROPS="/etc/grommunio-keycloak/grommunio.properties"
	local TMP_G_PROPS="${tmpdir}/grommunio.properties"
	local G_DB_TYPE='mariadb'
	local G_DB_HOST='localhost'
	local G_DB_PORT="3306"
	local G_DB_NAME='grommunio'
	local G_DB_USER='grommunio'
	local G_DB_PASSWORD='KnockKnock'
	if [ $SOURCED_GDB_CONFIG -eq 1 ]; then
		[ "$dbc_dbtype" = 'mysql' ] && DB_TYPE='mariadb'
		[ "$dbc_dbtype" = 'pgsql' ] && DB_TYPE='postgres'
		G_DB_HOST="$dbc_dbserver"
		G_DB_PORT="$dbc_dbport"
		G_DB_NAME="$dbc_dbname"
		G_DB_USER="$dbc_dbuser"
		G_DB_PASSWORD="$dbc_dbpass"
	fi
	esh -o "$TMP_G_PROPS" "${TMPL_DIR}/grommunio-properties.esh" \
		g_db_host="$G_DB_HOST" g_db_name="$G_DB_NAME" g_db_user="$G_DB_USER" \
		g_db_password="$G_DB_PASSWORD"
	# Restore original umask - finished with editing files with passwords
	umask "$OLD_UMASK"
	ucf --three-way --debconf-ok -Z "$TMP_G_PROPS" "$G_PROPS" && KEYCLOAK_RELOAD=1
	ucfr grommunio-keycloak-grommuniodb "$G_PROPS"
	chown groauth:gromox "$G_PROPS"

	rm -rf "$tmpdir"
	return 0
}

case "$PROGNAME" in
	setup-grommunio-keycloak-grommuniodb)
		check_if_root
		start_debconf -f -x 'grommunio-keycloak-grommuniodb' "$@"
		configure_keycloak_grommuniodb ${ARGS}
		systemctl restart grommunio-keycloak.service
		;;
esac
