#!/bin/sh
# rustelo-htmx-server — local installation wrapper
#
# Resolves the site root, sets required env vars, and exec's the real binary.
# Installed at $HOME/.local/bin/rustelo-htmx-server by `just local-install`.
#
# Site root resolution order:
#   1. --site <path>  (explicit workspace root containing site/)
#   2. $PWD has a site/ subdirectory
#   3. $HOME/.config/rustelo-htmx-server/ exists and has site/
#
# Any env var set in the caller environment is preserved (: "${VAR:=...}").
set -eu

REAL_BIN="$HOME/.local/libexec/rustelo-htmx-server"
NICKEL_BASE="$HOME/.local/share/rustelo/nickel"
TEMPLATES_BASE="$HOME/.local/share/rustelo-htmx-server/htmx-templates"
HTMX_ASSETS_BASE="$HOME/.local/share/rustelo-htmx-server/htmx"
CONFIG_HOME="$HOME/.config/rustelo-htmx-server"

# -- parse --site flag before forwarding remaining args to the server ------
SITE_ROOT=""
while [ $# -gt 0 ]; do
    case "$1" in
        --site|-s)
            SITE_ROOT="$2"; shift 2 ;;
        --site=*)
            SITE_ROOT="${1#*=}"; shift ;;
        *) break ;;
    esac
done

# -- site root resolution --------------------------------------------------
if [ -z "$SITE_ROOT" ]; then
    if [ -d "$PWD/site" ]; then
        SITE_ROOT="$PWD"
    elif [ -d "$CONFIG_HOME/site" ]; then
        SITE_ROOT="$CONFIG_HOME"
    else
        printf 'rustelo-htmx-server: site root not found\n' >&2
        printf '  provide one of:\n' >&2
        printf '    --site <workspace>       workspace root that contains site/\n' >&2
        printf '    run from a dir with site/ subfolder\n' >&2
        printf '    place config at %s/site/\n' "$CONFIG_HOME" >&2
        exit 1
    fi
fi

# -- env setup (caller values take precedence) -----------------------------
: "${SITE_CONFIG_PATH:=$SITE_ROOT/site/config/index.ncl}"
: "${NICKEL_IMPORT_PATH:=$NICKEL_BASE:$SITE_ROOT/site/config}"
# SSR content-grid index root (matches the deployed PV convention: site/r,
# sibling of the site tree). Absolute so it's independent of process cwd.
: "${SITE_SERVER_CONTENT_ROOT:=$SITE_ROOT/site/r}"

# Routes the site contributes, merged over the ones embedded in the image.
# Set only when the site actually ships them: unset means merge_site_routes
# returns empty, which is the correct answer for a site that declares none —
# but a silent 404 for one that does, since nothing else reports the miss.
# Absolute for the same reason as SITE_SERVER_CONTENT_ROOT: the value is read
# from wherever the server happens to be started.
if [ -d "$SITE_ROOT/site/config/routes" ]; then
    : "${RUSTELO_SITE_ROUTES:=$SITE_ROOT/site/config/routes}"
fi

if [ -d "$SITE_ROOT/htmx-templates" ]; then
    : "${HTMX_TEMPLATE_PATH:=$SITE_ROOT/htmx-templates}"
else
    : "${HTMX_TEMPLATE_PATH:=$TEMPLATES_BASE}"
fi

# htmx runtime (/assets/htmx → htmx.min.js + ext/*). Prefer a site-local copy,
# else the framework runtime installed under ~/.local/share.
if [ -d "$SITE_ROOT/templates/shared/htmx" ]; then
    : "${HTMX_ASSETS_PATH:=$SITE_ROOT/templates/shared/htmx}"
else
    : "${HTMX_ASSETS_PATH:=$HTMX_ASSETS_BASE}"
fi

# RUSTELO_SITE_ROUTES may be unset here (site ships no routes dir); exporting an
# unset name is a no-op and leaves it absent from the child env, which is what
# "no routes contributed" has to look like.
export SITE_CONFIG_PATH NICKEL_IMPORT_PATH HTMX_TEMPLATE_PATH HTMX_ASSETS_PATH SITE_SERVER_CONTENT_ROOT RUSTELO_SITE_ROUTES

exec "$REAL_BIN" "$@"
