#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DH_ALWAYS_EXCLUDE = __pycache__

PACKAGE := openmpi5-gnu15-ohpc
MPI_STACK := $(patsubst %-ohpc,%,$(PACKAGE))
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version | sed 's/-.*//')
MPI_PREFIX := /opt/ohpc/pub/mpi/$(MPI_STACK)/$(UPSTREAM_VERSION)
MODULE_DIR := /opt/ohpc/pub/moduledeps/gnu15/openmpi5
DOC_DIR := /opt/ohpc/pub/doc/contrib/$(PACKAGE)-$(UPSTREAM_VERSION)
COMPILER_ROOT := /opt/ohpc/pub/compiler/gcc/15.1.0
BUILD_DIR := build
export DPKG_SHLIBDEPS_ARGS = --warnings=0 --ignore-missing-info -l$(CURDIR)/debian/tmp/opt/ohpc/pub/mpi/$(MPI_STACK)/$(UPSTREAM_VERSION)/lib -l$(CURDIR)/debian/tmp/opt/ohpc/pub/mpi/$(MPI_STACK)/$(UPSTREAM_VERSION)/lib/openmpi -xlibnl-route-3-200 -xlibquadmath0 -xlibgcc-s1 -xlibgfortran5 -xlibssl3 -xlibcrypt1

%:
	dh $@

override_dh_autoreconf:
	@true

override_dh_autoreconf_clean:
	@true

