#!/usr/bin/env bash
set -x

dir="$(cd "$(dirname "$0")" && pwd)"
SOCK="/tmp/proxy.s"
PROXY_HOST="127.0.0.1:41080"


#socat tcp-listen:10007,fork exec:cat &
#echo $! > /tmp/socat-test.pid

if test -z "$@"; then
    list="no_auth username_auth long_username_password_auth"
else
    list="$@"
fi

socat UNIX-LISTEN:${SOCK},reuseaddr,fork TCP:${PROXY_HOST} &

for test in ${list}; do
    cfg="${dir}/${test}.cfg"
    if test ! -f "${cfg}"; then
        echo "missing 3proxy config: ${cfg}" >&2
        exit 1
    fi
    3proxy "${cfg}"
    sleep 1
    cargo test --test ${test} -- --test-threads 1
    test_exit_code=$?

    pkill -F /tmp/3proxy-test.pid

    if test "$test_exit_code" -ne 0; then
        break
    fi
done


#pkill -F /tmp/socat-test.pid
exit ${test_exit_code}
