#!/usr/bin/env bash
# =============================================================================
# JARO Ops Platform — deploy from this Mac to the AWS Lightsail server.
#
#   ./deploy/deploy.sh <server-ip-or-host>
#
# Replaces the old Bluehost cPanel manual upload. Rsyncs the ops/ app to the
# server, EXCLUDING:
#   - ops/uploads/  (live estimate documents — never overwritten or deleted)
#   - anything in .git, .DS_Store
# Safe to run as often as you like; only changed files transfer.
# Requires: the Lightsail SSH key loaded (ssh ubuntu@<IP> must work).
# =============================================================================
set -euo pipefail

HOST="${1:-}"
if [ -z "$HOST" ]; then echo "Usage: ./deploy/deploy.sh <server-ip-or-host>"; exit 1; fi

REPO="$(cd "$(dirname "$0")/.." && pwd)"
DEST="ubuntu@${HOST}"
WEBROOT="/var/www/jarocon/public"

echo "== Deploying ops/ -> ${DEST}:${WEBROOT}/ops =="
rsync -avz --delete \
  --exclude '.DS_Store' \
  --exclude '.git' \
  --filter 'protect uploads/***' \
  --exclude 'uploads/***' \
  --rsync-path 'sudo -u www-data rsync' \
  "${REPO}/ops/" "${DEST}:${WEBROOT}/ops/"

echo "== Sanity check =="
ssh "$DEST" "php -l ${WEBROOT}/ops/estimating/estimate.php >/dev/null && echo 'PHP OK' && curl -s -o /dev/null -w 'HTTP %{http_code}\n' http://localhost/ops/estimating/login.php"

echo "Done. Spot-check the site in your browser."
