#!/usr/bin/env bash

set -euo pipefail

print_help_text() {
    SRC_FILE=$1
    DEST_FILE=$2
    echo "To fix this error, run:"
    echo "bazel run //web_apps/pcp_ci/metrics_server:convert_renderer_pbtxt_to_pbjson -- -input $SRC_FILE -out $DEST_FILE"
    echo "yarn lint"
}

check_definition() {
    SRC_FILE=$1
    DEST_FILE=$2
    DEST_TMP_FILE=${DEST_FILE/%.ts/.new.ts}
    # Run converter.
    bazel run //web_apps/pcp_ci/metrics_server:convert_renderer_pbtxt_to_pbjson -- -input $SRC_FILE -out $DEST_TMP_FILE
    # Run linter.
    yarn lint $DEST_TMP_FILE
    # Check that it should be the same as the stored ts.
    diff $DEST_TMP_FILE $DEST_FILE || (
        c=$?
        print_help_text $SRC_FILE $DEST_FILE
        (exit $c)
    )
    # Remove newly generated ts.
    rm $DEST_TMP_FILE
}

# Check segmentation metrics renderer definition.
check_definition lidar/metrics/data/segmentation_metrics_renderer_definitions.pbtxt $PWD/src/all_metrics_configs/segmentation_renderer_definitions.ts
check_definition lidar/metrics/data/tracking_metrics_renderer_definitions.pbtxt $PWD/src/all_metrics_configs/tracking_renderer_definitions.ts
