#!/bin/sh
# Fetch npm-global CLIs as .tgz tarballs for offline install on the ISO target.
#
# These packages are installed by firstboot with `npm install -g` from local
# tarballs (no network needed on the target). Versions are pinned in
# packages/npm-globals.txt to prevent build-to-build drift, except lines that
# use a dist-tag on purpose (Pi tracks @latest; the resolved version is recorded
# in build-manifest.json as `pi_version`).
#
# Notes:
#   - Codex is shipped via the FreeBSD `codex` pkg (see pkg-list-host.txt),
#     not via npm — kept out of this bundle on purpose.
#   - Claude Code is intentionally not bundled in the XFCE USB image path.
#   - opencode + cursor have no working FreeBSD distribution (upstream gap).
#   - Output is dual-purpose: bundled into the ISO and used by firstboot.
#
# Usage:
#   ./scripts/fetch-npm-globals.sh           # fetch into tmp/npm-globals/
#   OUT_DIR=/some/path ./scripts/fetch-npm-globals.sh
#   NPM_GLOBALS_LIST=/path/to/list ./scripts/fetch-npm-globals.sh

set -eu

SCRIPT_DIR="$(dirname "$(realpath "$0")")"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
OUT_DIR="${OUT_DIR:-${ROOT_DIR}/tmp/npm-globals}"
NPM_GLOBALS_LIST="${NPM_GLOBALS_LIST:-${ROOT_DIR}/packages/npm-globals.txt}"

if ! command -v npm >/dev/null 2>&1; then
    echo "ERROR: npm not found in PATH (install www/npm-node24)" >&2
    exit 1
fi

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

mkdir -p "$OUT_DIR"
cd "$OUT_DIR"

# Keep the bundle deterministic. The output directory is cached across builds,
# so remove old versions and legacy package names before fetching current
# tarballs; otherwise firstboot may install stale CLIs after newer ones.
rm -f ./*.tgz

echo "==> Fetching npm-global tarballs into ${OUT_DIR}"
echo "==> Package list: ${NPM_GLOBALS_LIST}"
while IFS= read -r pkg || [ -n "$pkg" ]; do
    case "$pkg" in
        ''|'#'*) continue ;;
    esac
    echo "    npm pack ${pkg}"
    # `npm pack <name>@<version|dist-tag>` downloads the published tarball without
    # installing it. Output is `<name>-<resolved-version>.tgz` (scoped names get
    # their slash flattened to a dash). npm prints that filename on stdout — echo
    # it so the build log shows exactly which version a dist-tag (e.g. @latest)
    # resolved to.
    _packed=$(npm pack "$pkg" 2>/dev/null | tail -1)
    [ -n "${_packed}" ] && echo "      → ${_packed}"
done < "$NPM_GLOBALS_LIST"

echo ""
echo "==> Bundle contents:"
ls -lh "$OUT_DIR"/*.tgz

TOTAL=$(du -sh "$OUT_DIR" | cut -f1)
echo ""
echo "==> Total size: ${TOTAL}"
