export LC_ALL=C.UTF-8
unset LANGUAGE
export QT_QPA_PLATFORM=vnc
unset DBUS_SESSION_BUS_ADDRESS DESKTOP_SESSION DISPLAY WINDOWID
unset GPG_AGENT_INFO MONKEYSPHERE_VALIDATION_AGENT_SOCKET SSH_AUTH_SOCK
unset XDG_CURRENT_DESKTOP XDG_RUNTIME_DIR XDG_SESSION_COOKIE XDM_MANAGED

if ! v=$(dpkg-query -W --showformat='${Version}' qtbase5-dev); then
	print -ru2 -- "E: could not determine qtbase5-dev version"
	exit 255
fi

case x$v {
(x[0-9]*) ;;
(x)	print -ru2 -- "E: empty qtbase5-dev version"
	exit 255 ;;
(x*)	print -ru2 -- "E: implausible qtbase5-dev version ${v@Q}"
	exit 255 ;;
}

if dpkg --compare-versions "$v" lt 5.11; then
	print -ru2 -- "I: Qt5 too old for running the tests, skipping"
	exit 0
fi

set -e
set -o pipefail
rm -rf /tmp/testhome
mkdir /tmp/testhome
export HOME=/tmp/testhome
mkdir /tmp/testhome/.xdg
chmod 0700 /tmp/testhome/.xdg
[[ -n $XDG_RUNTIME_DIR ]] || export XDG_RUNTIME_DIR=/tmp/testhome/.xdg
set -x

objdir=$(perl -MDebian::Debhelper::Dh_Buildsystems -e \
    'buildsystems_init(); print load_buildsystem("cmake", "configure")->get_builddir();')
topdir=$(realpath .)

# parse DEB_BUILD_OPTIONS
j=
J=
fbdebug=0
nocheck=0
for i in "$@"; do
	if [[ $i = parallel ]]; then
		nproc=$(nproc) || nproc=1
		j=-j$nproc
		J=-j
	elif [[ $i = parallel=* ]]; then
		j=-j${i#*=}
		J=$j
	elif [[ $i = fbdebug ]]; then
		fbdebug=1
	elif [[ $i = nocheck ]]; then
		nocheck=1
	fi
done

# parse INSTALL_NAME
i=$(fgrep INSTALL_NAME "$objdir/config.h")
i=${i#*\"}
i=${i%\"*}
if [[ -z $i || $i = *$'\n'* ]]; then
	print -ru2 -- "E: could not determine INSTALL_NAME"
	print -ru2 -- I: output was \<$i\>
	exit 1
fi
i=${i%%+(/)}

# skip the remainder if nocheck
if (( nocheck )); then
	print -ru2 -- "I: tests disabled from DEB_BUILD_OPTIONS"
	exit 0
fi

# for idempotency
rm -rf "$objdir/mtest/share" "$objdir/mtest/libmscore/share"
# create symlinks for running the tests in situ
mkdir "$objdir/mtest/share" "$objdir/mtest/libmscore/share"
ln -s "$topdir/share" "$objdir/mtest/share/$i"
ln -s "$topdir/share" "$objdir/mtest/libmscore/share/$i"

# build the tests
cd "$objdir/mtest"
make $J

# run mtests
set +e
if (( fbdebug )); then
	set -A xvfbargs -- -a -s '-screen 0 1024x768x24 -fbdir /var/tmp'
else
	set -A xvfbargs -- -a
fi
#xvfb-run "${xvfbargs[@]}" -- ctest -O ctest.log $j --output-on-failure
xvfb-run "${xvfbargs[@]}" -- ctest -V -O ctest.log --output-on-failure
rv=$?
set -e +x
if (( rv )); then
	print -ru2 -- "W: mtests failed with errorlevel $rv"
	sed 's/^/N: /' <ctest.log
fi
x=$(grep 'tests.*out of' ctest.log | sed 's/[[0-9;m]*//g')
if [[ $x = '100% tests passed out of '+([0-9]) ]]; then
	:
elif [[ $x != +([0-9.])'% tests passed, 0 tests failed out of '+([0-9]) ]]; then
	print -ru2 -- "W: mtests found failures"
	print -ru2 -- N: $x
fi
print -ru2 "I: mtests run"
