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

for i in "$@"
do
  case $i in
    --suite_run_details=*)
      # ignore the txt file
      shift
      ;;
    --suite_run_details_pbtxt=*)
      input_file="${i#*=}"
      shift
      ;;
    --output_dir=*)
      output_dir="${i#*=}"
      output_dir=$output_dir"/"
      shift
      ;;
    --pipe_id=*)
      pipe_id="${i#*=}"
      shift
      ;;
    *)
      echo "invalid option"
      exit
      ;;
  esac
done

echo $input_file
echo $output_dir

evaluator_pbtxt_location=$output_dir"junction_evaluator_runner_output.pbtxt"
pipe_id_location=$output_dir"pipe_id.txt"

# Run evaluator
echo "running evaluator"
bazel run //mined_metric/builder/metrics_impl/junction_metrics:junction_evaluator_runner \
  -- --runs_pbtxt=$input_file --output_pbtxt_path=$evaluator_pbtxt_location
echo "evaluator complete"

# Run exporter
echo "running formater"
bazel run //mined_metric/builder/metrics_impl/junction_metrics/pmav:analyze_ntlj_run \
  -- --run_tag slac_metrics_pipeline \
  --input_dir $output_dir --output_dir $output_dir --experiment_id=1
echo "formater complete"

# notify_results expects the pipe_id file to contain the pipe id of the running
# job.
echo $pipe_id > $pipe_id_location
echo "running exporter"
bazel run //mined_metric/builder/metrics_impl/junction_metrics/pmav:notify_ntlj_results \
  -- --run_tag //ntlj_pipeline \
  --output_dir $output_dir
echo "exporter complete"
