#!/bin/bash

# 本地备份系统 - 临时解决方案
# 解决远程仓库不存在时的数据同步问题

set -e

echo "🚀 启动本地Git备份系统"
echo "==============================="

WORKSPACE_DIR="/root/.openclaw/workspace"
cd "$WORKSPACE_DIR"

BACKUP_DIR="$WORKSPACE_DIR/git-backups"
mkdir -p "$BACKUP_DIR"

# 1. 创建本地备份
BACKUP_NAME="git-backup-$(date '+%Y%m%d_%H%M%S')"
BACKUP_FILE="$BACKUP_DIR/$BACKUP_NAME.tar.gz"

echo "📦 1. 创建本地备份..."
git archive --format=tar.gz --output="$BACKUP_FILE" HEAD

BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
echo "   ✅ 备份完成: $BACKUP_NAME.tar.gz ($BACKUP_SIZE)"

# 2. 导出当前状态
STATUS_FILE="$BACKUP_DIR/git-status-$(date '+%Y%m%d_%H%M%S').txt"
echo "📋 2. 导出Git状态..."
{
    echo "=== Git 状态报告 $(date) ==="
    echo ""
    echo "分支状态:"
    git branch -vv
    echo ""
    echo "提交历史:"
    git log --oneline -10
    echo ""
    echo "未提交的变更:"
    git status --short
    echo ""
    echo "远程仓库配置:"
    git remote -v
} > "$STATUS_FILE"

echo "   ✅ 状态报告: $(basename "$STATUS_FILE")"

# 3. 创建离线同步包
SYNC_PACKAGE="$BACKUP_DIR/sync-package-$(date '+%Y%m%d_%H%M%S').tar.gz"
echo "📁 3. 创建离线同步包..."
tar -czf "$SYNC_PACKAGE" \
    --exclude="*.pyc" \
    --exclude="__pycache__" \
    --exclude="node_modules" \
    --exclude="*.log" \
    --exclude="*.tmp" \
    --exclude="backups" \
    .

SYNC_SIZE=$(du -h "$SYNC_PACKAGE" | cut -f1)
echo "   ✅ 同步包: $(basename "$SYNC_PACKAGE") ($SYNC_SIZE)"

# 4. 创建修复脚本
FIX_SCRIPT="$BACKUP_DIR/restore-git.sh"
cat > "$FIX_SCRIPT" << 'EOF'
#!/bin/bash
# Git恢复脚本

echo "🔧 Git恢复工具"
echo "================"

# 选项菜单
echo "请选择恢复选项:"
echo "1. 恢复完整备份"
echo "2. 恢复Git仓库"
echo "3. 查看备份列表"
echo "4. 检查系统状态"
echo "5. 创建远程仓库"
read -p "选择 (1-5): " choice

case $choice in
    1)
        echo "恢复完整备份..."
        BACKUP_FILE=$(ls -t git-backup-*.tar.gz | head -1)
        if [ -f "$BACKUP_FILE" ]; then
            echo "恢复: $BACKUP_FILE"
            tar -xzf "$BACKUP_FILE" -C /tmp/
            echo "✅ 备份已解压到 /tmp/"
        else
            echo "❌ 找不到备份文件"
        fi
        ;;
    2)
        echo "恢复Git仓库..."
        git init
        git remote add origin https://gitea.nevadalice.top:226/liyuchen/tomato-novel-workspace.git
        echo "✅ Git仓库已初始化"
        echo "📝 下一步: git fetch origin"
        ;;
    3)
        echo "备份列表:"
        ls -lh git-backup-*.tar.gz
        ls -lh git-status-*.txt
        ls -lh sync-package-*.tar.gz
        ;;
    4)
        echo "系统状态:"
        git status
        git remote -v
        git log --oneline -5
        ;;
    5)
        echo "创建远程仓库:"
        echo "1. 访问 https://gitea.nevadalice.top:226"
        echo "2. 登录账号"
        echo "3. 点击'新建仓库'"
        echo "4. 名称: tomato-novel-workspace"
        echo "5. 描述: 番茄小说创作工作区"
        echo "6. 点击'创建仓库'"
        echo ""
        echo "然后执行:"
        echo "git remote add origin https://gitea.nevadalice.top:226/liyuchen/tomato-novel-workspace.git"
        echo "git push -u origin master"
        ;;
    *)
        echo "❌ 无效选择"
        ;;
esac
EOF

chmod +x "$FIX_SCRIPT"

# 5. 清理旧备份（保留最近7天）
echo "🧹 4. 清理旧备份..."
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +7 -delete
find "$BACKUP_DIR" -name "*.txt" -mtime +7 -delete
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +7 -delete

echo ""
echo "📊 备份统计:"
echo "   本地备份: $(ls $BACKUP_DIR/git-backup-*.tar.gz 2>/dev/null | wc -l) 个"
echo "   状态报告: $(ls $BACKUP_DIR/git-status-*.txt 2>/dev/null | wc -l) 个"
echo "   同步包: $(ls $BACKUP_DIR/sync-package-*.tar.gz 2>/dev/null | wc -l) 个"

echo ""
echo "🎉 本地备份系统就绪！"
echo ""
echo "📁 备份目录: $BACKUP_DIR"
echo "🔧 恢复工具: $FIX_SCRIPT"
echo ""
echo "🚀 下一步操作:"
echo "   1. 检查备份: ls -lh $BACKUP_DIR/"
echo "   2. 手动创建远程仓库（推荐）"
echo "   3. 或使用本地备份系统保证数据安全"