#!/usr/bin/env bash
# 部署脚本共用：读取 HTTP_PROXY / http_proxy 并配置 apt、git、pip、curl
# 用法（install.sh / update.sh 前）：
#   export http_proxy=http://192.168.8.246:10810
#   export https_proxy=http://192.168.8.246:10810
#   export HTTP_PROXY="$http_proxy"
#   export HTTPS_PROXY="$https_proxy"

setup_deploy_proxy() {
  local proxy="${HTTP_PROXY:-${http_proxy:-}}"
  if [[ -z "$proxy" ]]; then
    return 0
  fi

  if [[ "$proxy" != http://* && "$proxy" != https://* && "$proxy" != socks5://* ]]; then
    proxy="http://${proxy}"
  fi

  export http_proxy="$proxy"
  export https_proxy="${HTTPS_PROXY:-${https_proxy:-$proxy}}"
  export HTTP_PROXY="$http_proxy"
  export HTTPS_PROXY="$https_proxy"
  export ALL_PROXY="${ALL_PROXY:-$http_proxy}"

  if [[ -n "${log_info:-}" ]]; then
    log_info "使用网络代理: ${http_proxy}"
  else
    echo "[INFO] 使用网络代理: ${http_proxy}"
  fi

  mkdir -p /etc/apt/apt.conf.d
  cat > /etc/apt/apt.conf.d/95grade-archive-proxy <<EOF
Acquire::http::Proxy "${http_proxy}";
Acquire::https::Proxy "${https_proxy}";
EOF
}
