#!/bin/sh

set -eu

WORKDIR=$(mktemp -d)

trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM

# Copy the source tree:
cp -r . $WORKDIR
cd $WORKDIR

# Use installed binaries:
sed -i -e "s/-I\$(top_srcdir)[^[:blank:]]*/\$\$(pkg-config --cflags --libs ticables2)/" \
    -e "s/\$(top_builddir)[^[:blank:]]*/\$\$(pkg-config --libs ticables2)/" \
    -e "s/LDADD/LDFLAGS/" tests/Makefile.am

# Clean if necessary:
if [ -f debian/autoreconf.before ]; then
    dh_auto_clean
    dh_autoreconf_clean
fi

# Reconfigure:
dh_autoreconf
dh_auto_configure -- --enable-libusb10

# Run the tests:
dh_auto_test

# Show if linked to the system's libs:
ldd tests/test_ticables_2 tests/torture_ticables

# Log the test output:
if [ -f tests/test-suite.log ]; then
  cat tests/test-suite.log
fi
