#!/bin/bash
# Every VS Code theme name HD pushes must exist in the generated theme pack.
#
# Why: HD's colour-scheme setting writes `workbench.colorTheme` into code-server's
# settings.json. VS Code does NOT warn on an unknown theme name -- it silently falls back to
# its DEFAULT LIGHT THEME. So a one-word mismatch between HD and the theme pack turns the
# editor white, and it looks like a broken theme rather than a missing one.
#
# That is not hypothetical: it happened twice on 2026-07-25. Once from a packaging mistake
# that stopped the pack registering at all, and once because HD referenced "Adom Blueprint"
# before that theme was generated. Both presented identically to the user: white editor.
#
# Usage: scripts/check-vscode-theme-names.sh   (exit 0 = every pushed name resolves)

set -u
cd "$(dirname "$0")/.."

MAP="src/lib/stores/settings.ts"
THEMES_DIR="vscode-themes/themes"

[ -f "$MAP" ] || { echo "check-vscode-theme-names: $MAP not found"; exit 2; }
[ -d "$THEMES_DIR" ] || { echo "check-vscode-theme-names: run tools/gen-vscode-themes.mjs first"; exit 2; }

# Names HD can push: the values of the VSCODE_THEME_FOR_SCHEME *declaration*.
#
# Parsed in python, not sed. A sed range re-triggers on every later match of its start
# pattern, and this identifier appears twice (declared, then used), so `sed -n '/NAME/,/^}/p'`
# opened a second range at the usage and swallowed the rest of the file -- which made the
# guard report every string in settings.ts as a missing theme.
pushed=$(python3 - "$MAP" <<'PYEOF'
import re, sys
src = open(sys.argv[1]).read()
m = re.search(r'const VSCODE_THEME_FOR_SCHEME[^=]*=\s*\{(.*?)\n\}', src, re.S)
if not m:
    sys.exit(0)   # no map declared yet: nothing to check, not a failure
for value in re.findall(r":\s*'([^']+)'", m.group(1)):
    print(value)
PYEOF
)

# Names the pack actually provides: the "name" field of each generated theme.
provided=$(grep -h '"name"' "$THEMES_DIR"/*.json | sed 's/.*"name"[[:space:]]*:[[:space:]]*"\(.*\)".*/\1/')

missing=""
while IFS= read -r n; do
    [ -z "$n" ] && continue
    printf '%s\n' "$provided" | grep -qxF "$n" || missing="$missing\n  - $n"
done <<< "$pushed"

if [ -n "$missing" ]; then
    echo "FAIL: HD pushes VS Code theme name(s) the pack does not provide:"
    printf "$missing\n"
    echo
    echo "  VS Code falls back to its DEFAULT LIGHT THEME on an unknown name, silently."
    echo "  Either add the theme to tools/gen-vscode-themes.mjs and regenerate, or fix the"
    echo "  name in VSCODE_THEME_FOR_SCHEME in $MAP."
    echo
    echo "  Pack currently provides:"
    printf '%s\n' "$provided" | sed 's/^/    /'
    exit 1
fi

count=$(printf '%s\n' "$pushed" | grep -c . || true)
echo "OK: all $count pushed theme name(s) resolve in the pack."

# The FRESH-INSTALL default writes a theme name too (hd-bootstrap.sh), and it is the one place
# a wrong name hurts someone who never opened Settings: a fresh machine boots straight into
# VS Code's default LIGHT theme with nothing on screen saying why. Same rule as the map above:
# the name must exist in the generated pack, verbatim.
bootstrap_theme=$(grep -oP "workbench\.colorTheme':\s*'\K[^']+" scripts/hd-bootstrap.sh | head -1)
if [ -n "$bootstrap_theme" ]; then
    if printf '%s\n' "$provided" | grep -Fxq "$bootstrap_theme"; then
        echo "OK: hd-bootstrap.sh default '$bootstrap_theme' resolves in the pack."
    else
        echo "FAIL: hd-bootstrap.sh writes workbench.colorTheme '$bootstrap_theme', which is not a"
        echo "      generated theme label. A fresh install would boot into VS Code's default LIGHT"
        echo "      theme. Labels are:"
        printf '%s\n' "$provided" | sed 's/^/        /'
        exit 1
    fi
fi

# THE GAP THIS CHECK USED TO HAVE.
#
# Everything above compares the REPO against the REPO. It passed all day while the editor kept
# falling back to light, because the pack INSTALLED in code-server was an older build that did
# not contain the theme HD was pushing. Build-time consistency is not deployment consistency.
#
# This cannot be verified from the repo, so it is a reminder rather than a gate: a CI box has no
# code-server to inspect. To check the live machine:
#
#   POST /workspace/exec  {"command":"ls ~/.local/share/code-server/extensions/adom.adom-themes-*/themes/"}
#
# and confirm one file per name above. After changing this list, REINSTALL the pack; a
# regenerate alone does not reach the editor.
echo "note: this compares the repo to itself. The pack INSTALLED in code-server is a separate"
echo "      thing and must be reinstalled after any change here, or the editor falls back to"
echo "      its default LIGHT theme on a name it does not have."
exit 0
