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

cd "$(git rev-parse --show-toplevel)"

# Try to ensure we tear down the network on exit, including failure, ctrl-c, etc.
trap ./tests/chutney/teardown EXIT

./tests/chutney/setup

# shellcheck disable=SC1091
source tests/chutney/arti.run
# Validate that CHUTNEY_DATA_DIR is set.
[ -n "${CHUTNEY_DATA_DIR:?}" ]

./tests/chutney/test

# Get the socks port of a tor client to use for benchmarking comparison.
tor_client_port=$(\
  jq -r \
  'limit(1; .nodes[] | select(.is_client and .backend=="TOR") | .socksport)'\
  "$CHUTNEY_DATA_DIR"/nodes/network.json)
RUST_LOG=debug target/x86_64-unknown-linux-gnu/release/arti-bench -c "$CHUTNEY_DATA_DIR"/nodes/arti.toml --socks5 127.0.0.1:"$tor_client_port" -o benchmark_results.json
