#!/bin/bash

set -e

PROXY="http://192.168.1.1:9072/"
APT_CONF_DIR="/etc/apt/apt.conf.d"
PROXY_FILE="${APT_CONF_DIR}/99proxy"

echo "==> 清理已有 APT Proxy 配置..."

# 删除所有包含 Acquire::http::Proxy 或 Acquire::https::Proxy 的配置文件
find "$APT_CONF_DIR" -type f | while read -r file; do
    if grep -qE 'Acquire::(http|https)::Proxy' "$file"; then
        echo "删除: $file"
        rm -f "$file"
    fi
done

echo "==> 写入新的 APT Proxy 配置..."

cat > "$PROXY_FILE" <<EOF
Acquire::http::Proxy "${PROXY}";
Acquire::https::Proxy "${PROXY}";
EOF

echo "==> 当前 APT Proxy 配置："
apt-config dump | grep -i Proxy || true

echo
echo "✔ APT 代理已设置为：${PROXY}"
