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

RUN_ID="$1"

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 'USAGE:'
    echo '  ./bandwidth.sh <CANDIDATE_RUN_ID> [OUTPUT_PATH]'
    exit 1
fi

# Route and vehicle type are inferred from $RUN_ID
bazel run //core/middleware/logger/bandwidth:topic_bw_over_runs -- \
    --avg_acceptable_kbps_increase=150 \
    --candidate_run_id="$RUN_ID" \
    --output_path="$TMP_DIR" $INGEST $JUST_THE_GOLD

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

