#!/bin/bash

# NOTE: In safety_pcp_offline_metric_builder_config.pbtxt, you need to set
# the file_path to ${DRIVING_HOME}/experimental/${USER}/metric_chums.pbtxt
# in order for this script to work properly.

# Top-Level Variables
#PIPEDREAM="--pipedream --default_num_workers=9"
PIPEDREAM=""
COMMENT_SUFFIX="_monday_local"
DRIVING_HOME=${ZOOX_WORKSPACE_ROOT:-HOME}
OFFLINE_PBTXT=${DRIVING_HOME}/mined_metric/builder/all_metric_builder_configs/safety_pcp_offline_metric_builder_config.pbtxt
declare -a SetsToProcess=("metrichub_yx_cas_tn.pbtxt" "metrichub_yx_cas_tp.pbtxt")

sed -i 's|s3://zoox-mined-metrics/mined_benchmark_data/safety_pcp/metrichub_test.pbtxt|${DRIVING_HOME}/experimental/${USER}/metric_chums.pbtxt|g' ${OFFLINE_PBTXT}

# Loop through each of the sets
for metric_set in "${SetsToProcess[@]}"; do
    # Download the set locally
    #echo "Pulling down ${metric_set} from s3..."
    #bazel run //base/file/tools:zoox_fs cp s3://zoox-mined-metrics/mined_benchmark_data/safety_pcp/$metric_set ${DRIVING_HOME}/experimental/${USER}/$metric_set

    #
    # State-only metrics
    #

    echo "Running state-only metrics for ${metric_set}"

    # Turn off Z-metrics and turn on state metrics
    sed -i 's/mining_by_cas_state: false/mining_by_state: true/g' ${OFFLINE_PBTXT}
    sed -i 's/mining_by_delta_z: true/mining_by_delta_z: false/g' ${OFFLINE_PBTXT}

    # link the file
    ln -sf ${DRIVING_HOME}/experimental/${USER}/${metric_set} ${DRIVING_HOME}/experimental/${USER}/metric_chums.pbtxt
    # Run the cases
    bazel run //mined_metric/builder:run_metric_builder -- --metric_id=safety_pcp_offline --logtostderr ${PIPEDREAM} --need_gpu=true --comment=${metric_set}_state_${COMMENT_SUFFIX}

    #
    # Z-only metrics
    #

    echo "Running Z-only metrics for ${metric_set}"

    # Turn off Z-metrics and turn on state metrics
    sed -i 's/mining_by_cas_state: true/mining_by_state: false/g' ${OFFLINE_PBTXT}
    sed -i 's/mining_by_delta_z: false/mining_by_delta_z: true/g' ${OFFLINE_PBTXT}

    # link the file
    ln -sf ${DRIVING_HOME}/experimental/${USER}/${metric_set} ${DRIVING_HOME}/experimental/${USER}/metric_chums.pbtxt
    # Run the cases
    bazel run //mined_metric/builder:run_metric_builder -- --metric_id=safety_pcp_offline --logtostderr ${PIPEDREAM} --need_gpu=true --comment=${metric_set}_z${COMMENT_SUFFIX}

    #
    # Combined metrics
    #

    echo "Running State and Delta Z metrics for ${metric_set}"

    # Turn off Z-metrics and turn on state metrics
    sed -i 's/mining_by_cas_state: true/mining_by_state: true/g' ${DRIVING_HOME}/mined_metric/builder/all_metric_builder_configs/safety_pcp_offline_metric_builder_config.pbtxt
    sed -i 's/mining_by_delta_z: false/mining_by_delta_z: true/g' ${DRIVING_HOME}/mined_metric/builder/all_metric_builder_configs/safety_pcp_offline_metric_builder_config.pbtxt

    # link the file
    ln -sf ${DRIVING_HOME}/experimental/${USER}/${metric_set} ${DRIVING_HOME}/experimental/${USER}/metric_chums.pbtxt
    # Run the cases
    bazel run //mined_metric/builder:run_metric_builder -- --metric_id=safety_pcp_offline --logtostderr ${PIPEDREAM} --need_gpu=true --comment=${metric_set}_combined${COMMENT_SUFFIX}

done