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

NODE_VER=$(/usr/bin/node --version)
MIN_VER="8"

if $(dpkg --compare-versions ${NODE_VER:1} "lt" $MIN_VER); then
  echo "ERROR: /usr/bin/node must be at least v$MIN_VER.x"
  exit 1
fi

PACKAGE_DIR=$ZOOX_WORKSPACE_ROOT/argus/proto-library/
OUT_DIR=$PACKAGE_DIR/protos/
FILES=(
  vehicle/common/proto/traffic_lights.proto
  vehicle/common/config/proto/vehicle.proto
  vehicle/common/proto/decision_debug.proto
  vehicle/common/proto/decision_introspection.proto
  vehicle/planner/decision/proto/registry.proto
  sim/simple_sim/proto/console_log.proto
)

bazel run //build/protobuf:protoc -- --plugin=$PACKAGE_DIR/node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=snakeToCamel=false,forceLong=string,useEnumNames=trueoutputEncodeMethods=false,outputJsonMethods=false,outputClientImpl=false --proto_path=$ZOOX_WORKSPACE_ROOT --ts_proto_out=$OUT_DIR ${FILES[@]}

# Workaround to fix symbol collision https://github.com/stephenh/ts-proto/issues/36
# Delete the empty declaration of CascadeController and the subsequent 2 lines
sed '/export interface CascadeController {/,+2d' -i $OUT_DIR/vehicle/planner/decision/proto/action.ts
