#!/usr/bin/env bash
# Revert the SUB-LABEL-SHIM injected into web-overrides/index.html on 2026-05-10.
#
# What it removes:
#   /* SUB-LABEL-SHIM-BEGIN ... */ ... /* SUB-LABEL-SHIM-END */
#
# Idempotent: safe to re-run.
# After reverting, hard-refresh the browser (Ctrl+Shift+R) so the cached
# index.html is fetched fresh.

set -euo pipefail

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
TARGET="$REPO_ROOT/web-overrides/index.html"

if [[ ! -f "$TARGET" ]]; then
  echo "ERROR: $TARGET not found" >&2
  exit 1
fi

if ! grep -q "SUB-LABEL-SHIM-BEGIN" "$TARGET"; then
  echo "shim already absent — nothing to do"
  exit 0
fi

cp "$TARGET" "$TARGET.bak.$(date -u +%Y%m%dT%H%M%SZ)"

# delete from BEGIN marker line through END marker line, inclusive
sed -i '/SUB-LABEL-SHIM-BEGIN/,/SUB-LABEL-SHIM-END/d' "$TARGET"

if grep -q "SUB-LABEL-SHIM" "$TARGET"; then
  echo "ERROR: revert left orphan markers in $TARGET" >&2
  exit 2
fi

echo "reverted. backup at $TARGET.bak.*"
echo "next: container needs no restart (index.html is bind-mounted); hard-refresh browser."
