#!/usr/bin/env bash
# 07_vault_memory_github_remote.sh — back vault + memory to GitHub, retiring
# the long-standing `project_migrate_offlab_pending` risk.
#
# Both are small (< 100MB combined). Push to private GitHub repos so a VPS wipe
# doesn't lose them.
#
# MANUAL STEPS FIRST (Cameron does these):
#   1. Create private GitHub repo `cameronosmith/vault` (or reuse existing)
#   2. Create private GitHub repo `cameronosmith/manager_memory`
#
# Then this script wires up the remotes + initial push.

set -euo pipefail

VAULT_DIR="/data/cameron/vault"
MEMORY_DIR="$HOME/.claude/projects/-data-cameron-agents-stuff/memory"

VAULT_REMOTE="git@github.com:cameronosmith/vault.git"
MEMORY_REMOTE="git@github.com:cameronosmith/manager_memory.git"

# --- Vault ---
if [[ -d "$VAULT_DIR/.git" ]]; then
  cd "$VAULT_DIR"
  if git remote get-url origin > /dev/null 2>&1; then
    echo "[vault] origin already set to: $(git remote get-url origin)"
  else
    echo "[vault] adding origin $VAULT_REMOTE"
    git remote add origin "$VAULT_REMOTE"
  fi
  echo "[vault] initial push (may take a moment)"
  git add -A
  git commit -m "vault: initial commit (backed to GitHub after phe recovery)" 2>&1 || echo "  (nothing to commit)"
  git branch -M main 2>&1 || true
  git push -u origin main
else
  echo "[vault] no .git dir — initializing"
  cd "$VAULT_DIR"
  git init -b main
  git add -A
  git commit -m "vault: initial commit"
  git remote add origin "$VAULT_REMOTE"
  git push -u origin main
fi

# --- Memory ---
if [[ -d "$MEMORY_DIR/.git" ]]; then
  cd "$MEMORY_DIR"
  echo "[memory] .git already exists — assuming already wired"
else
  echo "[memory] initializing new git repo"
  cd "$MEMORY_DIR"
  git init -b main
  git add -A
  git commit -m "memory: initial commit (manager memory backed to GitHub after phe recovery)"
  git remote add origin "$MEMORY_REMOTE"
  git push -u origin main
fi

echo
echo "==> Vault + memory now backed to GitHub."
echo "==> Consider adding a daily cron:"
echo "    0 4 * * *  cd $VAULT_DIR    && git add -A && git commit -m auto && git push"
echo "    5 4 * * *  cd $MEMORY_DIR   && git add -A && git commit -m auto && git push"
