#!/bin/bash
# Backup Script - Run before computer reset
# Backs up cron jobs, launchd services, configs, and tracking data

BACKUP_DIR="$HOME/.clawdbot/workspace/backup-before-reset-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP_DIR"

echo "=========================================="
echo "BACKUP SCRIPT FOR COMPUTER RESET"
echo "=========================================="
echo "Backup location: $BACKUP_DIR"
echo ""

# 1. Backup crontab
echo "[1/7] Backing up crontab..."
crontab -l > "$BACKUP_DIR/crontab-backup.txt"
echo "   ✓ Saved $(wc -l < "$BACKUP_DIR/crontab-backup.txt") cron jobs"

# 2. Backup launchd services
echo "[2/7] Backing up launchd services..."
mkdir -p "$BACKUP_DIR/launchd"
cp ~/Library/LaunchAgents/com.jakeshore.remix-sniper.plist "$BACKUP_DIR/launchd/" 2>/dev/null
echo "   ✓ Saved launchd plist files"

# 3. Backup PostgreSQL database
echo "[3/7] Backing up PostgreSQL database..."
/opt/homebrew/opt/postgresql@16/bin/pg_dump -d remix_sniper > "$BACKUP_DIR/remix_sniper-db.sql" 2>/dev/null
echo "   ✓ Saved database dump ($(wc -l < "$BACKUP_DIR/remix_sniper-db.sql") lines)"

# 4. Backup Remix Sniper tracking data
echo "[4/7] Backing up Remix Sniper tracking data..."
mkdir -p "$BACKUP_DIR/remix-sniper/tracking"
cp -R ~/.remix-sniper/* "$BACKUP_DIR/remix-sniper/" 2>/dev/null
echo "   ✓ Saved tracking data ($(find "$BACKUP_DIR/remix-sniper" -type f | wc -l) files)"

# 5. Backup environment files
echo "[5/7] Backing up environment files..."
mkdir -p "$BACKUP_DIR/env-files"
cp ~/projects/remix-sniper/.env "$BACKUP_DIR/env-files/" 2>/dev/null
echo "   ✓ Saved .env file (sensitive data)"

# 6. Backup Clawdbot workspace
echo "[6/7] Backing up Clawdbot workspace..."
mkdir -p "$BACKUP_DIR/clawdbot-workspace"
cp -R ~/.clawdbot/workspace/* "$BACKUP_DIR/clawdbot-workspace/" 2>/dev/null
echo "   ✓ Saved workspace ($(find "$BACKUP_DIR/clawdbot-workspace" -type f | wc -l) files)"

# 7. Backup scripts
echo "[7/7] Backing up custom scripts..."
mkdir -p "$BACKUP_DIR/scripts"
cp ~/.clawdbot/workspace/pickle_motivation.sh "$BACKUP_DIR/scripts/" 2>/dev/null
cp ~/.clawdbot/workspace/daily-anus-fact.sh "$BACKUP_DIR/scripts/" 2>/dev/null
echo "   ✓ Saved custom scripts"

# Create checksums
echo ""
echo "Creating file checksums..."
cd "$BACKUP_DIR"
find . -type f -exec shasum {} \; > "$BACKUP_DIR/sha256-checksums.txt"
echo "   ✓ Created checksums"

# Generate summary
echo ""
echo "=========================================="
echo "BACKUP COMPLETE"
echo "=========================================="
echo ""
echo "Backup location: $BACKUP_DIR"
echo ""
echo "Backup contents:"
echo "  - Cron jobs: $(wc -l < "$BACKUP_DIR/crontab-backup.txt") lines"
echo "  - Launchd services: $(ls -1 "$BACKUP_DIR/launchd/" 2>/dev/null | wc -l) files"
echo "  - PostgreSQL dump: $(du -h "$BACKUP_DIR/remix_sniper-db.sql" | cut -f1)"
echo "  - Remix Sniper data: $(du -sh "$BACKUP_DIR/remix-sniper" | cut -f1)"
echo "  - Clawdbot workspace: $(du -sh "$BACKUP_DIR/clawdbot-workspace" | cut -f1)"
echo "  - Environment files: $(ls -1 "$BACKUP_DIR/env-files/" 2>/dev/null | wc -l) files"
echo "  - Custom scripts: $(ls -1 "$BACKUP_DIR/scripts/" 2>/dev/null | wc -l) files"
echo ""
echo "✓ Checksums saved to: $BACKUP_DIR/sha256-checksums.txt"
echo ""
echo "IMPORTANT: Copy this backup to external storage before resetting!"
echo "   Example: rsync -av $BACKUP_DIR /Volumes/ExternalDrive/"
echo ""
