#!/bin/sh

set -e

# Tests not run here are pyunit and synth. These parts are not activated in
# Debian yet.
TESTS="sanity gna vests vpi"


if [ "$2" = mcode ]; then
	BACKEND=mcode
elif [ "$2" = llvm ]; then
	BACKEND=llvm
elif [ "$2" = gcc ]; then
	BACKEND=gcc
else
	echo >&2 "Invalid backend specification"
	exit 1
fi

if [ "$1" = buildtest ]; then
	RUNDIR=testrundir
	mkdir -p "$RUNDIR"
	GHDL="$PWD/$RUNDIR/$BACKEND/usr/bin/ghdl-$BACKEND"
elif [ "$1" = autopkgtest ]; then
	RUNDIR="$AUTOPKGTEST_TMP"
	GHDL=/usr/bin/ghdl-$BACKEND
else
	echo >&2 "Invalid test environment specification"
	exit 1
fi

if [ -z "$RUNDIR" ]; then
	echo >&2 "RUNDIR is empty string"
	exit 1
fi

# Copy testsuite into $RUNDIR to execute there, so that no cleanup is necessary
# (entire $RUNDIR will be deleted later). Also copy src/grt as at least one test
# references files from that using relative paths.
cp -r testsuite "$RUNDIR/"
mkdir -p "$RUNDIR/src"
cp -r src/grt "$RUNDIR/src"

export GHDL
(cd "$RUNDIR/testsuite"; ./testsuite.sh $TESTS)
