#!/usr/bin/env bash
set -euo pipefail

ROOT="${1:-.}"
BASE="$ROOT/frontend/public"

[ -d "$BASE" ] || { echo "❌ Dossier $BASE introuvable"; exit 1; }

script_tag='<script type="module" src="/assets/header.js?v=1"></script>'

# Inject before </body> in all .html if not already present
while IFS= read -r -d '' page; do
  if ! grep -q 'assets/header.js' "$page"; then
    sed -i '' -e "s#</body>#  ${script_tag}
</body>#g" "$page"
    echo "➕ header.js -> $page"
  fi
done < <(find "$BASE" -type f -name "*.html" -print0)

echo "✅ Injection terminée."
