#!/usr/bin/env bash
# Build the Zola site and sync `public/` to the Variomedia staging host.
#
# Usage:
#   scripts/deploy.sh           # build + deploy
#   scripts/deploy.sh --dry-run # show what rsync would do, transfer nothing
#   scripts/deploy.sh --no-build # skip `zola build` (deploy existing public/)

set -euo pipefail

REMOTE="mycelium:~/public/praxis/"
LOCAL="public/"

# Run from the repo root so relative paths are stable.
cd "$(dirname "$0")/.."

DRY_RUN=""
DO_BUILD=1
for arg in "$@"; do
  case "$arg" in
    --dry-run)  DRY_RUN="--dry-run" ;;
    --no-build) DO_BUILD=0 ;;
    -h|--help)
      sed -n '2,8p' "$0"
      exit 0 ;;
    *)
      echo "unknown flag: $arg" >&2
      exit 2 ;;
  esac
done

if [[ "$DO_BUILD" -eq 1 ]]; then
  echo "==> zola build"
  zola build
fi

if [[ ! -d "$LOCAL" ]]; then
  echo "error: $LOCAL does not exist — run without --no-build first" >&2
  exit 1
fi

echo "==> rsync $LOCAL -> $REMOTE${DRY_RUN:+  (dry-run)}"
rsync -avz --delete $DRY_RUN "$LOCAL" "$REMOTE"

echo "==> done"
