#!/bin/bash

# shellcheck disable=1090,2034

set -e

testml-run-file() {
#   (env | grep TESTML_ | sort) | perl -pe 's/^/# /' >&2

  local src_bin=src/testml-run.cpp
  local bridge_file=$PWD/test/$TESTML_BRIDGE.cpp
  local lib=$TESTML_ROOT_LIB/testml
  local testml_runner=$TESTML_CACHE/testml-run-cpp

  set -x
  /usr/bin/env c++ \
    -o $testml_runner \
    $src_bin \
    $lib/run/tap.cpp \
    $lib/run.cpp \
    $lib/stdlib.cpp \
    $lib/bridge.cpp \
    $bridge_file || return

  chmod +x $testml_runner
  $testml_runner "$TESTML_EXEC"
}

: "${TESTML_BIN:=testml-cpp-tap}"
: "${TESTML_LANG:=cpp}"
: "${TESTML_MODULE:=TestML::Run::TAP}"
: "${TESTML_BRIDGE:=testml-bridge}"
export TESTML_{BIN,LANG,MODULE,BRIDGE}

[[ -n $TESTML_SOURCED ]] ||
  source "${TESTML_ROOT:-$(dirname "$0")/..}/bin/testml"

testml_runner_sourced=true

[[ $0 != "${BASH_SOURCE[0]}" ]] || testml-run "$@"
