#!/bin/sh

set -e

tmpfile=$(mktemp)
cleanup() {
  rm -f "$tmpfile"
}
trap cleanup INT EXIT TERM

hit() {
  /usr/lib/apt/apt-helper \
    -o Acquire::http::Proxy-Auto-Detect=none \
    -o Acquire::http::Proxy=none \
    download-file "$@" "$tmpfile" 2>&1
}

detect_apt_cacher_ng() {
  local ip="$1"
  local proxy=http://$ip:3142
  if hit -o "Acquire::http::Proxy::${ip}=none" "$proxy" | grep -q -i '406.*usage.information'; then
    echo "$proxy"
    exit
  fi
}

detect_approx() {
  local ip="$1"
  local proxy=http://$ip:9999
  hit -o "Acquire::http::Proxy::${ip}=none" "$proxy" >/dev/null 2>&1 || true;
  if [ -s "$tmpfile" ] && grep -q -i '<title>approx\s*server</title>' "$tmpfile"; then
    echo "$proxy"
    exit
  fi
}

if which ip >/dev/null; then
  gateway=$(ip route | awk '/default/ { print($3) }')
else
  gateway=''
fi
for ip in 127.0.0.1 $gateway; do
  detect_apt_cacher_ng "$ip"
  detect_approx "$ip"
done
