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

usim_yaml=$1
vehicle=$2
pkgname='lidar/metrics'
perception_proto='vehicle/perception/perception_proto'

puck_xml=${usim_yaml/usim.yaml/usim.puck.xml}
echo $puck_xml
usim_bin=${usim_yaml/usim.yaml/usim.bin}
if [[ -f $puck_xml && -f $usim_bin ]]; then
  ZOOX_ROBOT_NAME="$vehicle" vehicle/launch/vehicle_params_launch pucks_file:=${puck_xml}
  # Overrides file specifies specific parameters etc. that needs to be overridden for this dataset.
  overrides_file=${usim_yaml/usim.yaml/usim.overrides.sh}
  if [[ -f ${overrides_file} ]]; then
    source ${overrides_file}
  fi
  # TODO (subhasis, 11/08): Remove this once bg subtraction don't use tf anymore.
  rosparam delete /driving/perception/background_subtractor_params_proto_file
  gt_proto=${usim_yaml/usim.yaml/usim.gt.lmdb}
  perc_proto=${usim_yaml/usim.yaml/usim.perc.lmdb}
  track_results_proto=${usim_yaml/usim.yaml/usim.tracking.results.lmdb}
  seg_results_proto=${usim_yaml/usim.yaml/usim.segmentation.results.proto}
  det_results_proto=${usim_yaml/usim.yaml/usim.detection.results.lmdb}
  # New usim files have different spin loader period.
  ${perception_proto}/usim_to_proto --params-ros -out $gt_proto -yaml_filename $usim_yaml -alsologtostderr
  # Use the gt lmdb in perception_to_proto to ensure exactly same spins are processed.
  ${perception_proto}/perception_to_proto --params-ros $gt_proto $perc_proto
  ${pkgname}/tracking_metrics --out_lmdb ${track_results_proto} --gt_lmdb ${gt_proto} --perc_lmdb ${perc_proto} --thresh 1.0
  ${pkgname}/segmentation_metrics -o ${seg_results_proto} --gt ${gt_proto} --perc ${perc_proto} --dist 10 20 30 40 50
  ${pkgname}/detection_metrics -out ${det_results_proto} -gt ${gt_proto} -perc ${perc_proto} -use_tracking_blobs
fi
