#!/bin/bash
set -e

cd "$(dirname "$0")"

echo "⚡ Building AOT WASM frontend (frontend/main.coni -> frontend/app.wasm)..."
if [ -x "/Users/nico/coni/coni-lang/coni" ]; then
  /Users/nico/coni/coni-lang/coni compile-wasm frontend/main.coni -o frontend
else
  coni compile-wasm frontend/main.coni -o frontend
fi
rm -f frontend/app.wat frontend/app_prepatch.wat frontend/main.wasm frontend/wasm_exec.js frontend/worker.js

echo "🚀 Syncing Paw & Crown Cute 2D Chess to vendredi..."
ssh vendredi 'mkdir -p /var/www/coni/wasm-apps/apps/chess/data'
rsync -rlvz --exclude '.git' --exclude '.DS_Store' --exclude 'coni.edn' --exclude 'data/chess_store.edn' --exclude '*.wat' --delete ./ vendredi:/var/www/coni/wasm-apps/apps/chess/

ssh vendredi 'cat << "EOF" > /var/www/coni/wasm-apps/apps/chess/coni.edn
{:compiler "/opt/coni-lang"
 :dependencies {"libs" "/opt/coni-lang/libs"}
 :main "backend/main.coni"}
EOF'

echo "🔄 Restarting conimo-chess service on vendredi..."
ssh vendredi 'sudo systemctl restart conimo-chess'

echo "✅ Deployed AOT WASM to https://pawcrown.hellonico.info"
