#!/bin/bash

set -e

# 检测系统类型
if [ -f /etc/os-release ]; then
    . /etc/os-release
    DISTRO=$ID
    VERSION=$VERSION_ID
else
    echo "无法识别系统类型，退出。"
    exit 1
fi

# 提示用户选择源
echo "请选择你想使用的镜像源："
echo "1) Official"
echo "2) Aliyun"
echo "3) Tencent"
echo "4) Huawei"
echo "5) Tsinghua"
read -p "请输入选项 [1-5]: " choice

# 根据选择设定镜像源地址
case "$choice" in
  1) TYPE="official" ;;
  2) TYPE="aliyun" ;;
  3) TYPE="tencent" ;;
  4) TYPE="huawei" ;;
  5) TYPE="tsinghua" ;;
  *) echo "无效选项，退出。" && exit 1 ;;
esac

# 根据系统类型生成对应源配置
echo "🎯 检测到系统类型: $DISTRO $VERSION"
echo "🚀 正在设置镜像源..."

update_debian_sources() {
    case "$TYPE" in
      official) URL="http://deb.debian.org/debian/" ;;
      aliyun) URL="https://mirrors.aliyun.com/debian/" ;;
      tencent) URL="https://mirrors.cloud.tencent.com/debian/" ;;
      huawei) URL="https://mirrors.huaweicloud.com/debian/" ;;
      tsinghua) URL="https://mirrors.tuna.tsinghua.edu.cn/debian/" ;;
    esac

    cat > /etc/apt/sources.list <<EOF
deb ${URL} bookworm main contrib non-free non-free-firmware
deb ${URL} bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.aliyun.com/debian-security bookworm-security main contrib non-free non-free-firmware
deb ${URL} bookworm-backports main contrib non-free non-free-firmware
EOF
    apt update
}

update_ubuntu_sources() {
    case "$TYPE" in
      official) URL="http://archive.ubuntu.com/ubuntu/" ;;
      aliyun) URL="https://mirrors.aliyun.com/ubuntu/" ;;
      tencent) URL="https://mirrors.cloud.tencent.com/ubuntu/" ;;
      huawei) URL="https://mirrors.huaweicloud.com/ubuntu/" ;;
      tsinghua) URL="https://mirrors.tuna.tsinghua.edu.cn/ubuntu/" ;;
    esac

    cat > /etc/apt/sources.list <<EOF
deb ${URL} jammy main restricted universe multiverse
deb ${URL} jammy-updates main restricted universe multiverse
deb ${URL} jammy-backports main restricted universe multiverse
deb ${URL} ubuntu jammy-security main restricted universe multiverse
EOF
    apt update
}

update_centos_sources() {
    local VER=${VERSION%%.*}
    case "$TYPE" in
      official) echo "CentOS 官方已不再维护 8 以下版本，请使用新系统。" && exit 1 ;;
      aliyun) URL="https://mirrors.aliyun.com/centos/" ;;
      tencent) URL="https://mirrors.cloud.tencent.com/centos/" ;;
      huawei) URL="https://mirrors.huaweicloud.com/centos/" ;;
      tsinghua) URL="https://mirrors.tuna.tsinghua.edu.cn/centos/" ;;
    esac

    sed -i.bak "s|^mirrorlist=|#mirrorlist=|g; s|^#baseurl=.*|baseurl=${URL}\$releasever/os/\$basearch/|g" /etc/yum.repos.d/CentOS-Base.repo
    yum clean all && yum makecache
}

update_alma_or_rocky_sources() {
    case "$TYPE" in
      aliyun) BASEURL="https://mirrors.aliyun.com" ;;
      tencent) BASEURL="https://mirrors.cloud.tencent.com" ;;
      huawei) BASEURL="https://repo.huaweicloud.com" ;;
      tsinghua) BASEURL="https://mirrors.tuna.tsinghua.edu.cn" ;;
      official) echo "使用默认官方源即可。" && exit 0 ;;
    esac

    for repo in baseos appstream extras; do
      sed -i.bak -e "s|^mirrorlist=|#mirrorlist=|g" \
        -e "s|^#baseurl=.*|baseurl=${BASEURL}/$DISTRO/\$releasever/${repo}/\$basearch/os/|g" \
        /etc/yum.repos.d/${DISTRO^^}-${repo}.repo 2>/dev/null || true
    done
    dnf clean all && dnf makecache
}

update_arch_sources() {
    case "$TYPE" in
      official) URL="Server = https://mirror.rackspace.com/archlinux/\$repo/os/\$arch" ;;
      aliyun) URL="Server = https://mirrors.aliyun.com/archlinux/\$repo/os/\$arch" ;;
      tencent) URL="Server = https://mirrors.cloud.tencent.com/archlinux/\$repo/os/\$arch" ;;
      huawei) URL="Server = https://repo.huaweicloud.com/archlinux/\$repo/os/\$arch" ;;
      tsinghua) URL="Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch" ;;
    esac

    sed -i "s|^Server =.*|${URL}|" /etc/pacman.d/mirrorlist
    pacman -Syyu --noconfirm
}

# 主调度逻辑
case "$DISTRO" in
  debian) update_debian_sources ;;
  ubuntu) update_ubuntu_sources ;;
  centos) update_centos_sources ;;
  almalinux|rocky) update_alma_or_rocky_sources ;;
  arch) update_arch_sources ;;
  *)
    echo "暂不支持该系统类型：$DISTRO"
    exit 1
    ;;
esac

echo -e "\n✅ 源配置完成，已切换为：$TYPE 镜像源"
