#!/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 "$ADTTMP"
}
if [ -z "$ADTTMP" ]; then
  ADTTMP=$(mktemp -d)
  trap cleanup INT TERM EXIT
fi

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

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

pass=0
fail=0
skip=0
total=0

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/"

for t in $tests; do
  if grep -q "^$t$" "$skiplist"; then
    echo "SKIP $t"
    skip=$(($skip + 1))
  elif ruby2.5 test/runner.rb -v $excludes --name='!/memory_leak/' $t >log 2>&1; then
    echo "PASS $t"
    pass=$(($pass + 1))
    total=$(($total + 1))
  else
    fail=$(($fail + 1))
    total=$(($total + 1))
    echo "FAIL $t"
    echo "FAIL $t" | sed -e 's/./-/g'
    echo
    cat log
  fi
done
rm -f log

echo
echo "Finished"
echo '--------'
echo "   Tests executed: $(($total))"
echo "             PASS: $pass"
echo "             FAIL: $fail"
echo "             SKIP: $skip"
echo

if [ $fail -gt 0 ]; then
  exit 1
fi
