#!/usr/bin/env bash
# NARROW publish for lighter copies and 'use in games' (tools/lighter_worker.py): GPU pass + index, then only index.json,
# cards/ and new blobs to gex44 (+ cloud copy), then the spike. Never syncs code or kits: the dev tree can hold unreleased
# work (10 Oct: a newer town kit than gex44's) that tools/publish_all.sh would ship with it.
set -euo pipefail
ROOT=$(cd "$(dirname "$0")/.." && pwd); cd "$ROOT"; C=~/.claude/coord/coord; H=root@5.9.10.114
exec 9>/tmp/fal-publish.lock; flock 9
python3 tools/build_index.py
tools/gpu/run.sh tools/gpu/measure_probe.py site/out/measure
python3 tools/build_index.py --measured site/out/measure/measured.json
for i in $(seq 1 20); do $C claim gex44:/srv/forge-assets/ --owner fal-lighter-publish --note "asset library: lighter copy publish (index, cards, blobs)" >/dev/null 2>&1 && break; sleep 30; done
rsync -a cards/ "$H:/srv/forge-assets/repo/cards/"
rsync -a --ignore-existing store/blobs/ "$H:/srv/forge-assets/blobs/"
rsync -a index.json "$H:/srv/forge-assets/repo/index.json"   # last: the Factory reloads it when it changes
ssh "$H" 'chown -R root:root /srv/forge-assets/repo/cards /srv/forge-assets/repo/index.json /srv/forge-assets/blobs && /srv/forge-assets/repo/tools/gcs_sync.sh'
$C release gex44:/srv/forge-assets/ --owner fal-lighter-publish >/dev/null 2>&1 || true
python3 tools/build_site.py "$(TZ=Europe/Paris date '+%H:%M CET')" >/dev/null && tools/publish.sh >/dev/null && find site/out/pub -delete
echo published
