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

parameters="$*"

# The core file will be downloaded to /tmp/TRIG-number if the script is executed
# with the default arguments. Make sure that the permission of the /tmp
# directory is set to 1777 to avoid potential permission denied problems.
results=($(bazel run //scripts/tools/core_files:core_analysis -- \
    ${parameters} | grep -o "gdb_arguments [^ ]\+ [^ ]\+"))

# Check for the magic string.
if [[ ${results[0]} != "gdb_arguments" ]]; then
    echo ERROR: Failed to find results from core analysis script.
    exit 1
fi

number_of_results=${#results[@]}
if ((${number_of_results} != 3)); then
    echo ERROR: Wrong number of arguments returned from core analysis script.
    echo ${results[@]}
    exit 1
fi

gdb "${results[1]}" "${results[2]}"
