#!/bin/bash # prebuild-fetch.sh — Fetch Estate API data at build time and save as static JSON # # Called before `hugo` to populate static/data/ with API snapshots. # The estate.js client reads from /data/*.json as the primary source. # # Usage: bash scripts/prebuild-fetch.sh set -euo pipefail DIR="$(cd "$(dirname "$0")/.." && pwd)" DATA_DIR="${DIR}/static/data" API_HOST="${API_HOST:-http://127.0.0.1:8000}" API_KEY="trentuna-estate-key-2026" mkdir -p "$DATA_DIR" ENDPOINTS=( "summary" "health" "disk" "events?limit=10" "repos" "providers" "builds" "trends?limit=5" "state" "api/garden" ) echo "→ Fetching Estate API data for garden build…" for ep in "${ENDPOINTS[@]}"; do filename="$(echo "$ep" | sed 's/[?&=/]/-/g' | sed 's/--*/-/g' | sed 's/^-//')" url="${API_HOST}/${ep}" if json=$(curl -sf -H "X-API-Key: ${API_KEY}" "$url" 2>/dev/null); then echo "$json" > "${DATA_DIR}/${filename}.json" echo " ✓ ${ep} → ${filename}.json ($(echo "$json" | wc -c) bytes)" else echo " ✗ ${ep} — API unreachable" echo '{"error":true,"message":"API unavailable at build time"}' > "${DATA_DIR}/${filename}.json" fi done echo "→ Done. ${#ENDPOINTS[@]} endpoints cached."