#!/usr/bin/env modbuild

unset MACOSX_DEPLOYMENT_TARGET

SOURCE_URL="http://glaros.dtc.umn.edu/gkhome/fetch/sw/$P/$P-$V.tar.gz"

declare -ri MAJOR_VERSION=${V%%.*}

case ${MAJOR_VERSION} in
3 )
	declare -r docfiles="CHANGES INSTALL LICENSE.txt README VERSION"
	;;
4 )
	declare -r docfiles="LICENSE.txt"
	;;
esac

pbuild::configure() {
	CC=$MPICC
	CXX=$MPICXX
	F77=$MPIF77
	F90=$MPIF90
	FC=$MPIFC
	FORTRAN=$MPIFORTRAN
	if (( MAJOR_VERSION == 4 )) ; then
		cmake \
			-DCMAKE_INSTALL_PREFIX="${PREFIX}" \
			-DMETIS_PATH="${MODULE_SRCDIR}/metis" \
			-DGKLIB_PATH="${MODULE_SRCDIR}/metis/GKlib" \
			"${MODULE_SRCDIR}" \
			|| exit 1
	fi
}

pbuild::build() {
	CC=$MPICC
	CXX=$MPICXX
	F77=$MPIF77
	F90=$MPIF90
	FC=$MPIFC
	FORTRAN=$MPIFORTRAN
	if (( MAJOR_VERSION == 3 )) ; then
		cd "${MODULE_SRCDIR}"
		make -e -j3 || exit 1

		mkdir -p $PREFIX/include/metis
		mkdir -p $PREFIX/lib

		cp *.h $PREFIX/include
		cp METISLib/*.h $PREFIX/include/metis
		cp lib*.a $PREFIX/lib
	elif (( MAJOR_VERSION == 4 )); then
		make -j3 || exit 1
		make install

		LIBMETIS_A=$(find . -name libmetis.a)
		METIS_H=$(find "${MODULE_SRCDIR}" -name metis.h)

		install -m 0644 $METIS_H    $PREFIX/include
		install -m 0644 $LIBMETIS_A $PREFIX/lib
	else
		die 42 "Unsupported version: $V"
	fi
}

pbuild::install() {
	:
}


pbuild::add_to_group 'MPI'
pbuild::set_docfiles ${docfiles} 
pbuild::make_all
pbuild::cleanup_src

