#!/bin/bash

# 容器名称列表
containers=("v2b1" "v2p1" "v2f1")

# 脚本1和脚本2的下载链接
script1_url="http://df.owncds.com/d-go.sh"
script2_url="http://df.owncds.com/d-install-dat-release.sh"

# 脚本1和脚本2的本地存储路径
local_script1_path="/tmp/d-go.sh"
local_script2_path="/tmp/d-install-dat-release.sh"

# 显示菜单并获取用户选择
show_menu() {
    echo "请选择更新模式:"
    echo "1. 全部更新 (执行 d-go.sh 和 d-install-dat-release.sh)"
    echo "2. 规则更新 (只执行 d-install-dat-release.sh)"
    echo "3. 退出"
    echo
    read -p "请输入选择 (1-3): " choice
}

# 下载脚本函数
download_scripts() {
    local mode=$1
    
    if [ "$mode" == "full" ] || [ "$mode" == "rules" ]; then
        echo "下载脚本2到本地..."
        curl "$script2_url" -o "$local_script2_path" || { echo "无法下载脚本2"; exit 1; }
        chmod +x "$local_script2_path"
    fi
    
    if [ "$mode" == "full" ]; then
        echo "下载脚本1到本地..."
        curl "$script1_url" -o "$local_script1_path" || { echo "无法下载脚本1"; exit 1; }
        chmod +x "$local_script1_path"
    fi
}

# 执行全部更新
execute_full_update() {
    local container=$1
    
    echo "=== 执行全部更新模式 ==="
    
    # 安装 unzip（如果未安装）
    echo "检查并安装 unzip..."
    docker exec "$container" sh -c "command -v unzip >/dev/null 2>&1 || apt-get update && apt-get install -y unzip" || {
        echo "无法安装 unzip，尝试使用 apk (Alpine)..."
        docker exec "$container" sh -c "apk add --no-cache unzip" 2>/dev/null || true
    }
    
    # 复制脚本1到容器并执行
    echo "复制脚本1到容器并执行..."
    docker cp "$local_script1_path" "$container:/root/d-go.sh"
    docker exec "$container" sh -c "/root/d-go.sh --version 4.45.2" || { 
        echo "无法执行脚本1在容器 $container"; 
        return 1; 
    }
    
    # 复制脚本2到容器并执行
    echo "复制脚本2到容器并执行..."
    docker cp "$local_script2_path" "$container:/root/d-install-dat-release.sh"
    docker exec "$container" sh -c "/root/d-install-dat-release.sh" || { 
        echo "无法执行脚本2在容器 $container"; 
        return 1; 
    }
    
    return 0
}

# 执行规则更新
execute_rules_update() {
    local container=$1
    
    echo "=== 执行规则更新模式 ==="
    
    # 只复制脚本2到容器并执行
    echo "复制脚本2到容器并执行..."
    docker cp "$local_script2_path" "$container:/root/d-install-dat-release.sh"
    docker exec "$container" sh -c "/root/d-install-dat-release.sh" || { 
        echo "无法执行脚本2在容器 $container"; 
        return 1; 
    }
    
    return 0
}

# 处理容器更新
process_containers() {
    local mode=$1
    
    echo "开始处理容器列表..."
    echo "更新模式: $mode"
    echo
    
    # 遍历容器列表
    for container in "${containers[@]}"; do
        echo "检查容器: $container"
        
        # 检查容器是否存在并运行
        if [ "$(docker ps -q -f name=^/${container}$)" ]; then
            echo "容器 $container 存在且正在运行，开始执行脚本..."
            
            # 根据模式执行相应的更新
            if [ "$mode" == "full" ]; then
                if execute_full_update "$container"; then
                    echo "容器 $container 全部更新完成"
                else
                    echo "容器 $container 全部更新失败，跳过重启"
                    echo
                    continue
                fi
            elif [ "$mode" == "rules" ]; then
                if execute_rules_update "$container"; then
                    echo "容器 $container 规则更新完成"
                else
                    echo "容器 $container 规则更新失败，跳过重启"
                    echo
                    continue
                fi
            fi
            
            # 重启容器
            echo "重启容器: $container"
            docker restart "$container"
            echo "容器 $container 重启完成"
            
        else
            echo "容器 $container 不存在或未运行，跳过此容器。"
        fi
        echo
    done
}

# 主函数
main() {
    echo "容器批量更新脚本"
    echo "支持的容器: ${containers[*]}"
    echo "==================="
    echo
    
    while true; do
        show_menu
        
        case $choice in
            1)
                echo "选择了全部更新模式"
                download_scripts "full"
                process_containers "full"
                break
                ;;
            2)
                echo "选择了规则更新模式"
                download_scripts "rules"
                process_containers "rules"
                break
                ;;
            3)
                echo "退出脚本"
                exit 0
                ;;
            *)
                echo "无效选择，请重新输入"
                echo
                ;;
        esac
    done
    
    echo "脚本执行完毕。"
}

# 执行主函数
main

