#!./common/bats/bin/bats
# -*-sh-*-

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

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

PKG=PNETCDF
module=pnetcdf
library=pnetcdf
header=pnetcdf.h
rpm=pnetcdf-$LMOD_FAMILY_COMPILER-$LMOD_FAMILY_MPI${DELIM}

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

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

@test "[libs/PNETCDF] 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 version="$(rpm -q --queryformat='%{VERSION}\n' ${rpm})"
	run cat "${OUTPUT}"
	assert_output "  1) ${module}/$version"
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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