#!/usr/bin/env bash
# Install the adom-3d-viewer-babylon9 bundle: fetch this version's built ESM
# bundle (release asset) into a shared vendor location tools can symlink.
set -euo pipefail
cd "$(dirname "$0")"
V=$(tr -d '[:space:]' < VERSION)
DEST="${ADOM_3D_VIEWER_DIR:-$HOME/.local/share/adom-3d-viewer-babylon9}"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
adom-wiki release download adom/adom-3d-viewer "$V" --platform any --out "$TMP"
TAR="$TMP/adom-3d-viewer-babylon9-$V.tar.gz"
if [ ! -f "$TAR" ]; then
  echo "ERROR: release asset adom-3d-viewer-babylon9-$V.tar.gz did not download" >&2
  exit 1
fi
mkdir -p "$DEST.new-$$"
tar -xzf "$TAR" -C "$DEST.new-$$"
rm -rf "$DEST"
mv "$DEST.new-$$" "$DEST"
echo "OK: adom-3d-viewer-babylon9 $V installed at $DEST"
echo "Hint: load $DEST/adom-3d-viewer-babylon9.esm.js via <script type=\"module\">; serve the whole directory (code-split chunks)."
