#!/bin/bash
# GOAL Fleet Backup Script (Mac/Linux)
# Run this in Terminal to back up the GOAL folder to a USB drive
#
# USAGE:
#   ./backup_goal.sh                          # backs up to ~/GOAL-Backup/
#   ./backup_goal.sh /Volumes/USBDRIVE/        # backs up to USB drive
#
# You only need to run this ONCE after everything is working.
# It copies ~/GOAL/ (the algorithm, state, constitution, config) to a backup location.
# If a machine breaks, you copy the backup back and run the bootstrap command.

DEST="${1:-$HOME/GOAL-Backup}"
SOURCE="$HOME/GOAL"

if [ ! -d "$SOURCE" ]; then
    echo "ERROR: ~/GOAL/ not found. Install the algorithm first."
    echo "Run: curl -sL https://goalinvestment.biz/downloads/bootstrap.sh | bash"
    exit 1
fi

echo "Backing up $SOURCE to $DEST..."

if [ -d "$DEST" ]; then
    STAMP=$(date +%Y%m%d_%H%M%S)
    DEST="$DEST-$STAMP"
    echo "  Backup exists. Creating timestamped copy: $DEST"
fi

cp -r "$SOURCE" "$DEST"

echo ""
echo "BACKUP COMPLETE"
echo "  Source: $SOURCE"
echo "  Backup: $DEST"
echo ""
echo "To restore: Copy the backup folder back to ~/GOAL/ then run:"
echo "  curl -sL https://goalinvestment.biz/downloads/bootstrap.sh | bash"
echo ""
