#!/bin/sh

set -e -u

AUTOPKGTEST_TMP=${AUTOPKGTEST_TMP:-$(mktemp -d)}
cp -a . "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"
rm -rf obj obj2 debian

# Build self-tests using default system compiler.  Run all tests except
# "test_syscall" which is written in a style that assumes a 32-bit time_t and
# will fail on all Debian 32-bit platforms since they now have 64-bit time_t.

cd "$AUTOPKGTEST_TMP"
mkdir obj
cd obj
cmake -DUNIT_TESTING=1 -DCMAKE_VERBOSE_MAKEFILE=ON ..
make VERBOSE=1 -C tests/
cd tests
sed -e 's#\(LD_PRELOAD=[^:]*\)[^;]*/\(libuid_wrapper.so\)#\1:\2#' -i CTestTestfile.cmake
make test ARGS="--output-on-failure -E ^test_syscall$"

# Build self-tests using default system compiler, but disable 64-bit time_t.
# This is the way upstream code was written for, and should work on all
# platforms, but on its own is not sufficient evidence of a working
# libuid-wrapper since most applications using libuid-wrapper will be compiled
# with the system compiler with 64-bit time_t on Debian.

cd "$AUTOPKGTEST_TMP"
mkdir obj2
cd obj2
cmake -DUNIT_TESTING=1 -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_C_FLAGS="-U_TIME_BITS  -U_FILE_OFFSET_BITS" ..
make VERBOSE=1 -C tests/
cd tests
sed -e 's#\(LD_PRELOAD=[^:]*\)[^;]*/\(libuid_wrapper.so\)#\1:\2#' -i CTestTestfile.cmake
make test ARGS="--output-on-failure"
