#!/usr/bin/env bash

# Find the active python version
python_version=$(python3 --version 2>&1 | sed -n 's/^Python \([0-9]\+\.[0-9]\+\).*$/\1/p' )
GDB_BIN_DIR="$(dirname "$(realpath -e "${BASH_SOURCE[0]}")")"

# Make the rocgdb command with the active python version
# If the command is working exec the program to rocgdb-py_xxx binary
gdb_command="rocgdb-py_$python_version"
if [ -f "$GDB_BIN_DIR/$gdb_command" ]; then
    # Check to see if the rocgdb executable is able to run
    "$GDB_BIN_DIR/$gdb_command" --version > /dev/null 2>&1
    if [ $? == 0 ]; then
        # The binary is working in the env. exec to that
        exec "$GDB_BIN_DIR/$gdb_command" "$@"
    fi
fi

echo "rocgdb execution failed. Could be due to unsupported python version ${python_version} in the system"
