#!/bin/sh

cache_host="apt-cacher.eon.lan"
cache_port="3142"

config_path="/etc/apt/apt.conf.d/99autoproxy.conf"
script_path="/etc/apt/detect-proxy.sh"

cat > "$config_path" << EOF
Acquire::http {
        Proxy-Auto-Detect "${script_path}"
}
EOF

cat > "$script_path" << EOF
#!/bin/sh
if nc -z "${cache_host}" 3142; then
     echo "http://${cache_host}:${cache_port}/"
     exit
fi 2>/dev/null

echo "DIRECT"
EOF

chmod +x "$script_path"