#!/usr/bin/env bash
# Run ON THE LAB BOX as cameronsmith after step 01 completes on the VPS.
# Rsyncs everything the fleet needs to its mirror paths on the VPS.
# COPIES — does not delete from lab. Safe to re-run.
set -euo pipefail

VPS_HOST="${VPS_HOST:-cameronsmith@5.78.127.30}"

echo "=== Sanity: VPS reachable as cameronsmith? ==="
ssh -o ConnectTimeout=8 -o BatchMode=yes "$VPS_HOST" 'id; whoami' || {
  echo "Can't SSH as cameronsmith yet. Make sure 01_vps_bootstrap.sh ran (creates user + installs lab's pubkey)."
  exit 1
}

# 1) agents_stuff (excluding things that shouldn't move yet)
echo "=== rsync agents_stuff/ (the fleet code + per-agent dirs) ==="
rsync -avz --human-readable \
  --exclude '.git/' \
  --exclude '__pycache__' \
  --exclude '*.pyc' \
  --exclude 'agents/*/scratch/' \
  /data/cameron/agents_stuff/ \
  "$VPS_HOST:/data/cameron/agents_stuff/"

# 2) vault (everything — small)
echo "=== rsync vault/ ==="
rsync -avz --human-readable \
  --exclude '.git/' \
  /data/cameron/vault/ \
  "$VPS_HOST:/data/cameron/vault/"

# 3) memory (manager's persistent memory)
echo "=== rsync ~/.claude/projects/.../memory/ ==="
ssh "$VPS_HOST" 'install -d -m 700 -o cameronsmith /home/cameronsmith/.claude/projects/-data-cameron-agents-stuff'
rsync -avz --human-readable \
  /home/cameronsmith/.claude/projects/-data-cameron-agents-stuff/memory/ \
  "$VPS_HOST:/home/cameronsmith/.claude/projects/-data-cameron-agents-stuff/memory/"

# 4) Claude session JSONLs (the actual conversations — magic file)
echo "=== rsync Claude session JSONLs (~/.claude/projects/) ==="
rsync -avz --human-readable \
  --include '*.jsonl' \
  --include '*/' \
  --exclude '*' \
  /home/cameronsmith/.claude/projects/ \
  "$VPS_HOST:/home/cameronsmith/.claude/projects/"

# 5) cloudflared cert + config
echo "=== rsync cloudflared (~/.cloudflared/) ==="
ssh "$VPS_HOST" 'install -d -m 700 -o cameronsmith /home/cameronsmith/.cloudflared'
rsync -avz --human-readable \
  /home/cameronsmith/.cloudflared/ \
  "$VPS_HOST:/home/cameronsmith/.cloudflared/"

# 6) reports (serve.py + UI + scrollback dir) -- skip the large media/transcoded files
echo "=== rsync para/.agents/reports/ (serve.py + UI) ==="
rsync -avz --human-readable \
  --exclude '__pycache__' \
  --exclude '_thumb_cache/' \
  --exclude '*.mp4' \
  --exclude '*.webm' \
  --exclude 'media/' \
  /data/cameron/para/.agents/reports/ \
  "$VPS_HOST:/data/cameron/para/.agents/reports/"

# 7) shared/ scripts are inside agents_stuff already; no extra rsync needed.

# 8) SSH config (so VPS can ssh into puget/yukon/mac with same Host aliases)
echo "=== rsync ~/.ssh/config (paths only — keys handled separately) ==="
rsync -avz \
  /home/cameronsmith/.ssh/config \
  "$VPS_HOST:/home/cameronsmith/.ssh/config"
ssh "$VPS_HOST" 'chmod 600 /home/cameronsmith/.ssh/config'

echo
echo "=== DONE rsync. Now run 03_vps_post_setup.sh on the VPS (as cameronsmith). ==="
