#!/usr/bin/env bash
# Deploy the PWA + embeddable player bundle to pages.jkaindl.de.
#
# Replicates the two-stage build of the retired Forgejo workflow
# (.forgejo/workflows/deploy.yml — dead since the migration to
# git.jkaindl.de: no CI runner there by design, and the git-pages action
# only targets Codeberg Pages) and rsyncs dist/ to the pages server
# (/srv/pages/physarum/, served at https://pages.jkaindl.de/physarum/).
#
# BASE_PATH=/physarum/ is deploy-critical: without it the build emits
# root-absolute asset URLs that break under the /physarum/ subpath.
# Third-party embeds expect the bundle at /physarum/physarum-player.js.
#
# Auth is via the dedicated deploy key behind the `pages-deploy` SSH host alias
# (~/.ssh/config) — restricted server-side to rsync into /srv/pages only.
#
# Requires real rsync 3.x — macOS ships openrsync, which is incompatible with
# the server-side rrsync wrapper: brew install rsync
#
# Usage: bash scripts/deploy-page.sh
set -euo pipefail
RSYNC="${RSYNC:-/opt/homebrew/bin/rsync}"
# No pipe here: grep -q + pipefail would turn rsync's SIGPIPE into a failure.
case "$("$RSYNC" --version 2>/dev/null || true)" in
*"version 3."*) ;;
*)
echo "ERROR: $RSYNC is not rsync 3.x (macOS openrsync won't work): brew install rsync" >&2
exit 1
;;
esac
DEST="pages-deploy:physarum/"
cd "$(git rev-parse --show-toplevel)"
if [[ -n "$(git status --porcelain)" ]]; then
echo "ERROR: working tree not clean — commit or stash before deploying." >&2
exit 1
fi
echo "=== Building PWA (BASE_PATH=/physarum/) ==="
npm ci
BASE_PATH=/physarum/ npm run build
echo ""
echo "=== Building embeddable player bundle ==="
npm run build:element
cp dist-player/physarum-player.js dist/physarum-player.js
cp dist-player/physarum-player.js.map dist/physarum-player.js.map
if [[ ! -f dist/index.html ]]; then
echo "ERROR: dist/index.html missing after build." >&2
exit 1
fi
echo ""
echo "=== Publishing dist/ via rsync ==="
"$RSYNC" -az --delete --chmod=D755,F644 dist/ "$DEST"
echo ""
echo "✓ Deployed. Live: https://pages.jkaindl.de/physarum/"