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

# Fresh-orb setup for Mix Park (SvelteKit 2 + Vite 8 + Three.js, npm lockfile).
# Non-interactive and idempotent: safe to re-run on a warm snapshot.

log() { echo "==> $1 (t=${SECONDS}s)"; }

# Node 24 (Vite 8 / SvelteKit 2 need >= 20.19; repo is developed on 24.x)
log "check node"
if ! command -v node >/dev/null 2>&1 || ! node --version | grep -qE '^v2[4-9]'; then
	log "install node 24"
	curl -fsSL https://nodejs.org/dist/v24.13.0/node-v24.13.0-linux-x64.tar.xz -o /tmp/node24.tar.xz
	sudo tar -xJf /tmp/node24.tar.xz -C /usr/local --strip-components=1
	rm -f /tmp/node24.tar.xz
fi
node --version
npm --version

# Dependencies from the lockfile. Cold orb: exact clean install. Warm snapshot
# (node_modules restored): cheap converging install instead of a full rebuild.
if [ ! -d node_modules ]; then
	log "npm ci (cold)"
	npm ci --no-audit --no-fund
else
	log "npm install (warm)"
	npm install --no-audit --no-fund
fi

# svelte-kit sync runs via the "prepare" script during install, but make sure
# generated files exist even when the install above was a no-op.
log "svelte-kit sync"
npm run prepare >/dev/null

log "done"
