# tests adapted from the test_build target of the toplevel makefile

export OMPI_MCA_plm_rsh_agent=/bin/false
export OMPI_MCA_rmaps_base_oversubscribe=1

PETSC_DIR=/usr/lib/petsc

echo "Running test examples to verify correct installation"
echo "Using PETSC_DIR=${PETSC_DIR}"

cp makefile makefile.orig
sed "s|include ././\${PETSC_ARCH}/lib/petsc/conf/petscvariables|include \${PETSC_DIR}/lib/petsc/conf/petscvariables|" -i makefile

BASEDIR=`pwd`
TESTDIR=src/snes/examples/tutorials

cd ${TESTDIR}
make PETSC_ARCH=${PETSC_ARCH}  PETSC_DIR=${PETSC_DIR} clean

echo "run SNES testex19"
make PETSC_ARCH=${PETSC_ARCH}  PETSC_DIR=${PETSC_DIR} testex19

echo "test HYPRE"
make PETSC_ARCH=${PETSC_ARCH}  PETSC_DIR=${PETSC_DIR} DIFF=${PETSC_DIR}/lib/petsc/bin/petscdiff runex19_hypre | tee hypre.diff

echo "test MUMPS"
make PETSC_ARCH=${PETSC_ARCH}  PETSC_DIR=${PETSC_DIR} DIFF=${PETSC_DIR}/lib/petsc/bin/petscdiff runex19_fieldsplit_mumps | tee mumps.diff

# don't test runex19_superlu_dist, SuperLU-Dist does not play well with others
# (needs ParMETIS, Scotch-parmetis does not set perm_c; else segfaults on DestroyLU)

echo "run testex5f"
make PETSC_ARCH=${PETSC_ARCH}  PETSC_DIR=${PETSC_DIR} testex5f

make PETSC_ARCH=${PETSC_ARCH}  PETSC_DIR=${PETSC_DIR} clean

cd ${BASEDIR}

echo "Completed test examples"
mv makefile.orig makefile

if [ -n "$( cat ${TESTDIR}/hypre.diff ${TESTDIR}/mumps.diff )" ]; then
  echo "Tests failed"
  exit 1
else
  rm -f ${TESTDIR}/hypre.diff ${TESTDIR}/mumps.diff
fi
