#!/bin/sh
# autopkgtest check: Build and run the upstream testsuite

set -ex

# Require $AUTOPKGTEST_TMP for temporary build files
if [ -z "$AUTOPKGTEST_TMP" ]
then
	echo "Required envvar \"$AUTOPKGTEST_TMP\"is not set" >&2
	exit 1
fi

# Build tests
CSFML_SOURCE="$PWD"
cd "$AUTOPKGTEST_TMP"

cp "$CSFML_SOURCE/debian/tests/CMakeLists.txt.upstream" CMakeLists.txt
cmake . -DCSFML_SOURCE="$CSFML_SOURCE" -DCMAKE_BUILD_TYPE=Debug
make "-j$(nproc)" VERBOSE=1
echo "build: OK"

# WIP
ctest --verbose
echo "tests: OK"
exit 0

# Spawn a dummy X server to run the tests in
export DISPLAY=:99
cp "$CSFML_SOURCE/debian/tests/xorg-dummy.conf" .

trap 'kill $XORG_PID' INT TERM EXIT
Xorg -noreset -logfile ./xorg.log -config ./xorg-dummy.conf "$DISPLAY" &
XORG_PID=$!
sleep 1

# Run the tests
#  TODO The sf::Window tests fail with the dummy Xorg server because
#  the server does not present any visuals with MSAA support.
#  My hunch is it's related to this Mesa MR:
#  https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/31019
ctest --verbose --exclude-regex 'sf::Window$'
echo "tests: OK"
