#!/bin/bash -e

# translate the declarative instance description into the inithooks conf
# file, so that every later hook reads the values it already reads today
# - runs before everything else and is a no-op unless the file exists

INITHOOKS_DEFAULT="${INITHOOKS_DEFAULT:-/etc/default/inithooks}"
# shellcheck source=default/inithooks
source "$INITHOOKS_DEFAULT"

export INITHOOKS_LOGFILE

if [[ -n "$_TURNKEY_INIT" ]]; then
    # turnkey-init is an explicit interactive run
    exit 0
fi

# INITHOOKS_DECL names the file outright when it is set; with nothing set,
# declarative.py --which answers with the first of its candidates that is
# there, and with nothing at all when the image carries no description.
if [[ -z "$INITHOOKS_DECL" ]]; then
    if ! INITHOOKS_DECL=$("$INITHOOKS_PATH/bin/declarative.py" --which); then
        # a reader that cannot run cannot say there is a description; the
        # boot carries on as an image without one, and says why
        echo "warning: declarative.py --which failed, no description read" >&2
        exit 0
    fi
fi

if [[ -z "$INITHOOKS_DECL" ]] || [[ ! -e "$INITHOOKS_DECL" ]]; then
    exit 0
fi

if [[ -e "$INITHOOKS_CONF" ]] && grep -q '[^[:space:]]' "$INITHOOKS_CONF"; then
    echo "warning: $INITHOOKS_CONF is not empty, ignoring $INITHOOKS_DECL" >&2
    exit 0
fi

"$INITHOOKS_PATH/bin/declarative.py" --apply \
    --conf="$INITHOOKS_CONF" "$INITHOOKS_DECL"
