#!/usr/bin/env bash
# Mirror WESP zootech + admin static UI into site web public/static (1:1 paths).
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
WESP_STATIC="${WESP_STATIC:-$ROOT/../wesp/static}"
DEST="$ROOT/apps/web/public/static"
ORCH_BOOT="$ROOT/apps/web/public/wesp/js/wesp-orchestrator-boot.js"
AUTH_BRIDGE_SRC="$ROOT/apps/web/public/wesp/js/wesp-orchestrator-auth-bridge.js"

if [ ! -d "$WESP_STATIC" ]; then
  echo "WESP static not found: $WESP_STATIC" >&2
  exit 1
fi

mkdir -p "$DEST"

# Full mirror: same /static/* and page HTML paths as on the WESP hub.
rsync -a --delete \
  --exclude '.DS_Store' \
  "$WESP_STATIC/" "$DEST/"

# Orchestrator-only adapters (never overwritten by hub copy).
mkdir -p "$DEST/js"
if [ -f "$ORCH_BOOT" ]; then
  cp "$ORCH_BOOT" "$DEST/js/wesp-orchestrator-boot.js"
fi
if [ -f "$AUTH_BRIDGE_SRC" ]; then
  cp "$AUTH_BRIDGE_SRC" "$DEST/js/wesp-orchestrator-auth-bridge.js"
fi
LOGIN_ADAPTER_SRC="$ROOT/apps/web/public/wesp/js/wesp-orchestrator-login.js"
if [ -f "$LOGIN_ADAPTER_SRC" ]; then
  cp "$LOGIN_ADAPTER_SRC" "$DEST/js/wesp-orchestrator-login.js"
fi

echo "Synced WESP static from $WESP_STATIC -> $DEST"
