# shellcheck shell=bash
#------------------------------------------------------------------------------
# env.sh - Common settings of the RVM's test scripts (sourced, not run)
#
# Sets, from the location of the tests (this file is in rvm/tests/), unless
# the environment already gives them:
#
#   RVM_DIR        the RVM's directory (rvm/), where rvm.rex and js/ are;
#   RVM_BIN        the bin/ directory of the Rexx Parser (two levels up),
#                  which REXX_PATH is set to (the RVM needs the parser);
#   RVM_TESTS_TMP  the root of the work directories
#                  (default: ${TMPDIR:-/tmp}/rvm-tests).
#
# Defines rvm_oracle ARG: ARG is "rvm" or "js" (an oracle directory for that
# RVM is built under RVM_TESTS_TMP, see fuzz/make-oracle.sh) or the path of
# an existing oracle directory; prints the oracle directory.
#
# This file is part of the RVM, the Rexx Virtual Machine
# [See https://rexx.epbcn.com/rexx-parser/]
#
# Copyright (c) 2026 Josep Maria Blasco <josep.maria.blasco@epbcn.com>
#
# License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
#------------------------------------------------------------------------------
RVM_TESTS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RVM_DIR="${RVM_DIR:-$(cd "$RVM_TESTS/.." && pwd)}"
RVM_BIN="${RVM_BIN:-$(cd "$RVM_DIR/../.." && pwd)}"
RVM_TESTS_TMP="${RVM_TESTS_TMP:-${TMPDIR:-/tmp}/rvm-tests}"
REXX_PATH="$RVM_BIN"
export RVM_TESTS RVM_DIR RVM_BIN RVM_TESTS_TMP REXX_PATH
mkdir -p "$RVM_TESTS_TMP"

rvm_oracle() {
  case "$1" in
    rvm) bash "$RVM_TESTS/fuzz/make-oracle.sh" "$RVM_TESTS_TMP/oracle-rvm" ;;
    js)  bash "$RVM_TESTS/fuzz/make-oracle-js.sh" "$RVM_TESTS_TMP/oracle-js" ;;
    "")  echo "an oracle is needed: rvm, js or an oracle directory" >&2; return 1 ;;
    *)   (cd "$1" && pwd) ;;
  esac
}
