#!/bin/sh
# This script was made by Paul Gevers <elbrus@debian.org>
# It is licensed under the same license as the fpc package

set -e # Make sure the test fails if a command fails
echo starting run-upstream-testsuite
FULL_TARGET=`fpc -iSP`-`fpc -iSO` # x86_64-linux
FPC_VER=`fpc -iV`
DEB_HOST_MULTIARCH=`dpkg-architecture -qDEB_HOST_MULTIARCH`
DEB_HOST_ARCH=`dpkg-architecture -qDEB_HOST_ARCH`
# Make sure we use the installed fpcmake
export FPCMAKE=`which fpcmake`-${FPC_VER}
TESTUNITDIRROOT=fpcsrc/tests/tstunits
TESTUNITDIR=${TESTUNITDIRROOT}/${FULL_TARGET}
LIBDIR=/usr/lib/${DEB_HOST_MULTIARCH}/fpc/${FPC_VER}
UTESTS=fpcsrc/tests/utils/testsuite/utests
if [ -f fpcsrc/tests/Makefile ] ; then
	MAKEMAKEFILES=false
else   
	MAKEMAKEFILES=true
fi
echo about to run debian/rules makefiles
[ "$MAKEMAKEFILES" = "true" ] && debian/rules makefiles
echo debian/rules makefiles builds the rtl which caused problems later
echo so clean up the rtl that was just built.
find . -name '*.o' -delete
find . -name '*.ppu' -delete

echo Generate missing configuration file for testsuite
[ -f ${UTESTS}.cfg ] || grep '^  Def' ${UTESTS}.pp > ${UTESTS}.cfg

echo If the make target is run without intervention, the units target is build and
echo installed, while we want to test with the installed units.
[ -d ${TESTUNITDIR} ] || mkdir ${TESTUNITDIR}
for FILE in ${LIBDIR}/units/$FULL_TARGET/*/*.[op]* ; do
	[ -h ${TESTUNITDIR}/${FILE##*/} ] || ln -s ${FILE} ${TESTUNITDIR}
done
echo Compiled > ${TESTUNITDIRROOT}/rtl-stamp.${FULL_TARGET}
echo Compiled > ${TESTUNITDIRROOT}/fpcunit-stamp.${FULL_TARGET}
echo Compiled > ${TESTUNITDIRROOT}/packages-stamp.${FULL_TARGET}

echo 'Some tests need additional objects, lets make them first (why are they not'
echo 'automatically build?)'
mkdir -p fpcsrc/tests/test/cg/obj/${FULL_TARGET##*-}/${FULL_TARGET%%-*} #linux/x86_64
make -C fpcsrc/tests create_c_objects TEST_FPC=`which fpc` TEST_CCOMPILER=gcc TEST_CFLAGS=-fno-stack-protector
echo 'Run the real test suite; this does not fail on its own'
LC_ALL=C.UTF-8 make -C fpcsrc/tests all TEST_FPC=`which fpc`
echo The next line just creates some nice output
make -C fpcsrc/tests digest TEST_FPC=`which fpc`

echo Let\'s save some of the output
if [ -n "${ADT_ARTIFACTS}" ] ; then
	cp -p fpcsrc/tests/output/${FULL_TARGET}/faillist ${ADT_ARTIFACTS}
	cp -p fpcsrc/tests/output/${FULL_TARGET}/log ${ADT_ARTIFACTS}
	cp -p fpcsrc/tests/output/${FULL_TARGET}/longlog ${ADT_ARTIFACTS}
fi

echo Compare the new results with known results if we have them
REF_FILE=debian/tests/ref_fail_${DEB_HOST_ARCH}
if [ -f ${REF_FILE} ] ; then
	echo
	echo Difference between expected failures and current failures:
	diff -u ${REF_FILE} fpcsrc/tests/output/${FULL_TARGET}/faillist || true
	echo
	echo "Checking (and fail if so) for new failures:"
	if diff ${REF_FILE} fpcsrc/tests/output/${FULL_TARGET}/faillist | grep \>\  >/dev/null ; then
		echo "Unexpected failures"
		return 1
	else
		echo "No new failures"
	fi
else
    echo "No reference file found for $DEB_HOST_ARCH. Please consider adding one."
fi

echo For debugging, add clean target
if [ -n "$CLEAN" ] ; then
	make -C fpcsrc/tests digest TEST_FPC=`which fpc`
	debian/rules clean-makefiles
	rm debian/deb-build-fpc.cfg
fi
