#!/usr/bin/env bash
# 04_code_to_vps_local.sh — clone fleet-critical repos from GitHub to VPS local disk.
#
# Runs on VPS. Does NOT depend on phe being reachable — GitHub is canonical source.
# But you should have run through the manifest in Step 2 of POST_PHE_RECOVERY_CHECKLIST.md
# so this list is accurate.
#
# Convention: each repo lives at /data/cameron/repos/<name>/ (matching what the
# old sshfs mount path used to serve, so agent scripts referencing /data/cameron/repos/*
# keep working with no path changes).

set -euo pipefail

DEST_ROOT="/data/cameron/repos"
mkdir -p "$DEST_ROOT"

# Format: "<name> <github_url>"
# Add / remove based on Step 2 manifest + Cameron's call.
MIRROR_LIST=(
  "custom_robot_building git@github.com:cameronosmith/custom_robot_building.git"
  "text-to-cad           git@github.com:earthtojake/text-to-cad.git"
  # TBD — Cameron confirms these before running:
  # "para                  git@github.com:cameronosmith/para.git"          # may need sparse checkout
  # "raiden_fork           git@github.com:cameronosmith/raiden_fork.git"   # confirm name
  # ... add more from lab_repos_manifest.tsv
)

echo "==> Cloning ${#MIRROR_LIST[@]} repos to $DEST_ROOT (source: GitHub)"

for entry in "${MIRROR_LIST[@]}"; do
  name=$(echo "$entry" | awk '{print $1}')
  url=$(echo "$entry" | awk '{print $2}')
  dest="$DEST_ROOT/$name"

  if [[ -d "$dest/.git" ]]; then
    echo "[skip] $name already cloned at $dest — pulling latest"
    git -C "$dest" pull --ff-only 2>&1 | tail -3
    continue
  fi

  if [[ -e "$dest" ]]; then
    echo "[warn] $dest exists but isn't a git repo — skipping"
    continue
  fi

  echo "[clone] $name from $url"
  git clone "$url" "$dest" 2>&1 | tail -3
done

echo
echo "==> Done. Verify each with:"
echo "     for d in $DEST_ROOT/*/; do echo \$d; git -C \$d remote get-url origin; done"
