#!/bin/bash

# 欢迎界面
echo "=============================================="
echo " 欢迎使用 Docker 镜像加速配置工具"
echo "=============================================="

# 判断操作系统类型
os_type=$(uname -s)

# 检查并安装必要的依赖
install_dependencies() {
    case "$os_type" in
        Linux)
            if [ -f /etc/openwrt_release ]; then
                # OpenWRT
                if ! opkg list-installed | grep -q jq; then
                    echo "jq 未安装，正在安装..."
                    opkg update
                    opkg install jq
                fi
                if ! opkg list-installed | grep -q curl; then
                    echo "curl 未安装，正在安装..."
                    opkg update
                    opkg install curl
                fi
            else
                # 其他 Linux 发行版
                if ! command -v jq &> /dev/null; then
                    echo "jq 未安装，正在安装..."
                    sudo apt-get update
                    sudo apt-get install -y jq
                fi
                if ! command -v curl &> /dev/null; then
                    echo "curl 未安装，正在安装..."
                    sudo apt-get update
                    sudo apt-get install -y curl
                fi
            fi
            ;;
        Darwin)
            # macOS
            if ! command -v jq &> /dev/null; then
                echo "jq 未安装，正在安装..."
                brew install jq
            fi
            if ! command -v curl &> /dev/null; then
                echo "curl 未安装，正在安装..."
                brew install curl
            fi
            ;;
        *)
            echo "不支持的操作系统: $os_type"
            exit 1
            ;;
    esac
}

# 安装 Docker
install_docker() {
    case "$os_type" in
        Linux)
            if [ -f /etc/openwrt_release ]; then
                # OpenWRT
                if ! opkg list-installed | grep -q docker; then
                    echo "Docker 未安装，正在安装..."
                    opkg update
                    opkg install docker
                fi
            else
                # 其他 Linux 发行版
                if ! command -v docker &> /dev/null; then
                    echo "Docker 未安装，正在安装..."
                    sudo apt-get update
                    sudo apt-get install -y docker.io
                    sudo systemctl start docker
                    sudo systemctl enable docker
                fi
            fi
            ;;
        Darwin)
            # macOS
            if ! command -v docker &> /dev/null; then
                echo "请安装 Docker Desktop for Mac：https://www.docker.com/products/docker-desktop"
                exit 1
            fi
            ;;
        *)
            echo "不支持的操作系统: $os_type"
            exit 1
            ;;
    esac
}

# 镜像加速器列表
mirrors=(
    "https://docker.melikeme.cn"
    "https://docker.melikeme.de"
)

# 临时文件
temp_file=$(mktemp)

# 函数：测试响应时间
ping_mirror() {
    local mirror=$1
    local result=$(curl -o /dev/null -s -w %{time_total}\\n $mirror)
    echo "$result $mirror" >> $temp_file
}

# 测试每个镜像加速器的响应时间
test_mirrors() {
    for mirror in "${mirrors[@]}"; do
        ping_mirror $mirror &
    done

    # 等待所有测试完成
    wait

    # 找到响应时间最短的前三个镜像加速器
    best_mirrors=$(sort -n $temp_file | head -n 3 | awk '{print $2}')

    # 输出最佳镜像加速器
    echo "最佳镜像加速器是："
    echo "$best_mirrors"

    # 修改 Docker 配置文件
    daemon_file="/etc/docker/daemon.json"
    if [ ! -f $daemon_file ]; then
        echo "{}" | sudo tee $daemon_file
    fi

    # 更新配置文件
    sudo jq ".\"registry-mirrors\" = [$(echo $best_mirrors | sed 's/ /","/g' | sed 's/^/\"/;s/$/\"/')]" $daemon_file > $daemon_file.tmp && sudo mv $daemon_file.tmp $daemon_file

    # 重启 Docker 服务
    case "$os_type" in
        Linux)
            if [ -f /etc/openwrt_release ]; then
                # OpenWRT
                /etc/init.d/dockerd restart
            else
                # 其他 Linux 发行版
                sudo systemctl restart docker
            fi
            ;;
        Darwin)
            # macOS
            sudo brew services restart docker
            ;;
        *)
            echo "不支持的操作系统: $os_type"
            exit 1
            ;;
    esac

    # 清理临时文件
    rm $temp_file

    echo "Docker 镜像加速器已配置为：$best_mirrors"
}

# 交互界面
echo "请选择一个选项："
echo "1) 安装 Docker"
echo "2) 配置最优镜像加速器"

# 检查 Docker 是否安装
if command -v docker &> /dev/null; then
    echo "当前状态: Docker 已安装"
else
    echo "当前状态: Docker 未安装"
fi

# 读取用户输入
read -p "请输入选项 (1/2): " choice

case $choice in
    1)
        if command -v docker &> /dev/null; then
            echo "Docker 已安装，无需再次安装"
        else
            read -p "Docker 未安装，是否安装 Docker？ (y/n): " install_choice
            if [ "$install_choice" = "y" ]; then
                install_docker
            else
                echo "退出安装"
                exit 1
            fi
        fi
        ;;
    2)
        if ! command -v docker &> /dev/null; then
            echo "请先安装 Docker"
            exit 1
        fi
        # 检查并安装必要的依赖
        install_dependencies

        # 配置最优镜像
        read -p "是否配置最优镜像加速器？ (y/n): " config_choice
        if [ "$config_choice" = "y" ]; then
            test_mirrors
        else
            echo "未配置镜像加速器"
            exit 0
        fi
        ;;
    *)
        echo "无效选项"
        exit 1
        ;;
esac
