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

RUN_ID="$1"
BASELINE_RUN_ID="$2"

if [[ $# == "2" ]]; then
    TMP_DIR="$(mktemp -d -t bw-XXXXXXXXXX)"
    JUST_THE_GOLD="--just_the_gold"
    INGEST="--ingest"
elif [[ $# == "3" ]]; then
    TMP_DIR=$3
    mkdir -p $TMP_DIR
    JUST_THE_GOLD=""
    INGEST=""
else
    echo 'USAGE:'
    echo '  ./zimbot_bandwidth.sh <CANDIDATE_RUN_ID> <BASELINE_RUN_ID> [OUTPUT_PATH]'
    exit 1
fi

bazel run //scripts/tools/zci:plot_bw_analysis -- \
    --bw_type=bw_vehicle \
    --upload \
    --no_bazel \
    --run_id="$RUN_ID"

bazel run //scripts/tools/zci:plot_bw_analysis -- \
    --bw_type=bw_vehicle \
    --upload \
    --no_bazel \
    --run_id="$BASELINE_RUN_ID"

bazel run //core/middleware/logger/bandwidth:topic_bw_over_runs -- \
    --avg_acceptable_kbps_increase=150 \
    --run_id="$BASELINE_RUN_ID" \
    --candidate_run_id="$RUN_ID" \
    --output_path="$TMP_DIR" $INGEST $JUST_THE_GOLD

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