#!/bin/bash -e
# Initialize independent Hub services without exposing enrollment capability.
set +x
. /etc/default/inithooks
[ ! -e "$INITHOOKS_CONF" ] || . "$INITHOOKS_CONF"

if [ -n "${HUB_EMAIL_TOKEN:-}" ]; then
    if ! printf '%s' "$HUB_EMAIL_TOKEN" | "$INITHOOKS_PATH/bin/turnkey-mail" enroll --token-stdin; then
        echo 'App email setup did not finish. Check turnkey-mail status; temporary failures retry automatically.' >&2
    fi
    # Explicit legacy keys remain supported; new Hub launches use only a token.
    [ -n "${HUB_APIKEY:-}" ] || exit 0
fi
[ "${HUB_APIKEY:-}" != "SKIP" ] || exit 0
"$INITHOOKS_PATH/bin/hubservices.py" --apikey="${HUB_APIKEY:-}" --fqdn="${FQDN:-}"
