#!/bin/bash
# Restore OpenClaw from Gitea backup
# Usage: ./restore-from-gitea.sh

set -e

REPO_DIR="$HOME/openclaw-backup"
REPO_URL="http://gitea.kangaroo-eel.ts.net:3000/Anthony/openclaw-backup.git"
STATE_DIR="$HOME/.openclaw"
WORKSPACE_DIR="$HOME/.openclaw/workspace"

echo "=== OpenClaw Restore Script ==="
echo ""

# Clone or update repo
if [ -d "$REPO_DIR" ]; then
    echo "Updating existing backup repo..."
    cd "$REPO_DIR"
    git pull
else
    echo "Cloning backup repo..."
    git clone "$REPO_URL" "$REPO_DIR"
    cd "$REPO_DIR"
fi

# Restore state directory
if [ -d "$REPO_DIR/state" ]; then
    echo "Restoring state directory..."
    rsync -av "$REPO_DIR/state/" "$STATE_DIR/"
else
    echo "Warning: No state directory in backup"
fi

# Restore workspace (optional - may not want to overwrite)
echo ""
echo "Workspace restore is optional. Current workspace may have newer files."
read -p "Restore workspace too? (y/N): " answer
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
    echo "Restoring workspace..."
    rsync -av "$REPO_DIR/workspace/" "$WORKSPACE_DIR/"
fi

echo ""
echo "=== Restore Complete ==="
echo "You may need to restart the gateway: openclaw gateway restart"
