← back to 90210 Agentabrams

sync.sh

45 lines

#!/bin/bash
# Reconcile standalone source builds -> this topic hub (games/<id>/).
# Mapping lives in sources.tsv: "<hub-id>\t<abs-source-project-path>".
# Usage: ./sync.sh [--commit]
set -euo pipefail
HUB="$(cd "$(dirname "$0")" && pwd)"
MAP="$HUB/sources.tsv"
[ -f "$MAP" ] || { echo "no sources.tsv"; exit 0; }
changed=0
while IFS=$'\t' read -r id src; do
  [ -z "${id:-}" ] && continue
  case "$id" in \#*) continue;; esac
  src="${src/#\~/$HOME}"
  dest="$HUB/games/$id"
  if [ ! -d "$src" ]; then echo "! source missing for $id: $src"; continue; fi
  mkdir -p "$dest"
  if ! cmp -s "$src/index.html" "$dest/index.html" 2>/dev/null; then
    cp "$src/index.html" "$dest/index.html"; echo "synced index.html -> games/$id/"; changed=1
  fi
  for aa in "$src"/*.aa; do [ -e "$aa" ] || continue; cp "$aa" "$dest/"; done
done < "$MAP"
[ "$changed" = 1 ] && echo "sync: updates applied" || echo "sync: all in sync"

# Regenerate the file:// fallback mirror of games.json (a <script src> loads
# from a local file; fetch() is blocked over file://). Keeps games-data.js in
# lockstep with games.json so double-clicking index.html shows every game.
if [ -f "$HUB/games.json" ]; then
  python3 - "$HUB" <<'PY'
import json, os, sys
hub = sys.argv[1]
d = json.load(open(os.path.join(hub, 'games.json')))
with open(os.path.join(hub, 'games-data.js'), 'w') as f:
    f.write('/* AUTO-GENERATED from games.json by sync.sh — do not edit by hand.\n')
    f.write('   file:// fallback: <script src> works from a local file, fetch() does not. */\n')
    f.write('window.__GAMES_DATA__ = ')
    json.dump(d, f, ensure_ascii=False, indent=2)
    f.write(';\n')
PY
  echo "regenerated games-data.js ($(grep -c '"id"' "$HUB/games.json") games)"
fi
if [ "${1:-}" = "--commit" ] && [ -n "$(git -C "$HUB" status --porcelain)" ]; then
  git -C "$HUB" add -A
  git -C "$HUB" -c user.email="steve@designerwallcoverings.com" -c user.name="Steve" commit -q -m "sync builds from source" && echo "committed hub"
fi