#!/bin/sh
# autopkgtest check based on upstream cmake test ($ make test)
set -ue

mkdir -p $AUTOPKGTEST_TMP/tests
mkdir -p $AUTOPKGTEST_TMP/include/primesieve

cp -vpd -t $AUTOPKGTEST_TMP/include/primesieve include/primesieve/PreSieve.hpp
cp -vpd -t $AUTOPKGTEST_TMP/include/primesieve include/primesieve/PrimeSieve.hpp
cp -vpd -t $AUTOPKGTEST_TMP/include/primesieve include/primesieve/ParallelSieve.hpp
cp -vpd -t $AUTOPKGTEST_TMP/include/primesieve include/primesieve/pod_vector.hpp
cp -vpd -t $AUTOPKGTEST_TMP/include/primesieve include/primesieve/pmath.hpp
cp -vpd -t $AUTOPKGTEST_TMP/include/primesieve include/primesieve/macros.hpp
cp -vpd -t $AUTOPKGTEST_TMP/include/primesieve include/primesieve/calculator.hpp

cp -vpd -t $AUTOPKGTEST_TMP/tests test/*.c
cp -vpd -t $AUTOPKGTEST_TMP/tests test/*.cpp

cd $AUTOPKGTEST_TMP

cat > $AUTOPKGTEST_TMP/tests/GNUmakefile << EOF
#!/usr/bin/make -f

PROGRAMS = \
	\$(patsubst %.c,%,\$(wildcard *.c)) \
	\$(filter-out atomic cpu_info, \$(patsubst %.cpp,%,\$(wildcard *.cpp)) )

default: all

CFLAGS = \$(shell pkg-config primesieve --cflags)
CXXFLAGS = -I../include \$(shell pkg-config primesieve --cflags)
LDLIBS = \$(shell pkg-config primesieve --libs) -pthread

all: build

build: \$(PROGRAMS)

check: build
	\$(foreach tst, \$(PROGRAMS), echo "+-+-+-+ \$(tst) +-+-+-+" ; ./\$(tst) ; )

clean:
	\$(RM) \$(PROGRAMS)

EOF

make -C $AUTOPKGTEST_TMP/tests check

exit 0
