#!/usr/bin/env bash
set -xeuo pipefail

# Set and fully resolve chutney bin if not already set.
: "${CHUTNEY_BIN:=$(type -P chutney)}"
if [ -z "${CHUTNEY_BIN:-}" ]; then
    echo "Couldn't locate chutney bin. Ensure it's on PATH or set CHUTNEY_BIN."
    echo "You can install with:"
    echo "python3 -m pip install git+https://gitlab.torproject.org/tpo/core/chutney.git"
    exit 1
elif [ ! -x "$CHUTNEY_BIN" ]; then
    echo "CHUTNEY_BIN='$CHUTNEY_BIN' doesn't exist or isn't executable"
    exit 1
else
    # CHUTNEY_BIN is set; tell the user so.
    echo "Using chutney at '${CHUTNEY_BIN}'"
fi

export CHUTNEY_DATA_DIR="${CHUTNEY_DATA_DIR:-$(pwd)}"

if [ -z "${RUST_LOG:-}" ]; then
    echo "Setting RUST_LOG=info for your convenience."
    export RUST_LOG=info
fi

target="basic"
cd "$(git rev-parse --show-toplevel)"

# Try to ensure we tear down the network on exit, including failure, ctrl-c, etc.
function teardown() {
    "${CHUTNEY_BIN}" stop "$target"
}
trap teardown EXIT

./tests/chutney/setup -n "$target"

cargo run -p arti-bench --locked --release -- -c "${CHUTNEY_DATA_DIR}/nodes/arti.toml" "$@"

"${CHUTNEY_BIN}" stop "$target"
