#!/usr/bin/env bash
# Vendors the App's canonical hud-core into src/hud/core (design: App docs/g2-hud-pipeline.md section 3.2).
# Usage: scripts/sync-hud-core.sh <app-checkout-dir> <app-sha>
# The copy is byte-identical (tests, __golden__ and HUD_CORE.lock included); VENDORED_FROM records the App sha.
# Never edit src/hud/core by hand: change the App, then re-run this.
set -euo pipefail

if [ "$#" -ne 2 ]; then
    echo "usage: $0 <app-checkout-dir> <app-sha>" >&2
    exit 2
fi
app="$1"
sha="$2"
here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
src="$app/sources/hud/core"
dest="$here/src/hud/core"

[ -d "$src" ] || { echo "no hud-core at $src" >&2; exit 1; }
head="$(git -C "$app" rev-parse HEAD)"
if [ "$head" != "$sha" ]; then
    echo "App checkout is at $head, not $sha; check out $sha first" >&2
    exit 1
fi

rm -rf "$dest"
mkdir -p "$dest"
cp -R "$src"/. "$dest"/
printf '%s\n' "$sha" > "$dest/VENDORED_FROM"
echo "vendored hud-core from App $sha into src/hud/core"
