#!/usr/bin/env bash
# Deploy WIMVEE GLYTCH to https://partly.party/glytch/
#   /glytch/           -> FKTRY (the factory game, vite build)
#   /glytch/match.html -> the original match-the-glitch prototype (single file)
# Pattern per deploy-map: bundle -> rsync to /tmp staging -> docker cp into
# forum-nginx -> curl verify (cache-busted). Cloudflare fronts partly.party:
# purge cache after major changes.
set -euo pipefail
cd "$(dirname "$0")"

VPS=humanjing@100.71.119.27
CONTAINER=forum-nginx
WEBROOT=/usr/share/nginx/html

echo "— build —"
(cd fktry && npx vite build --base=./)
cp index.html fktry/dist/match.html

echo "— stage —"
rsync -az --delete fktry/dist/ "$VPS:/tmp/glytch-stage/"

echo "— swap into container —"
ssh "$VPS" "docker exec $CONTAINER rm -rf $WEBROOT/glytch \
  && docker cp /tmp/glytch-stage $CONTAINER:$WEBROOT/glytch \
  && rm -rf /tmp/glytch-stage"

echo "— verify (cache-busted) —"
curl -fsS -o /dev/null -w '%{http_code}  https://partly.party/glytch/\n'           "https://partly.party/glytch/?v=$(date +%s)"
curl -fsS -o /dev/null -w '%{http_code}  https://partly.party/glytch/match.html\n' "https://partly.party/glytch/match.html?v=$(date +%s)"
# data-endpoint check, not just index (classic nginx-routing failure mode):
ASSET=$(ls fktry/dist/assets | grep -m1 '\.js$')
curl -fsS -o /dev/null -w "%{http_code}  https://partly.party/glytch/assets/$ASSET\n" "https://partly.party/glytch/assets/$ASSET?v=$(date +%s)"
echo "done. If this was a major change: purge Cloudflare cache for partly.party."
