#!/bin/bash
# autopkgtest check: Run smoketest against an installed version of LibreOffice
# (c) 2018 Software in the Public Interest, Inc.
# Authors: Rene Engelhard <rene@debian.org>

set -e
set -E

SRCDIR=`pwd`
CHECK_PARALLELISM=1
if [ -n "$AUTOPKGTEST_TMP" ]; then
	TMP=`mktemp -q -p $AUTOPKGTEST_TMP`
	WORKDIR=`mktemp -q -d -p $AUTOPKGTEST_TMP`
else
	TMP=`mktemp -q`
	WORKDIR=`mktemp -q -d`
fi

function unapply() {
	cd $SRCDIR
	echo
	echo "====== Unapplying the patches ======"
	patch -p1 -R < ./debian/tests/patches/smoketest-standalone.diff
	patch -p0 -R < ./debian/tests/patches/smoketest-disable-extension-tests.diff
	patch -p1 -R < ./debian/tests/patches/make-check-no-uicheck.diff
}

# skip on debci, no sense to run this, this will definitely not succeed in 160 mins
if [ "$AUTOPKGTEST_NORMAL_USER" = "debci" ]; then exit 77; fi

trap "unapply" ERR

echo
echo "====== Patching the tree ======"
patch -p1 < ./debian/tests/patches/smoketest-standalone.diff
patch -p0 < ./debian/tests/patches/smoketest-disable-extension-tests.diff
patch -p1 < ./debian/tests/patches/make-check-no-uicheck.diff

echo
echo "====== Enabling core dumps ======"
# yes, we want core dumps and stack traces
ulimit -c unlimited

# Build the whole LO except checks. Yes, this fundamentally sucks, but
# there's no sane way (or I didn't find it) so  that we can run the 
# C++ unit tests without this.
echo
echo "====== Building LibreOffice ======"
export DEB_BUILD_OPTIONS="nocheck"
./debian/rules build-arch ENABLE_JUNIT4=n
unset DEB_BUILD_OPTIONS

echo "====== run make check ======"
export PARALLELISM=$CHECK_PARALLELISM
./debian/rules check

cd $SRCDIR

unapply

