#!/bin/sh
# Pack the release asset for spt-pacer-tool: dist/adapter.spt, the fat
# multi-platform adapter archive — the ONE thing a release ships (plus its
# checksum). Since spt-core 0.44.0 a [shell].spawn program token resolves
# install-dir-first, so the archive is self-contained: `spt adapter add
# --release BigscreenVR/spt-pacer-tool --gh` + `spt shell spawn PACER` is the
# whole install, no PATH placement.
#
# Layout (published multi-platform `.spt` shape, "Cover several platforms in
# one .spt"): the shared manifest.toml at the archive root, and under each
# target-triple root EXACTLY the per-platform tree a flat .spt would place at
# the archive root — for pacer that is the bare binary. On install, spt-core
# extracts the shared root plus only the current node's triple, FLATTENED into
# the install dir; a node whose triple is absent is refused with
# NoArtifactForPlatform, never a partial install.
#
#   adapter.spt
#   |- manifest.toml                       shared
#   |- x86_64-pc-windows-msvc/
#   |  `- pacer-shell.exe
#   |- x86_64-unknown-linux-gnu/
#   |  `- pacer-shell
#   `- x86_64-unknown-linux-musl/
#      `- pacer-shell                      pre-glibc-2.39 hosts
#
# musl rides INSIDE the archive: triple recognition for it landed in spt-core
# v0.30.0 (doyle-confirmed 2026-07-26; the docs sentence lags in PR #98) — a
# musl node self-identifies as the musl triple and the extractor selects and
# flattens it like the other two.
#
# Build the inputs first (ALWAYS rebuild all three fresh — a stale binary
# silently ships old code; the staleness check below is a backstop, not the
# discipline):
#   cargo build --release
#   cargo zigbuild --release --target x86_64-unknown-linux-gnu
#   cargo zigbuild --release --target x86_64-unknown-linux-musl
# [impl->REQ-PACER-INSTALL]
set -eu

ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd)
cd "$ROOT"

DIST="$ROOT/dist"
STAGE="$DIST/.stage"

WIN_TRIPLE=x86_64-pc-windows-msvc
GNU_TRIPLE=x86_64-unknown-linux-gnu
MUSL_TRIPLE=x86_64-unknown-linux-musl

WIN_BIN="target/release/pacer-shell.exe"
GNU_BIN="target/$GNU_TRIPLE/release/pacer-shell"
MUSL_BIN="target/$MUSL_TRIPLE/release/pacer-shell"

for f in "$WIN_BIN" "$GNU_BIN" "$MUSL_BIN" manifest.toml; do
    [ -f "$f" ] || { echo "package-adapter: missing $f — build all three targets first" >&2; exit 1; }
done

# A stale binary silently ships old code, so refuse anything older than the
# sources it was supposed to be built from.
for f in "$WIN_BIN" "$GNU_BIN" "$MUSL_BIN"; do
    for src in src/*.rs Cargo.toml; do
        [ "$f" -nt "$src" ] || { echo "package-adapter: $f is older than $src — rebuild" >&2; exit 1; }
    done
done

rm -rf "$DIST"
mkdir -p "$STAGE/$WIN_TRIPLE" "$STAGE/$GNU_TRIPLE" "$STAGE/$MUSL_TRIPLE"

cp manifest.toml "$STAGE/manifest.toml"
cp "$WIN_BIN"  "$STAGE/$WIN_TRIPLE/pacer-shell.exe"
cp "$GNU_BIN"  "$STAGE/$GNU_TRIPLE/pacer-shell"
cp "$MUSL_BIN" "$STAGE/$MUSL_TRIPLE/pacer-shell"
chmod +x "$STAGE/$GNU_TRIPLE/pacer-shell" "$STAGE/$MUSL_TRIPLE/pacer-shell"

tar -czf "$DIST/adapter.spt" -C "$STAGE" manifest.toml "$WIN_TRIPLE" "$GNU_TRIPLE" "$MUSL_TRIPLE"
rm -rf "$STAGE"

( cd "$DIST" && sha256sum adapter.spt > SHA256SUMS )

echo "packed under dist/ (adapter version $(grep -m1 '^version' manifest.toml)):"
( cd "$DIST" && ls -l . && echo && echo "adapter.spt contents:" && tar -tzf adapter.spt )
