#!/bin/bash
set -e

pkg=libsparsepp-dev

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/* "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

# The examples shipped upstream in examples/ reside directly in this directory
# while tests/ contains upstream tests.  Build and run all of them, each one in
# its own directory because some of them write data files.  The symlink lets
# tests/spp_relative_include_test.cc find "../sparsepp/spp.h" as it does in the
# upstream tree.
top="${AUTOPKGTEST_TMP}"
ln -s /usr/include/sparsepp sparsepp
for src in *.cc tests/spp_test.cc tests/spp_alloc_test.cc \
           tests/spp_relative_include_test.cc ; do
    name=$(basename "${src}" .cc)
    mkdir -p "${top}/run/${name}"
    ( cd "${top}/run/${name}" &&
      g++ -O2 -std=c++11 -D_XOPEN_SOURCE=700 -o "${name}" "${top}/${src}" &&
      "./${name}" > /dev/null )
done
