#!/bin/bash

# Run with sim/argus_sim/run.sh <chum_uri> <simulator_args>

set -euo pipefail

workspace="$(bazel info workspace)"
source "$workspace/scripts/shell/zooxrc.sh"

# Run Simulation
unique_id="$RANDOM"
bazel run //sim/argus_sim:argus_sim run "$@" visualize:=true external_transports:=true chum_output_path:=/tmp/chum "chum_output_variant:=local_sim$unique_id" || exit_code=$?

# Display Chum URIs
original_chum="$1"
sim_chum="$1?i=/tmp/chum&v=local_sim${unique_id}"
echo "Simulated Chum URI:"
echo "$sim_chum"
echo "Original Chum URI:"
echo "$original_chum"

domain_name="$(sim/argus_sim/get_domain_name.sh)"

python3 "$workspace/sim/argus_sim/uri_builder.py" --chum_reference "$original_chum" --chum_comparison "$sim_chum" --base_domain "$domain_name"
echo "If this link fails to find server, in a new terminal, run: $workspace/argus/run.sh (and leave it running for future sims!)"

exit ${exit_code-}
