#!/usr/bin/env modbuild

pbuild::pre_configure() {
        pbuild::add_configure_args "--enable-shared"
        pbuild::add_configure_args "--enable-static"

        if (( V_MAJOR < 5 )); then
		pbuild::add_configure_args "--enable-mpi-cxx"
		pbuild::add_configure_args "--enable-mpi-cxx-seek"
		pbuild::add_configure_args "--enable-orterun-prefix-by-default"
        fi
	if (( V_MAJOR < 5 )) && pbuild::use_flag 'slurm'; then
		pbuild::add_configure_args "--with-pmi=/usr"
		pbuild::add_configure_args "--with-pmi-libdir=/usr/lib64/slurmpmi"
	fi
	if (( V_MAJOR >= 5 )); then
		pbuild::add_configure_args "--enable-prte-prefix-by-default"
	fi
        if (( V_MAJOR < 4 )); then
		pbuild::add_configure_args "--enable-mpi-f90"
		pbuild::add_configure_args "--enable-mpi-profile"
		pbuild::add_configure_args "--enable-smp-locks"
	fi
        if (( V_MAJOR >= 4 )); then
		pbuild::add_configure_args "--enable-mpi-fortran"
		pbuild::add_configure_args "--without-verbs"
	fi

	if [[ -v CUDA_VERSION ]]; then
		std::info "Enabling CUDA ${CUDA_VERSION}."
		pbuild::add_configure_args "--with-cuda=${CUDA_HOME}"
	fi

        if [[ -v HWLOC_VERSION ]]; then
                unset HWLOC_VERSION
		std::info "Enabling external hwloc ${HWLOC_PREFIX}."
		pbuild::add_configure_args "--with-hwloc=${HWLOC_PREFIX}"
        else
       		pbuild::add_configure_args "--with-hwloc=internal"
        fi

	if [[ -v LIBEVENT_VERSION ]]; then
		std::info "Enabling libevent ${LIBEVENT_VERSION}."
                pbuild::add_configure_args "--with-libevent=${LIBEVENT_PREFIX}"
        fi 

        if [[ -v PMIX_VERSION ]]; then
		std::info "Enabling PMIX ${PMIX_VERSION}."
                unset PMIX_VERSION
		pbuild::add_configure_args "--with-pmix=${PMIX_PREFIX}"
        fi

	if pbuild::use_flag merlin7 && [[ ! -v LIBFABRIC_VERSION ]]; then
	      if pkg-config --exists libfabric; then
	      	     std::info "Get LIBFABRIC_VERSION via pkg-config"
	      	     LIBFABRIC_PREFIX=$(pkg-config --variable=prefix libfabric)
	             LIBFABRIC_VERSION="$(LIBFABRIC_PREFIX##*/)"
	      fi
	fi
	if [[ -v LIBFABRIC_VERSION ]]; then
		std::info "Enabling libfabric ${LIBFABRIC_VERSION}."
                pbuild::add_configure_args "--with-ofi=${LIBFABRIC_PREFIX}"
        fi 
	
	if [[ -v UCX_VERSION ]]; then
		std::info "Enabling UCX ${UCX_VERSION}."
		pbuild::add_configure_args "--with-ucx=${UCX_PREFIX}"
	fi

        if [[ -v INTEL_VERSION ]]; then
                pbuild::add_configure_args "CC=icc"
                pbuild::add_configure_args "CXX=icpc"
                pbuild::add_configure_args "FC=ifort"
                pbuild::add_configure_args "F90=ifort"
                pbuild::add_configure_args "F77=ifort"
                pbuild::add_configure_args "LDFLAGS=-Wc,-static-intel,-O0"
        fi

        if pbuild::use_flag slurm || \
		pbuild::use_flag dgx || \
		pbuild::use_flag merlin6 || \
		pbuild::use_flag merlin7; then
	    std::info "Enabling SLURM."
	    pbuild::add_configure_args "--with-slurm=yes"
	fi
        if pbuild::use_flag slurm || pbuild::use_flag dgx || pbuild::use_flag merlin6; then
		std::info "Enabling GPFS."
		pbuild::add_configure_args "--with-gpfs=/usr/lpp/mmfs"
        fi

        if pbuild::use_flag "libpmix"; then
		std::info "Enabling install of libpmix."
        	pbuild::add_configure_args "--enable-install-libpmix"
        fi
}

pbuild::post_install_no_slurm() {
	if ! pbuild::use_flag slurm && \
			! pbuild::use_flag dgx && \
			! pbuild::use_flag merlin6 && \
			! pbuild::use_flag merlin7; then
		mkdir -p "${PREFIX}/lib/fallback"
		local -r binary=$(ls "${PREFIX}"/lib/libmpi.so.*.*.*)
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/libuc[mpst].so'
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/libuct_ib.so.0'
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/libnuma.so'
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/libibverbs.so'
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/librdmacm.so'
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/libpmi.so'
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/libpmi2.so'
		pbuild::install_shared_libs "${binary}" "${PREFIX}/lib/fallback" '/libpmi2.so'
	fi
}

pbuild::post_install() {
	if [[ -v CUDA_VERSION ]]; then
		echo "opal_warn_on_missing_libcuda = 0" >> ${PREFIX}/etc/openmpi-mca-params.conf
	fi

	local fname=''
	local old_rpath=''
	local new_rpath=''
	# remove '/usr/lib64' from RPATH/RUNPATH
        while read fname; do
		old_rpath=$(objdump -x "${fname}" | awk '/RPATH|RUNPATH/ {print $2}')
		new_rpath=$(echo "${old_rpath}" | sed 's/:\/usr\/lib64:/:/g')
		if [[ "${old_rpath}" != "${new_rpath}" ]]; then
			patchelf \
				--force-rpath \
				--set-rpath "${new_rpath}" \
				"${fname}"
		fi
        done < <(find "${PREFIX}" -type f \( ! -name "*.a" -and ! -name "*.mod" \) \
		      -exec grep -IL . "{}" \;)
}

# Local Variables:
# mode: sh
# sh-basic-offset: 8
# tab-width: 8
# End:
