#!/bin/bash -e

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

disable_init_fence() {
    systemctl disable turnkey-init-fence
    systemctl stop turnkey-init-fence
    echo "turnkey-init-fence is down"
}

if [[ -n "$_TURNKEY_INIT" ]]; then
    # ONLY run this from within turnkey-init
    for home in /root /home/admin; do
        if [[ -e $home/.profile.d/turnkey-init-fence ]]; then
            chmod -x $home/.profile.d/turnkey-init-fence
        fi
    done
    chmod -x /usr/lib/inithooks/firstboot.d/??turnkey-init-fence*
    disable_init_fence
elif [[ -z "$AUTO_RUN" ]]; then
    # disable the init-fence if AUTO_RUN not set (set by firstboot.d/29preseed
    # on headless builds)
    disable_init_fence
fi
