#!/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
}

# skip on debci, no sense to run this, the required cppunit test will definitely not succeed in 160 mins
if [ "`id`" = "debci" ]; then exit 77; fi

# FIXME: skip if we don't have the necessary stuff built

trap "unapply" ERR

echo
echo "====== Patching the tree to only build the smoketest unittest against an existing installation ======"
patch -p1 < ./debian/tests/patches/smoketest-standalone.diff
patch -p0 < ./debian/tests/patches/smoketest-disable-extension-tests.diff

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

echo
echo "====== Configuring MySQL/MariaDB ======"
# Create/change needed stuff for connectivity MySQL unit test
mysql_user=root
mysql_pw=`pwgen -1`
echo "create database testdatabase;" | mysql
# set root password and don't use socket auth
echo "update mysql.user set password=password('${mysql_pw}') where user='${mysql_user}'; update mysql.user set plugin='' where user='${mysql_user}'; flush privileges; " | mysql
export CONNECTIVITY_TEST_MYSQL_DRIVER=${mysql_user}/${mysql_pw}@sdbc:mysql:mysqlc:localhost:3306/testdatabase

echo "====== Running LibreOffice MySQL tests ======"
export OOO_TEST_SOFFICE="${1:-path:/usr/lib/libreoffice/program/soffice}"
export PARALLELISM=$CHECK_PARALLELISM
export LD_LIBRARY_PATH=/usr/lib/libreoffice/program:$LD_LIBRARY_PATH
cd connectivity && make -rk \
	CppunitTest_mysql_test \
	verbose=t

cd $SRCDIR

unapply

