#!/bin/bash

echo "=== Discord Tokens 恢复脚本 ==="

# 检查数据分区
echo "1. 检查数据分区状态..."
if [ -d "/mnt/data" ]; then
    echo "✅ /mnt/data 目录存在"
    
    # 尝试访问备份文件
    if [ -f "/mnt/data/docker_backup_20250713_165104/containers/discordbot-v1.0.4_export.tar" ]; then
        echo "✅ 找到Discord备份文件"
        
        # 提取tokens.txt
        echo "2. 提取tokens.txt..."
        tar -xf "/mnt/data/docker_backup_20250713_165104/containers/discordbot-v1.0.4_export.tar" -C /tmp/ app/tokens.txt
        
        if [ -f "/tmp/app/tokens.txt" ]; then
            echo "✅ 成功提取tokens.txt"
            
            # 备份当前文件
            cp /home/will/docker/discord_tokens.txt /home/will/docker/discord_tokens.txt.backup
            
            # 恢复原始tokens
            cp /tmp/app/tokens.txt /home/will/docker/discord_tokens.txt
            
            echo "✅ tokens.txt已恢复！"
            echo "原始文件大小: $(stat -c%s /home/will/docker/discord_tokens.txt) 字节"
            echo "备份文件: /home/will/docker/discord_tokens.txt.backup"
            
            # 重启Discord bot
            echo "3. 重启Discord Bot..."
            docker compose restart discordbot
            
        else
            echo "❌ 无法提取tokens.txt"
        fi
    else
        echo "❌ 未找到备份文件"
        echo "请手动编辑 /home/will/docker/discord_tokens.txt 添加你的Discord bot tokens"
    fi
else
    echo "❌ /mnt/data 目录不存在，数据分区可能未挂载"
    echo "请手动挂载数据分区或编辑tokens文件"
fi

echo ""
echo "=== 手动恢复方法 ==="
echo "如果自动恢复失败，请："
echo "1. 编辑文件: nano /home/will/docker/discord_tokens.txt"
echo "2. 添加你的Discord bot tokens (每行一个)"
echo "3. 重启Discord Bot: docker compose restart discordbot"