#!/usr/bin/env -S bats --report-formatter junit --formatter tap -j 4
# -*-sh-*-

source ../../../common/test_helper_functions.bash || exit 1
source ../../../common/functions || exit 1

if [ -s ../../../common/TEST_ENV ]; then
	source ../../../common/TEST_ENV
fi

setup_file() {
	PKG=PETSC
	MODULE=petsc
	TESTNAME=libs/PETSc
	LIBRARY=libpetsc
	HEADER=petsc.h

	check_rms

	export PKG MODULE TESTNAME LIBRARY HEADER
}

setup() {
	OUTPUT="$(mktemp)"

	export OUTPUT
}

teardown() {
	rm -f "${OUTPUT}"
}

@test "[${TESTNAME}] Verify ${PKG} module is loaded and matches rpm version (${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	module list "${MODULE}" | grep "1) ${MODULE}" >&"${OUTPUT}" || exit 1
	run grep "${MODULE}" "${OUTPUT}"
	assert_success

	# check version against rpm
	local rpm
	rpm=$(get_rpm_name "${MODULE}")
	local version
	version="$(rpm -q --queryformat='%{VERSION}\n' "${rpm}")"
	run cat "${OUTPUT}"
	assert_output "  1) ${MODULE}/${version}"
}

@test "[${TESTNAME}] Verify module ${PKG}_DIR is defined and exists (${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	PKG_DIR="${PKG}_DIR"

	if [ -z "${!PKG_DIR}" ]; then
		flunk "${PKG}_DIR directory not defined"
	fi

	if [ ! -d "${!PKG_DIR}" ]; then
		flunk "directory ${!PKG_DIR} does not exist"
	fi
}

# ----------
# Lib Tests
# ----------

@test "[${TESTNAME}] Verify module ${PKG}_LIB is defined and exists (${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	PKG_LIB="${PKG}_LIB"

	if [ -z "${!PKG_LIB}" ]; then
		flunk "${PKG}_LIB directory not defined"
	fi

	if [ ! -d "${!PKG_LIB}" ]; then
		flunk "directory ${!PKG_LIB} does not exist"
	fi
}

@test "[${TESTNAME}] Verify dynamic library available in ${PKG}_LIB (${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	PKG_LIB="${PKG}_LIB"

	if [ -z "${!PKG_LIB}" ]; then
		flunk "${PKG}_LIB directory not defined"
	fi

	if [ ! -s "${!PKG_LIB}/${LIBRARY}.so" ]; then
		flunk "${LIBRARY}.so does not exist"
	fi
}

@test "[${TESTNAME}] Verify static library is not present in ${PKG}_LIB (${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	PKG_LIB="${PKG}_LIB"

	if [ -z "${!PKG_LIB}" ]; then
		flunk "${PKG}_LIB directory not defined"
	fi

	if [ -e "${!PKG_LIB}/${LIBRARY}.a" ]; then
		flunk "${LIBRARY}.a exists when not expecting it"
	fi
}

# --------------
# Include Tests
# --------------

@test "[${TESTNAME}] Verify module ${PKG}_INC is defined and exists (${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	PKG_INC="${PKG}_INC"

	if [ -z "${!PKG_INC}" ]; then
		flunk "${PKG}_INC directory not defined"
	fi

	if [ ! -d "${!PKG_INC}" ]; then
		flunk "directory ${!PKG_INC} does not exist"
	fi
}

@test "[${TESTNAME}] Verify header file is present in ${PKG}_INC (${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	PKG_INC="${PKG}_INC"

	if [ -z "${!PKG_INC}" ]; then
		flunk "${PKG}_INC directory not defined"
	fi

	if [ ! -s "${!PKG_INC}/${HEADER}" ]; then
		flunk "${HEADER} file does not exist"
	fi
}

@test "[${TESTNAME}] Sample job (${RESOURCE_MANAGER}/${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI})" {
	if [ ! -x C_test ]; then
		flunk "C_test binary not available"
	fi

	run_mpi_binary ./C_test "atest" 1 1
	assert_success
}
