#!/bin/bash

# tag the init-fence index page with the appliance name, version and build;
# parsing and rendering live in lib/tagid.sh

[ -n "$_TURNKEY_INIT" ] && exit 0

INITHOOKS_PATH="${INITHOOKS_PATH:-/usr/lib/inithooks}"
INITFENCE_DEFAULT="${INITFENCE_DEFAULT:-/etc/default/turnkey-init-fence}"
TURNKEY_VERSION_FILE="${TURNKEY_VERSION_FILE:-/etc/turnkey_version}"
APT_CONF_TURNKEY="${APT_CONF_TURNKEY:-/etc/apt/apt.conf.d/01turnkey}"

# shellcheck source=lib/tagid.sh
source "$INITHOOKS_PATH/lib/tagid.sh"

APP_NAME=$(tagid_app_name "$(< "$TURNKEY_VERSION_FILE")")
VERSION=$(tagid_version "$(< "$TURNKEY_VERSION_FILE")")
BUILD=$(tagid_build "$(< "$APT_CONF_TURNKEY")")

### initfence 

# shellcheck source=default/turnkey-init-fence
source "$INITFENCE_DEFAULT"

if [[ ! -d "$HTDOCS" ]]; then
    mkdir -p "$(dirname "$HTDOCS")"
    cp -R "$INITHOOKS_PATH/turnkey-init-fence/htdocs" "$(dirname "$HTDOCS")"
fi

INITFENCE_INDEX="$HTDOCS/index.html"
sed -i "s|@APP_NAME@|$APP_NAME|" $INITFENCE_INDEX

if ! tagid_is_tagged "$INITFENCE_INDEX"; then
    tagid_render_scripts "$BUILD" "$VERSION" "$APP_NAME" >> "$INITFENCE_INDEX"
fi

# reload init-fence to pick up the updated index (if it is not running there
# is nothing to reload; it reads the index when it starts)
if systemctl is-active --quiet turnkey-init-fence; then
    systemctl reload turnkey-init-fence
fi
