#!/bin/bash
set -e

export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=":oversubscribe"

echo "max available processors nproc=`nproc`"

# don't have Makefile.config in runtime CI tests
cd src${HYPRE_LIBNAME_EXT}
cp Makefile Makefile.orig
sed "s|include config/Makefile.config|#include config/Makefile.config|" -i Makefile
cd test
cp Makefile Makefile.orig
sed "s|include ../config/Makefile.config|#include ../config/Makefile.config\n.SUFFIXES:\n.SUFFIXES: .o .obj .f .c .C .cxx .cc\n\nifeq (\$(CUCC), )\n.c.obj:\n\t\$(CC) \$(CFLAGS) -c \$< -o \$@\nelse\n.c.obj:\n\t\$(CUCC) \$(CUFLAGS) -c \$< -o \$@\nendif|" -i Makefile

echo "Building tests for HYPRE${HYPRE_LIBNAME_EXT}"
make clean
rm -f TEST_*.stdout TEST_*.stderr
make    CC=mpicc \
	LINK_CC=mpicc \
	CFLAGS="-I/usr/include/hypre${HYPRE_LIBNAME_EXT} -I/usr/include/superlu -I/usr/include/superlu-dist `pkg-config --cflags mpi` $CFLAGS" \
	LDFLAGS="`pkg-config --libs mpi` -lm" \
	HYPRE_LIB_INSTALL=/usr/lib/`dpkg-architecture -q DEB_HOST_MULTIARCH` \
	HYPRE_LIBNAME_EXT=${HYPRE_LIBNAME_EXT}
cd ..

echo "Running tests for HYPRE${HYPRE_LIBNAME_EXT}"
echo "== running make check (single) =="
make check

echo "== running make check (MPI -P 3 1 1) =="
CHECKRUN="mpirun -n 3" PARMS='-P 3 1 1' make check

echo "== not running make checkpar =="
#make checkpar


cd test
cp Makefile Makefile.test
mv Makefile.orig Makefile

cd ..
cp Makefile Makefile.test
mv Makefile.orig Makefile
