#!/bin/bash

# Run with sim/argus_sim/run_sdl.sh <test_sdl args>

set -euo pipefail

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

# Run Simulation
exec 5>&1
test_output="$(sim/launch.sh local planner "$@" --save_chum --simulator_args='--params-kv sim/enable_monitors=true' 2>&1 | tee /dev/fd/5)" || exit_code=$?

# Extract Chum URIs
sim_chum_uris="$(echo "$test_output" | sed -n -e 's/.*Chum URI: \([^ ]*\).*/\1/p')"

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

# Generate and print an Argus link for each scenario in the suite
while IFS= read -r sim_chum; do
    echo ""
    echo "Simulated Chum URI:"
    echo "$sim_chum"
    python3 "$workspace/sim/argus_sim/uri_builder.py" --chum_reference "$sim_chum" --base_domain "$domain_name"
done <<< "$sim_chum_uris"
echo "If these link fails to find server, in a new terminal, run: $workspace/argus/run.sh (and leave it running for future sims!)"

exit ${exit_code-}
