#!/bin/bash
# Debian 一键升级 cURL 到 backports 最新版本
# 适用于 Debian 11 / Debian 12

set -e

echo ">>> 检测 Debian 版本..."
VERSION=$(grep VERSION_CODENAME /etc/os-release | cut -d= -f2)

if [[ "$VERSION" == "bookworm" ]]; then
    BACKPORTS_SOURCE="deb http://deb.debian.org/debian bookworm-backports main contrib non-free non-free-firmware"
    TARGET="bookworm-backports"
elif [[ "$VERSION" == "bullseye" ]]; then
    BACKPORTS_SOURCE="deb http://deb.debian.org/debian bullseye-backports main contrib non-free non-free-firmware"
    TARGET="bullseye-backports"
else
    echo "ERROR: 未知的 Debian 版本，无法自动配置 backports。"
    exit 1
fi

echo ">>> 当前系统版本: $VERSION"
echo ">>> Backports 源: $TARGET"

# Backports 文件位置
BP_FILE="/etc/apt/sources.list.d/backports.list"

echo ">>> 写入 Backports 源到 $BP_FILE"
echo "$BACKPORTS_SOURCE" | tee $BP_FILE >/dev/null

echo ">>> 更新软件源..."
apt update -y

echo ">>> 安装新版 cURL（来自 $TARGET）..."
apt -t $TARGET install -y curl libcurl4

echo ">>> 安装完成，当前 cURL 版本："
curl --version

echo ">>> 你可能需要 restart 相关依赖 cURL 的服务（例如 Onedrive）。"
