#!/bin/sh

exec 2>&1
set -e
#set -x

# test against the default OpenSSL settings and not the Debian-specific ones
export OPENSSL_CONF=`pwd`/debian/openssl.cnf

tests="$@"

cleanup() {
  rm -rf "$AUTOPKGTEST_TMP"
}
if [ -z "$AUTOPKGTEST_TMP" ]; then
  AUTOPKGTEST_TMP=$(mktemp -d)
  trap cleanup INT TERM EXIT
fi

skiplist=$(readlink -f $(dirname $0))/skiplist
excludedir=$(readlink -f $(dirname $0))/excludes
cp -r 'test/' $AUTOPKGTEST_TMP
cp -r 'tool/' $AUTOPKGTEST_TMP
cd $AUTOPKGTEST_TMP

if [ -z "$tests" ]; then
  # FIXME for now, we are excluding the tests for C extensions; couldn't figure
  # out how to properly build them without building everything else
  tests=$(find 'test/' -name 'test_*.rb' -and -not -path '*-ext-*' | sort)
fi

excludes="--excludes-dir=test/excludes/"
excludes="$excludes --excludes-dir=${excludedir}/any/"
excludes="$excludes --excludes-dir=${excludedir}/$(dpkg-architecture -qDEB_HOST_ARCH)/"
excludes="$excludes --excludes-dir=${excludedir}/autopkgtest/"

run_tests=''
for t in $tests; do
  if ! grep -q "^$t$" "$skiplist"; then
    run_tests="$run_tests $t"
  fi
done

ruby2.7 test/runner.rb -v $excludes --name='!/memory_leak/' $run_tests
