#!/bin/sh
set -u

# We need a special testbed that tells us where to find models we can use
if [ -z "${MODELS_DIR:-}" ]; then
	echo "Environment variable MODELS_DIR not set, skipping tests."
	exit 77
elif ! [ -d "$MODELS_DIR" ]; then
	echo "Not a directory: $MODELS_DIR" >&2
	exit 1
fi

if [ -z "${MODEL_NAMES:-}" ]; then
	MODEL_NAMES="$(grep -Ev '^(#.*|[[:space:]]*)$' debian/tests/supported-models.non-free)"
fi

at_least_one=0
exitcode=
for model_name in $MODEL_NAMES; do
	model_fullpath="$MODELS_DIR/$model_name"
	if ! [ -f "$model_fullpath" ]; then
		echo "Model $model_fullpath not found, skipping"
		continue
	fi
	at_least_one=1

	echo "Running tests using model: $model_fullpath"

	java -ea -cp "/usr/share/java/*" "$@" "$model_fullpath" || exitcode=1

	echo "Finished running tests using model: $model_fullpath"
done

# If not a single test could be run, treat this as an overall skip
[ "$at_least_one" -ge 0 ] ||  exit 77
exit $exitcode
