#!/usr/bin/env -S bats --report-formatter junit --formatter tap -j 8
# -*-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=NETCDF
	MODULE=netcdf
	TESTNAME="libs/NetCDF"
	LIBRARY=libnetcdf
	HEADER=netcdf.h
	if [ "${PARALLEL_ENABLED}" == "no" ]; then
		RPM="netcdf-${LMOD_FAMILY_COMPILER}${DELIM}"
		DELIMITER="${LMOD_FAMILY_COMPILER}"
	else
		RPM="netcdf-${LMOD_FAMILY_COMPILER}-${LMOD_FAMILY_MPI}${DELIM}"
		DELIMITER="${LMOD_FAMILY_COMPILER}/${LMOD_FAMILY_MPI}"
	fi

	export PKG MODULE TESTNAME LIBRARY HEADER RPM DELIMITER
}

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

	export OUTPUT
}

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

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

	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 (${DELIMITER})" {
	DIR="${PKG}_DIR"
	if [ -z "${!DIR}" ]; then
		flunk "${PKG}_DIR directory not defined"
	fi

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

# ----------
# Binaries
# ----------

@test "[${TESTNAME}] Verify module ${PKG}_BIN is defined and exists (${DELIMITER})" {
	BIN="${PKG}_BIN"
	if [ -z "${!BIN}" ]; then
		flunk "${PKG}_BIN directory not defined"
	fi

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

@test "[${TESTNAME}] Verify availability of nc-config binary (${DELIMITER})" {
	run which nc-config
	assert_success

	run nc-config --version
	assert_success
}

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

@test "[${TESTNAME}] Verify module ${PKG}_LIB is defined and exists (${DELIMITER})" {
	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
}

@test "[${TESTNAME}] Verify dynamic library available in ${PKG}_LIB (${DELIMITER})" {
	LIB="${PKG}_LIB"

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

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

@test "[${TESTNAME}] Verify static library is not present in ${PKG}_LIB (${DELIMITER})" {
	LIB="${PKG}_LIB"

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

	if [ -e "${!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 (${DELIMITER})" {
	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 "[${TESTNAME}] Verify header file is present in ${PKG}_INC (${DELIMITER})" {
	INC="${PKG}_INC"

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

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