#!/usr/bin/env bash
# Run ON THE VPS as cameronsmith AFTER 02_rsync_from_lab.sh completes.
# Mounts lab's large data via sshfs at matching paths, registers cloudflared,
# preps but does NOT start the fleet. resume_fleet.sh stays manual until Cameron flips the switch.
set -euo pipefail

LAB_HOST="cameronsmith@100.74.71.38"  # Lab box's Tailscale IP — only reachable from tailnet members

echo "=== 1. sshfs key — VPS → lab ==="
# This needs the VPS's outbound pubkey (cat ~/.ssh/id_ed25519.pub on VPS) in lab's authorized_keys.
# Cameron should run, on the lab box: cat >> ~/.ssh/authorized_keys <<< 'VPS-PUBKEY-HERE'
ssh -o BatchMode=yes -o ConnectTimeout=10 "$LAB_HOST" 'echo lab reachable' || {
  echo "VPS can't ssh to lab yet. Add VPS's pubkey (~/.ssh/id_ed25519.pub on VPS) to lab's authorized_keys, then re-run."
  exit 1
}

echo "=== 2. sshfs mounts (lab's large/data-y dirs at matching paths) ==="
# Mount points were created in 01_vps_bootstrap.sh.
# Strategy: mount the SUBDIRS that hold data, NOT all of /data/cameron (which would overlay our fleet files).
SSHFS_OPTS="reconnect,ServerAliveInterval=15,ServerAliveCountMax=3,follow_symlinks,cache=yes,kernel_cache,compression=no,uid=1011,gid=1011"

PUGET_HOST="cameronsmith@100.104.232.94"  # puget-232243-01
declare -A MOUNTS=(
  ["/data/cameron/para/libero"]="$LAB_HOST:/data/cameron/para/libero"
  ["/data/cameron/para/robot"]="$LAB_HOST:/data/cameron/para/robot"
  ["/data/cameron/para/panda_streaming"]="$LAB_HOST:/data/cameron/para/panda_streaming"
  ["/data/cameron/para/video_training"]="$LAB_HOST:/data/cameron/para/video_training"
  ["/data/cameron/para/para_mac"]="$LAB_HOST:/data/cameron/para/para_mac"
  ["/data/cameron/repos"]="$LAB_HOST:/data/cameron/repos"
  ["/data/cameron/mac_robot_datasets"]="$LAB_HOST:/data/cameron/mac_robot_datasets"
  ["/data/cameron/yam_remote"]="$LAB_HOST:/data/cameron/yam_remote"
  ["/data/cameron/yukon_remote"]="$LAB_HOST:/data/cameron/yukon_remote"
  # scratch_files moved to puget 2026-07-09 (was lab). serve.py's /upload lands here.
  ["/data/cameron/scratch_files"]="$PUGET_HOST:/home/cameronsmith/scratch_files"
)
for mp in "${!MOUNTS[@]}"; do
  mkdir -p "$mp"
  if mountpoint -q "$mp"; then
    echo "[skip] $mp already mounted"
    continue
  fi
  echo "[mount] ${MOUNTS[$mp]} -> $mp"
  sshfs "${MOUNTS[$mp]}" "$mp" -o "$SSHFS_OPTS" || echo "  WARN: mount failed; continuing"
done

echo "=== 3. systemd auto-remount on reboot ==="
# Write a small systemd service so mounts come back after reboot.
sudo tee /etc/systemd/system/fleet-mounts.service >/dev/null <<EOF
[Unit]
Description=Mount lab box's data dirs via sshfs
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
User=cameronsmith
Group=cameronsmith
ExecStart=/data/cameron/agents_stuff/migration/03_vps_post_setup.sh --mounts-only
TimeoutSec=120

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable fleet-mounts.service

if [[ "${1:-}" == "--mounts-only" ]]; then exit 0; fi

echo "=== 4. cloudflared — install as a service ==="
# The credentials and config.yml were rsync'd to ~/.cloudflared.
# Register the tunnel as a systemd service that runs as cameronsmith.
if [[ ! -f /etc/systemd/system/cloudflared-omidlab.service ]]; then
  sudo tee /etc/systemd/system/cloudflared-omidlab.service >/dev/null <<EOF
[Unit]
Description=Cloudflared tunnel for omidlab.net
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=cameronsmith
ExecStart=/usr/local/bin/cloudflared tunnel --config /home/cameronsmith/.cloudflared/config.yml run 350655a2-4ba4-4a88-beb5-405192dcbff3
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
  sudo systemctl daemon-reload
fi
# DO NOT start the service yet — that would steal the tunnel from the lab box (split brain).
# Cameron will start it during cutover.
echo "cloudflared service registered but NOT started. Start during cutover with:"
echo "  sudo systemctl start cloudflared-omidlab"

echo "=== 5. Tmux session 'agents' scaffolding ==="
# Create the session and its windows in a paused state — no Claude processes started.
if ! tmux has-session -t agents 2>/dev/null; then
  tmux new-session -d -s agents -n manager
  for w in yams project_highlevel backbones glasses yam_yukon life_manager paper_writer manager_alt website_builder figure_maker mobile_chat puget 567_project mac voice_interface our_wandb cf-omidlab; do
    tmux new-window -t agents -n "$w"
  done
  echo "tmux session 'agents' created with $(tmux list-windows -t agents | wc -l) windows. NOT running claude in them yet."
else
  echo "tmux session 'agents' already exists — leaving alone."
fi

echo
echo "=== DONE post-setup. The VPS is READY but the fleet is NOT STARTED. ==="
echo "Cutover steps (when Cameron is ready):"
echo "  1. On lab: stop cloudflared (tmux send-keys -t agents:cf-omidlab C-c)"
echo "  2. On VPS: sudo systemctl start cloudflared-omidlab"
echo "  3. On VPS: cd /data/cameron/agents_stuff && ./resume_fleet.sh"
echo "  4. (optional, later) Disable password ssh on VPS — see 01 script output."
