#!/usr/bin/env modbuild

# pbuild::set_download_url "https://www.unidata.ucar.edu/downloads/$P/ftp/$P-$V.tar.gz"
pbuild::set_download_url "https://github.com/Unidata/netcdf-c/archive/refs/tags/v${V_PKG}.tar.gz" "netcdf-${V_PKG}.tar.gz"

pbuild::pre_configure() {
	pbuild::add_configure_args "CXX=${MPICXX}"
	pbuild::add_configure_args "CC=$MPICC"
	pbuild::add_configure_args "CXX=$MPICXX"
	pbuild::add_configure_args "F77=$MPIF77"
	pbuild::add_configure_args "F90=$MPIF90"
	pbuild::add_configure_args "FC=$MPIFC"
	pbuild::add_configure_args "FORTRAN=$MPIFORTRAN"
	pbuild::add_configure_args "--enable-netcdf-4"
	pbuild::add_configure_args "--with-pic"
	pbuild::add_configure_args "--enable-pnetcdf"
	pbuild::add_configure_args "--enable-remote-fortran-bootstrap"
	#pbuild::add_configure_args "--disable-shared"
	case "${COMPILER}" in
		pgi )
			# PGI uses GCC's include files, some object files and
			# the STL implementation!
			# The PGI C pre-processor is broken and doesn't work 
			# for HDF5. We use the pre-processor of the underlying
			# GCC...
			# This is a bit hackish!
			#
			# The following eval sets GCCDIR! Which is something 
			# like:
			# /opt/psi/Programming/gcc/7.3.0/bin/../lib/gcc/x86_64-pc-linux-gnu/7.3.0
			#
			eval $(pgcc -show 2>/dev/null | \
				awk '/^GCCDIR[[:space:]]*=/{gsub(/[[:space:]]/,""); print $0}')
			pbuild::add_configure_args "CPP=${GCCDIR%%/..*}/cpp"
			pbuild::add_configure_args "CFLAGS=-fPIC"
			pbuild::add_configure_args "CXXFLAGS=-fPIC"
			pbuild::add_configure_args "FCFLAGS=-fPIC"
			pbuild::add_configure_args "CC=${MPICC} -fPIC"
			;;
		* )
			pbuild::add_configure_args "CC=${MPICC}"
			;;
	esac
}

pbuild::compile() {
	make check
	make 
	make install
	# configure requires libnetcdf
	LIBRARY_PATH+=":${BUILD_DIR}/liblib/.libs"
	LD_LIBRARY_PATH+=":${BUILD_DIR}/liblib/.libs"
	make build-netcdf-fortran
	make install-netcdf-fortran
}

pbuild::install() {
	:
}

pbuild::add_to_group 'HDF5'
pbuild::install_docfiles 'COPYRIGHT' 'README.md' 'RELEASE_NOTES.md'
