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

script_dir="$(builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
mode="$1"

if [[ -e "$script_dir/chum_store" && $mode == "regen" ]]; then
    echo Cleaning up first...
    rm -r "$script_dir/chum_store" "$script_dir/data"
fi

if [[ ! -e "$script_dir/chum_store" ]]; then
    mkdir "$script_dir/chum_store" "$script_dir/data"

    bazel build \
        //vehicle/common/zci/example/repro_missing_topic:create_missing_topic \
        //vehicle/common/zci/example/repro_missing_topic:repro_missing_topic \
        //data/chum/tools:chum_ls \
        //base/message_utils:show_message

    bazel run //vehicle/common/zci/example/repro_missing_topic:create_missing_topic \
        -- $script_dir/component_one_{,transport_}params.pbtxt \
           $script_dir/component_two_{,transport_}params.pbtxt \
           $script_dir/chum_store

    bazel_run //scripts/tools/zci:stacktrace_analysis -- \
        /tmp/create_trace.txt > /tmp/create_summary.txt

    chum_ls_range=$(bazel_run //data/chum/tools:local_store_range -- \
        -chum_root "$script_dir/chum_store" -format chum_ls)

    show_message_range=$(bazel_run //data/chum/tools:local_store_range -- \
        -chum_root "$script_dir/chum_store" -format show_message)

    echo $chum_ls_range
    echo $show_message_range

    bazel_run //data/chum/tools:chum_ls -- --input=/tmp/missing_topic_log \
        $chum_ls_range > "$script_dir/data/chum_ls.txt"

    bazel_run //base/message_utils:show_message -- $show_message_range \
        --stdout \
        --topic /example/component_one/reproducibility \
        "--chum_input=$script_dir/chum_store" \
        > "$script_dir/data/component_one_reproducibility.json"

    bazel_run //base/message_utils:show_message -- $show_message_range \
        --stdout \
        --topic /example/component_two/reproducibility \
        "--chum_input=$script_dir/chum_store" \
        > "$script_dir/data/component_two_reproducibility.json"

    bazel_run //base/message_utils:show_message -- $show_message_range \
        --stdout \
        --topic /logged_topic \
        "--chum_input=$script_dir/chum_store" \
        > "$script_dir/data/logged_topic.json"

    bazel_run //base/message_utils:show_message -- $show_message_range \
        --stdout \
        --topic /unlogged_topic \
        "--chum_input=$script_dir/chum_store" \
        > "$script_dir/data/unlogged_topic.json"
fi

if [[ $# > 0 && $mode == "debug" ]]; then
    bazel run --config=gdb \
        //vehicle/common/zci/example/repro_missing_topic:repro_missing_topic  \
        -- $script_dir/component_one_{,reproducer_}params.pbtxt \
           $script_dir/component_two_{,reproducer_}params.pbtxt \
           $script_dir/chum_store
else
    bazel run \
        //vehicle/common/zci/example/repro_missing_topic:repro_missing_topic \
        -- $script_dir/component_one_{,reproducer_}params.pbtxt \
           $script_dir/component_two_{,reproducer_}params.pbtxt \
           $script_dir/chum_store

    bazel_run //scripts/tools/zci:stacktrace_analysis -- \
        /tmp/regen_trace.txt > /tmp/regen_summary.txt
fi
