# Copyright 2023 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

_process_ids="";

#All user scripts require these variable, let's check for them here already
: "${BSIM_OUT_PATH:?BSIM_OUT_PATH must be defined}"

#Give a default value to BOARD if it does not have one yet:
BOARD="${BOARD:-nrf52_bsim}"

#The board target full string (with qualifiers):
BOARD_TS="${BOARD//\//_}"

function run_in_background(){
  $@ & _process_ids="$_process_ids $!"
}

function wait_for_background_jobs(){
  exit_code=0
  for process_id in $_process_ids; do
    wait $process_id || let "exit_code=$?"
  done
  [ $exit_code -eq 0 ] || exit $exit_code
}

trap ctrl_c INT

function ctrl_c() {
  echo "Aborted by CTRL-C"

  for process_id in $_process_ids; do
    kill -15 $process_id
  done
}

function check_program_exists() {
  if [ ! -f $1 ]; then
    echo -e "  \e[91m`pwd`/`basename $1` cannot be found (did you forget to\
 compile it?)\e[39m"
    exit 1
  fi
}

function Execute() {
  EXECUTE_TIMEOUT="${EXECUTE_TIMEOUT:-30}"

  check_program_exists $1
  run_in_background timeout --kill-after=5 -v ${EXECUTE_TIMEOUT} $@
}

function _guess_test_relpath(){
  local PA="$(cd -- "$(dirname "${BASH_SOURCE[2]}")" && pwd)"
  PA="$(realpath --relative-to "${ZEPHYR_BASE}" "${PA}")"
  echo $PA | sed -E 's/\/tests?_scripts//'
}

# For a caller running from a tests_script/ folder, get the path of its parent
# relative to $ZEPHYR_BASE
# This must be run without/before cd'ing into another directory
function guess_test_relpath(){
  echo $(_guess_test_relpath)
}

# For a caller running from a tests_script/ folder, get the path of its parent
# relative to $ZEPHYR_BASE, replacing "/" with "_"
# This must be run without/before cd'ing into another directory
function guess_test_long_name(){
  echo $(_guess_test_relpath) | tr / _
}
