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

RUN_ID="$1"
WINDOW_START="$(date --date='-6 months' +'%Y-%m-%d')"

if [[ $# == "1" ]]; then
    TMP_DIR="$(mktemp -d -t bw-XXXXXXXXXX)"
    JUST_THE_GOLD="--just_the_gold"
    INGEST="--ingest"
elif [[ $# == "2" ]]; then
    TMP_DIR=$2
    mkdir -p $TMP_DIR
    JUST_THE_GOLD=""
    INGEST=""
else
    echo 'One or two arguments please.'
    exit 1
fi

bazel run //experimental/jake:print_topic_bw_over_runs -- \
    --output-path="$TMP_DIR" \
    --run_type="integration_testing" \
    --route="SF - Bryant IAT" \
    --vehicle_type="Kitt" \
    --git_branch_regex="vehicle-merge-queue-batch-.*-base" \
    --window_start="$WINDOW_START" $JUST_THE_GOLD

bazel run //scripts/tools/zci:compare_bw_analysis -- \
        --lhs_run_id "$RUN_ID" \
        --avg_acceptable_kbps_increase=150 \
        --bw_type=bw_vehicle_diff \
        --rhs_pbtxt "$TMP_DIR/golden_bw_analysis.pbtxt" \
        --no_bazel $INGEST

if [[ $# == "1" ]]; then
    rm -r $TMP_DIR
fi
