#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

WORKSPACE=$(bazel info workspace)
OUT_DIR=$WORKSPACE/argus/frontend/component-library/src/flatbuffers/
FILES=(
  argus/chum_to_json_service/plugins/data.fbs
  argus/chum_to_json_service/plugins/lidar_object_segmentation/lidar_object_segmentation.fbs
  argus/chum_to_json_service/plugins/tds_input/tds_input.fbs
  argus/chum_to_json_service/plugins/tds_labels/tds_labels.fbs
  argus/chum_to_json_service/plugins/lidar_point_cloud/lidar_point_cloud.fbs
  argus/chum_to_json_service/plugins/safetynet/safetynet.fbs
  argus/chum_to_json_service/plugins/zrn_monitor/zrn_monitor.fbs
  argus/chum_to_json_service/plugins/heatmap_collection/heatmap_collection.fbs
)

bazel run //third_party/flatbuffers:flatc -- --ts -o $OUT_DIR "${FILES[@]/#/$WORKSPACE/}"

for FILE in ${FILES[@]}; do
  OUT_FILE=$(basename $FILE | sed -e 's/\.fbs/_generated\.ts/g')
  sed -i -e 's/\.\/flatbuffers/flatbuffers/g' $OUT_DIR/$OUT_FILE
done
