#!../../../common/bats/bin/bats
# -*-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

PKG=SCOTCH
module=scotch
BINARY=gout
LIBRARY=libscotch
HEADER=scotch.h
rpm=scotch-$LMOD_FAMILY_COMPILER${DELIM}

setup() {
	module purge
	module load ohpc
	module load $module
}

@test "[libs/Scotch] Verify $PKG module is loaded and matches rpm version ($LMOD_FAMILY_COMPILER)" {
	module list "$module" | grep "1) $module" >&.cmd_output || exit 1
	run grep "$module" .cmd_output
	assert_success

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

@test "[libs/Scotch] Verify ${PKG}_DIR is defined and directory exists ($LMOD_FAMILY_COMPILER)" {
	PKG_DIR="${PKG}_DIR"

	if [ -z "${!PKG_DIR}" ]; then
		flunk "env setting ${PKG_DIR} not defined"
	fi

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

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

@test "[libs/Scotch] Verify module ${PKG}_LIB is defined and exists ($LMOD_FAMILY_COMPILER)" {
	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 "[libs/Scotch] Verify dynamic library available in ${PKG}_LIB ($LMOD_FAMILY_COMPILER)" {
	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 "[libs/Scotch] Verify static library is not present in ${PKG}_LIB ($LMOD_FAMILY_COMPILER)" {
	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 "[libs/Scotch] Verify module ${PKG}_INC is defined and exists ($LMOD_FAMILY_COMPILER)" {
	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/Scotch] Verify header file is present in ${PKG}_INC ($LMOD_FAMILY_COMPILER)" {
	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
}

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

@test "[libs/Scotch] Verify module ${PKG}_BIN is defined and exists ($LMOD_FAMILY_COMPILER)" {
	BIN="${PKG}_BIN"

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

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

@test "[libs/Scotch] Verify availability of ${BINARY} binary ($LMOD_FAMILY_COMPILER)" {
	run which "${BINARY}"
	assert_success
}

rm -f .cmd_output
