#!/bin/bash

# Note that as of v19.0, the init-fence service is enabled by default

# Only run if AUTO_RUN is set (set by firstboot.d/29preseed on headless builds)
# and not if called by turnkey-init
if [[ -n "$_TURNKEY_INIT" ]]; then
    exit 0
elif [[ -z "$AUTO_RUN" ]]; then
    logger -t inithooks "Skipping $(basename "$0") because AUTO_RUN not set"
    exit 0
fi

# shellcheck source=default/inithooks
source /etc/default/inithooks
if [[ -e "$INITHOOKS_CONF" ]]; then
    # shellcheck disable=SC1090
    source "$INITHOOKS_CONF"
fi

USERNAME="root"
if [[ "${SUDOADMIN,,}" == "true" ]]; then
    USERNAME="admin"
fi

PROFILE_FIRSTLOGIN=$(eval printf ~$USERNAME)/.profile.d/turnkey-init-fence
if [[ -f "$PROFILE_FIRSTLOGIN" ]]; then
    chmod +x "$PROFILE_FIRSTLOGIN"
fi
