#!/bin/bash
#
# Given a pbtxt of chum uri, this script helps to estimate log conversion
# success. It simulates logmare and synthetic logmare, calculates the HMM
# and dumps the results in a combined csv for further analysis.
#
# Note: This is a super scrappy approach which runs locally. It takes ~12 hours
# to run through ~100 simulations. It also depends heavily on the formatting of
# stdout. It should be used causiously.
#
# Example usage:
#     bash calculate_success_metric.sh

tmp_dir=$(mktemp -d)
results_dir="$tmp_dir/results"
mkdir "$results_dir"
final_csv="$tmp_dir/combined_results.csv"
failures_csv="$tmp_dir/failures.csv"
chum_uri_pbtxt="/mnt/nautilus_rw/sim/agent_metrics/chum_sets/chum_set_challenge_193.pbtxt"

readonly tmp_dir results_dir final_csv chum_uri_pbtxt failures_csv

echo "Storing results in: $tmp_dir"
echo "Using ZOOX_WORKSPACE_ROOT: $ZOOX_WORKSPACE_ROOT"
read -n 1 -s -r -p "Press any key to continue or ctrl-c to exit."

# Write csv titles.
echo "logmare_chum_variant,synthetic_logmare_chum_variant,logmare_result,\
synthetic_logmare_result,track_id,mean_distance_error,max_distance_error"\
      >> "$final_csv"
echo "chum_uri,failure" >> "$failures_csv"

# For each chum uri in file
while read chum_line; do
  # Grab the chum uri held between the double quotes
  chum_uri=$(echo $chum_line | cut -d'"' -f2)
  filename_friendly_chum="${chum_uri:7}"
  echo "Processing: $chum_uri"

  echo "Calculating logmare."
  logmare_output_txt="$tmp_dir/${filename_friendly_chum}_logmare_output.txt"
  sim/argus_sim/run_sdl.sh  "$chum_uri"  --simulator_args='--params-kv sim/metrics=true' \
    > "$logmare_output_txt"  </dev/null
  # Grab the chum uri after the text: "] Chum URI: "
  logmare_chum_uri=$(cat "$logmare_output_txt" | sed -n -e 's/^.*] Chum URI: //p')
  if [[ "$logmare_chum_uri" == "" ]]; then
    echo "No logmare chum uri found."
    echo "$chum_uri,Logmare simulation failed." >> "$failures_csv"
    continue
  fi
  # Grab the result: SUCCESS, DIVERGED FROM BAG, COLLISION MONITOR, etc.
  # It is the second to last line
  logmare_result=$(tail -n 2 "$logmare_output_txt" | head -1)
  echo "Logmare chum uri: $logmare_chum_uri"

  echo "Calculating synthetic logmare."
  synthetic_logmare_output_txt="$tmp_dir/${filename_friendly_chum}_synthetic_logmare_output.txt"
  bazel run //sim/log_test_converter:convert_chum_uri -- \
    --chum_uri="$chum_uri" \
    --output_path="$ZOOX_WORKSPACE_ROOT/sim/simple_sim/testdata/smart_agent_debug/changed.pbtxt" \
     > /dev/null </dev/null
  sim/argus_sim/run_sdl.sh //sim/simple_sim/testdata/smart_agent_debug:changed \
    --simulator_args="--params-kv sim/metrics=true" > "$synthetic_logmare_output_txt"  </dev/null
  rm "$ZOOX_WORKSPACE_ROOT/sim/simple_sim/testdata/smart_agent_debug/changed.pbtxt"
  # Grab the chum uri after the text: "] Chum URI: "
  synthetic_logmare_chum_uri=$(cat "$synthetic_logmare_output_txt" | sed -n -e 's/^.*] Chum URI: //p')
  if [[ "$synthetic_logmare_chum_uri" == "" ]]; then
    echo "No synthetic logmare chum uri found."
    echo "$chum_uri,Synthetic logmare simulation failed." >> "$failures_csv"
    continue
  fi
  # Grab the result: SUCCESS, DIVERGED FROM BAG, COLLISION MONITOR, etc.
  # It is the second to last line
  synthetic_logmare_result=$(tail -n 2 "$synthetic_logmare_output_txt" | head -1)
  echo "Synthetic logmare chum uri: $synthetic_logmare_chum_uri"

  echo "Calculating metric."
  output_basename="$results_dir"/"${filename_friendly_chum}"
  output_file_csv="${output_basename}_all_errors.csv"
  bazel run --config=cuda //sim/agent_metrics:compare_agent_trajectories -- --chum_uri="$logmare_chum_uri" \
      --synthetic_chum_uri="$synthetic_logmare_chum_uri" --output_basename="$output_basename" \
       > /dev/null </dev/null

  # Prepend the Chum URIs and success result to each line in the output csv.
  echo "Appending: $output_file_csv"
  while read csv_line; do
    echo "$logmare_chum_uri,$synthetic_logmare_chum_uri,$logmare_result,\
          $synthetic_logmare_result,$csv_line" >> "$final_csv";
  done <"$output_file_csv"

  echo "Results written to: $output_file_csv, and $final_csv"

done <"$chum_uri_pbtxt"

echo "All results written to: $final_csv"
