#!/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="Foster City Loop" \
    --vehicle_type="Kitt" \
    --git_branch_regex="vehicle-merge-queue-batch-.*-base" \
    --window_start="$WINDOW_START" \
    --run_id='20201209T184424-kitt_22' \
    --run_id='20201215T174907-kitt_22' \
    --run_id='20201217T204846-kitt_22' \
    --run_id='20210105T181536-kitt_22' \
    --run_id='20210106T180224-kitt_22' \
    --run_id='20210107T180930-kitt_22' \
    --run_id='20210111T172955-kitt_22' \
    --run_id='20210112T170433-kitt_22' \
    --run_id='20210115T171055-kitt_22' \
    --run_id='20210119T170200-kitt_22' \
    --run_id='20210120T170205-kitt_22' \
    --run_id='20210121T164733-kitt_22' \
    --run_id='20210122T170731-kitt_22' \
    --run_id='20210125T170644-kitt_22' \
    --run_id='20210126T172237-kitt_22' \
    --run_id='20210127T171530-kitt_22' \
    --run_id='20210128T172127-kitt_22' \
    --run_id='20210129T171139-kitt_22' \
    --run_id='20210201T171431-kitt_22' $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
