#!/bin/bash -e
# reconfigure: yes
# reconfigure-title: Mantis address
# reconfigure-description: Change the address people use to open Mantis.

. /etc/default/inithooks
[[ ! -e "$INITHOOKS_CONF" ]] || . "$INITHOOKS_CONF"
[[ -z "${_TURNKEY_RECONFIGURE:-}" ]] || APP_DOMAIN=''

if [[ -z "${_TURNKEY_INIT:-}" && "${AUTO_RUN^^}" == TRUE && "${APP_DOMAIN:-}" == DEFAULT ]]; then
    profile=/root/.profile.d/turnkey-init-fence
    [[ "${SUDOADMIN,,}" == true ]] && profile=/home/admin/.profile.d/turnkey-init-fence
    if [[ -x "$profile" && -x "$INITHOOKS_PATH/firstboot.d/97turnkey-init-fence-disable" ]] &&
       systemctl is-active --quiet turnkey-init-fence && systemctl is-enabled --quiet turnkey-init-fence; then
        echo "Mantis address will be requested at first login."
        exit 0
    fi
    echo "Cannot defer Mantis setup: first-login initialization fence is unavailable." >&2
    exit 1
fi
"$INITHOOKS_PATH/bin/mantis-address.py" --domain="${APP_DOMAIN:-}"
