#!/bin/bash
# Cloud Backup Script - Backs up all critical data to cloud storage
# Uses rclone to sync to configured cloud provider

set -e

BACKUP_DIR="$HOME/.clawdbot/workspace/backup-cloud-$(date +%Y%m%d-%H%M%S)"
REMOTE_NAME="${1:-remix-backup}"  # rclone remote name
REMOTE_DIR="${2:-remix-sniper-backup}"  # Remote directory

mkdir -p "$BACKUP_DIR"

echo "=========================================="
echo "CLOUD BACKUP SCRIPT"
echo "=========================================="
echo "Backup location: $BACKUP_DIR"
echo "Cloud target: $REMOTE_NAME:$REMOTE_DIR"
echo ""

# Check if remote exists
echo "Checking cloud remote..."
if ! rclone listremotes 2>/dev/null | grep -q "^$REMOTE_NAME:"; then
  echo "ERROR: Remote '$REMOTE_NAME:' not configured"
  echo ""
  echo "To set up cloud storage:"
  echo "  Google Drive: rclone config create gdrive drive"
  echo "  Dropbox:     rclone config create dropbox dropbox"
  echo "  S3/DO Space: rclone config create s3 s3"
  echo "  OneDrive:    rclone config create onedrive onedrive"
  echo ""
  echo "Then run this script with: $0 <remote-name>"
  echo ""
  echo "See: https://rclone.org/ for full setup instructions"
  exit 1
fi

echo "   ✓ Found remote: $REMOTE_NAME:"
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"

# 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"

# Create manifest
cat > "$BACKUP_DIR/MANIFEST.txt" <<EOF
Remix Sniper Cloud Backup
Generated: $(date)

Contents:
- Cron jobs: $(wc -l < "$BACKUP_DIR/crontab-backup.txt") lines
- Launchd services: $(ls -1 "$BACKUP_DIR/launchd/" 2>/dev/null | wc -l) files
- PostgreSQL dump: $(du -h "$BACKUP_DIR/remix_sniper-db.sql" | cut -f1)
- Remix Sniper data: $(du -sh "$BACKUP_DIR/remix-sniper" | cut -f1)
- Clawdbot workspace: $(du -sh "$BACKUP_DIR/clawdbot-workspace" | cut -f1)
- Environment files: $(ls -1 "$BACKUP_DIR/env-files/" 2>/dev/null | wc -l) files
- Custom scripts: $(ls -1 "$BACKUP_DIR/scripts/" 2>/dev/null | wc -l) files
EOF

echo ""

# Upload to cloud
echo "=========================================="
echo "UPLOADING TO CLOUD"
echo "=========================================="
echo "Remote: $REMOTE_NAME:$REMOTE_DIR/$(basename "$BACKUP_DIR")"
echo ""

# Create remote directory if needed
echo "Creating remote directory..."
rclone mkdir "$REMOTE_NAME:$REMOTE_DIR/$(basename "$BACKUP_DIR")" 2>/dev/null || true

# Upload files
echo "Uploading files..."
rclone sync "$BACKUP_DIR/" "$REMOTE_NAME:$REMOTE_DIR/$(basename "$BACKUP_DIR")/" \
  --progress \
  --transfers 4 \
  --exclude ".DS_Store" \
  --exclude "._*" \
  --exclude "*.pyc" \
  --exclude "__pycache__/" \
  --exclude ".venv/" \
  --exclude "node_modules/"

echo ""
echo "=========================================="
echo "BACKUP COMPLETE"
echo "=========================================="
echo ""
echo "Local backup: $BACKUP_DIR"
echo "Cloud backup: $REMOTE_NAME:$REMOTE_DIR/$(basename "$BACKUP_DIR")/"
echo ""
echo "To restore from cloud:"
echo "  rclone sync $REMOTE_NAME:$REMOTE_DIR/$(basename "$BACKUP_DIR")/ ~/.clawdbot/workspace/restore-from-cloud/"
echo ""
echo "To list cloud backups:"
echo "  rclone ls $REMOTE_NAME:$REMOTE_DIR/"
echo ""