override_dh_auto_configure:
	set -e; \
	cp debian/pbs-config ./pbs-config; \
	chmod +x ./pbs-config; \
	rm -rf $(BUILD_DIR); \
	mkdir -p $(BUILD_DIR); \
	cp debian/pbs-config $(BUILD_DIR)/pbs-config; \
	chmod +x $(BUILD_DIR)/pbs-config; \
	UCX_DOC_DIR=$$(find /opt/ohpc/pub/doc/contrib -maxdepth 1 -type d -name 'ucx-ohpc-*' | head -n1); \
	UCX_DOC_VERSION_DIR=""; \
	if [ -n "$$UCX_DOC_DIR" ] && [ -d "$$UCX_DOC_DIR/ucx-ohpc" ]; then \
		UCX_DOC_VERSION_DIR=$$(find "$$UCX_DOC_DIR"/ucx-ohpc -mindepth 1 -maxdepth 1 -type d 2>/dev/null | head -n1); \
		if [ -n "$$UCX_DOC_VERSION_DIR" ]; then \
			UCX_DOC_VERSION_BASENAME=$$(basename "$$UCX_DOC_VERSION_DIR"); \
			if [ -d /opt/ohpc/pub/mpi/ucx-ohpc/$$UCX_DOC_VERSION_BASENAME ] && [ ! -d /opt/ohpc/pub/mpi/ucx-ohpc/$$UCX_DOC_VERSION_BASENAME/include ]; then \
				rm -rf /opt/ohpc/pub/mpi/ucx-ohpc/$$UCX_DOC_VERSION_BASENAME; \
			fi; \
			if [ ! -e /opt/ohpc/pub/mpi/ucx-ohpc/$$UCX_DOC_VERSION_BASENAME ]; then \
				mkdir -p /opt/ohpc/pub/mpi/ucx-ohpc; \
				ln -s "$$UCX_DOC_VERSION_DIR" /opt/ohpc/pub/mpi/ucx-ohpc/$$UCX_DOC_VERSION_BASENAME; \
			fi; \
		fi; \
	fi; \
	UCX_DIR=$$(ls -d /opt/ohpc/pub/mpi/ucx-ohpc/* 2>/dev/null | head -n1); \
	if [ -z "$$UCX_DIR" ] && [ -n "$$UCX_DOC_VERSION_DIR" ]; then \
		UCX_DIR=$$UCX_DOC_VERSION_DIR; \
	fi; \
	if [ ! -d /opt/ohpc/pub/libs/hwloc/lib/pkgconfig ] && [ -d /opt/ohpc/pub/libs/hwloc/lib/x86_64-linux-gnu/pkgconfig ]; then \
		ln -sf x86_64-linux-gnu/pkgconfig /opt/ohpc/pub/libs/hwloc/lib/pkgconfig; \
	fi; \
	if [ ! -e /opt/ohpc/pub/libs/hwloc/lib/libhwloc.so ] && [ -e /opt/ohpc/pub/libs/hwloc/lib/x86_64-linux-gnu/libhwloc.so ]; then \
		ln -sf x86_64-linux-gnu/libhwloc.so /opt/ohpc/pub/libs/hwloc/lib/libhwloc.so; \
	fi; \
	if [ ! -e /opt/ohpc/pub/libs/hwloc/lib/libhwloc.so.15 ] && [ -e /opt/ohpc/pub/libs/hwloc/lib/x86_64-linux-gnu/libhwloc.so.15 ]; then \
		ln -sf x86_64-linux-gnu/libhwloc.so.15 /opt/ohpc/pub/libs/hwloc/lib/libhwloc.so.15; \
	fi; \
	if [ -d /opt/pbs/lib/pkgconfig ] && [ ! -e /opt/pbs/lib/pkgconfig/libz.pc ] && [ -e /usr/lib/x86_64-linux-gnu/pkgconfig/zlib.pc ]; then \
		ln -sf /usr/lib/x86_64-linux-gnu/pkgconfig/zlib.pc /opt/pbs/lib/pkgconfig/libz.pc; \
	fi; \
	export PATH=$(CURDIR):$$PATH:$(COMPILER_ROOT)/bin; \
	export CC=$(COMPILER_ROOT)/bin/gcc; \
	export CXX=$(COMPILER_ROOT)/bin/g++; \
	export FC=$(COMPILER_ROOT)/bin/gfortran; \
	export F77=$(COMPILER_ROOT)/bin/gfortran; \
	export F90=$(COMPILER_ROOT)/bin/gfortran; \
	export LDFLAGS="$(LDFLAGS) -Wl,--as-needed"; \
	export UCX_DIR=$${UCX_DIR:-/opt/ohpc/pub/mpi/ucx-ohpc}; \
	UCX_PKGCONFIG=""; \
	if [ -d "$$UCX_DIR/lib/pkgconfig" ]; then \
		UCX_PKGCONFIG="$$UCX_DIR/lib/pkgconfig:"; \
	fi; \
	export PKG_CONFIG_PATH=/opt/pbs/lib/pkgconfig:$${UCX_PKGCONFIG}/opt/ohpc/pub/libs/hwloc/lib/pkgconfig:/opt/ohpc/pub/libs/hwloc/lib/x86_64-linux-gnu/pkgconfig:$$PKG_CONFIG_PATH; \
	cd $(BUILD_DIR) && ../configure \
		--prefix=$(MPI_PREFIX) \
		--disable-static \
		--enable-builtin-atomics \
		--with-sge \
		--with-libevent=external \
		--with-hwloc=/opt/ohpc/pub/libs/hwloc \
		--with-libfabric \
		--with-ucx=$$UCX_DIR \
		--with-tm \
		--with-pbs-server-home=/var/spool/pbs \
		--with-database-user=postgres \
		CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" FCFLAGS="$(FCFLAGS)"
	rm -f $(BUILD_DIR)/pbs-config

override_dh_auto_build:
	$(MAKE) -C $(BUILD_DIR) -j$(shell nproc)

override_dh_auto_install:
	dh_auto_install --destdir=$(CURDIR)/debian/tmp --builddirectory=$(BUILD_DIR)
	find $(CURDIR)/debian/tmp -name '*.la' -delete
	bin_dir=$(CURDIR)/debian/tmp$(MPI_PREFIX)/bin; \
	if [ -f "$$bin_dir/prun" ]; then mv "$$bin_dir/prun" "$$bin_dir/prrte-prun"; fi
	install -d $(CURDIR)/debian/tmp$(MODULE_DIR)
	{ \
		echo '#%Module1.0#####################################################################'; \
		echo ''; \
		echo 'proc ModulesHelp { } {'; \
		echo ''; \
		echo 'puts stderr " "'; \
		echo 'puts stderr "This module loads the openmpi5 library built with the gnu15 toolchain."'; \
		echo 'puts stderr "\nVersion $(UPSTREAM_VERSION)\n"'; \
		echo ''; \
		echo '}'; \
		echo 'module-whatis "Name: openmpi5 built with gnu15 toolchain"'; \
		echo 'module-whatis "Version: $(UPSTREAM_VERSION)"'; \
		echo 'module-whatis "Category: runtime library"'; \
		echo 'module-whatis "Description: A powerful implementation of MPI/SHMEM"'; \
		echo 'module-whatis "URL: http://www.open-mpi.org"'; \
		echo ''; \
		echo 'set     version            $(UPSTREAM_VERSION)'; \
		echo ''; \
		echo 'setenv          MPI_DIR             /opt/ohpc/pub/mpi/openmpi5-gnu15/$(UPSTREAM_VERSION)'; \
		echo 'setenv          OMPI_MCA_mca_base_component_show_load_errors 0'; \
		echo 'prepend-path    PATH                /opt/ohpc/pub/mpi/openmpi5-gnu15/$(UPSTREAM_VERSION)/bin'; \
		echo 'prepend-path    MANPATH             /opt/ohpc/pub/mpi/openmpi5-gnu15/$(UPSTREAM_VERSION)/share/man'; \
		echo 'prepend-path    LD_LIBRARY_PATH     /opt/ohpc/pub/mpi/openmpi5-gnu15/$(UPSTREAM_VERSION)/lib'; \
		echo 'prepend-path    MODULEPATH          /opt/ohpc/pub/moduledeps/gnu15-openmpi5'; \
		echo 'prepend-path    PKG_CONFIG_PATH     /opt/ohpc/pub/mpi/openmpi5-gnu15/$(UPSTREAM_VERSION)/lib/pkgconfig'; \
		echo ''; \
		echo 'depends-on hwloc'; \
		echo 'depends-on ucx'; \
		echo 'family "MPI"'; \
	} > $(CURDIR)/debian/tmp$(MODULE_DIR)/$(UPSTREAM_VERSION)
	printf '#%%Module1.0#####################################################################\n##\n## version file for openmpi5-%s\n##\nset     ModulesVersion      "%s"\n' $(UPSTREAM_VERSION) $(UPSTREAM_VERSION) > $(CURDIR)/debian/tmp$(MODULE_DIR)/.version.$(UPSTREAM_VERSION)
	install -d $(CURDIR)/debian/tmp$(DOC_DIR)
	install -m 0644 LICENSE $(CURDIR)/debian/tmp$(DOC_DIR)/
	if [ -f README.md ]; then install -m 0644 README.md $(CURDIR)/debian/tmp$(DOC_DIR)/; fi
	if [ -f AUTHORS ]; then install -m 0644 AUTHORS $(CURDIR)/debian/tmp$(DOC_DIR)/; fi

override_dh_install:
	dh_install
	find debian/$(PACKAGE)/opt/ohpc/pub/mpi/openmpi5-gnu15/$(UPSTREAM_VERSION) -name '*.la' -delete

override_dh_shlibdeps:
	@echo "Skipping dh_shlibdeps; runtime dependencies covered via debian/control"

override_dh_auto_test:
	@echo "Test suite not executed for OpenHPC packaging."

override_dh_clean:
	dh_clean
	rm -rf $(BUILD_DIR)
	rm -f pbs-config config.log
