#!/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-*' -and -not -path '*/mkmf/*' | 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/"

# dpkg-source does not allow empty file creatian via a patch, the files below
# are created during build time to run tests, we need to do the same for
# autopkgtest.
empty_files="$AUTOPKGTEST_TMP/test/openssl/fixtures/pkey/empty.pem"
for f in $empty_files; do
  touch $f
done

run_tests=''
for t in $tests; do
  if grep -q "^$t$" "$skiplist"; then
    continue
  fi
  case "$t" in
    test/rubygems/*)
      continue
      ;;
  esac

  run_tests="$run_tests $t"
done

ruby3.4 test/runner.rb -v $excludes --name='!/memory_leak/' $run_tests
