#!/bin/bash
# For debugging proxy.rs
#
set -e
set -u
#set -o pipefail


# Make sure to clean up afterwards
trap 'jobs -p | xargs -r kill' EXIT

# cargo build

local_port="8080"

# Make sure anything that wasn't cleaned up is killed
netstat -lnp | grep -iF "$local_port" | perl -lanE 'say $F[-1]' | grep -oP '^\d+' \
    | xargs -d '\n' -r kill

dst_dir="$(mktemp -d)"
echo "Hello, world!" > "${dst_dir}/index.html"

cargo build
cargo run -- up -p "$local_port"
python3 -m http.server --directory "$dst_dir" "$local_port" &
sleep 1
curl "http://localhost:${local_port}" || true
curl --connect-timeout 3 --max-time 5 "http://$(cargo run -- ip):${local_port}"
wait
