#!/bin/bash
# Copyright 2020 Simon McVittie
# SPDX-License-Identifier: LGPL-2.1+

set -eux
set -o pipefail

#export PX_DEBUG=1

reset () {
    unset XDG_CURRENT_DESKTOP
    unset FTP_PROXY
    unset GSETTINGS_BACKEND
    unset HTTPS_PROXY
    unset HTTP_PROXY
    unset KDE_FULL_SESSION
    unset NO_PROXY
    unset ftp_proxy
    unset http_proxy
    unset https_proxy
    unset no_proxy
}

reset
export XDG_CURRENT_DESKTOP=GNOME
export GSETTINGS_BACKEND=keyfile
export XDG_CONFIG_HOME="$AUTOPKGTEST_TMP/config"
mkdir -p "$XDG_CONFIG_HOME/glib-2.0/settings"
cat > "$XDG_CONFIG_HOME/glib-2.0/settings/keyfile" <<'EOF'
[system/proxy]
autoconfig-url=''
ignore-hosts=['localhost']
mode='manual'
use-same-proxy=false

[system/proxy/ftp]
host='ftpproxy.example.net'
port=8000

[system/proxy/http]
host='proxy.example.net'
port=8000

[system/proxy/https]
host='sslproxy.example.net'
port=8000

[system/proxy/socks]
host='socksproxy.example.net'
port=8000
EOF

answer="$(proxy http://example.com)"
test "${answer}" = "http://proxy.example.net:8000"

answer="$(proxy https://example.com)"
test "${answer}" = "http://sslproxy.example.net:8000"

answer="$(proxy ftp://ftp.example.com)"
test "${answer}" = "http://ftpproxy.example.net:8000"

answer="$(proxy http://localhost)"
test "${answer}" = direct://
