#!/bin/bash
# Workspace Backup Script
# Backs up the workspace to GitHub

set -e

WORKSPACE_DIR="~/.openclaw/workspace"
BACKUP_LOG="~/.openclaw/workspace/.backup.log"

echo "=== Backup started: $(date) ===" >> "$BACKUP_LOG"

cd "$WORKSPACE_DIR"

# Check for changes
if git diff --quiet && git diff --cached --quiet; then
    echo "No changes to backup." >> "$BACKUP_LOG"
    echo "=== Backup completed (no changes): $(date) ===" >> "$BACKUP_LOG"
    exit 0
fi

# Stage and commit
git add -A
git commit -m "Auto-backup: $(date '+%Y-%m-%d %H:%M:%S')" >> "$BACKUP_LOG" 2>&1 || true

# Push to remote
git push origin master >> "$BACKUP_LOG" 2>&1 || {
    echo "Push failed. Check credentials." >> "$BACKUP_LOG"
    exit 1
}

echo "=== Backup completed successfully: $(date) ===" >> "$BACKUP_LOG"