#!/usr/bin/make -f

# Disable dpkg hardening - breaks Slurm plugin stack
export DEB_BUILD_MAINT_OPTIONS = hardening=-all
export CFLAGS =
export CXXFLAGS =
export LDFLAGS = -Wl,-z,lazy -L/opt/ohpc/admin/pmix/lib -L/opt/ohpc/pub/libs/hwloc/lib -Wl,-rpath,/opt/ohpc/admin/pmix/lib -Wl,-rpath,/opt/ohpc/pub/libs/hwloc/lib
export LD_LIBRARY_PATH := /opt/ohpc/admin/pmix/lib:/opt/ohpc/pub/libs/hwloc/lib:$(LD_LIBRARY_PATH)
export LIBRARY_PATH = /opt/ohpc/admin/pmix/lib:/opt/ohpc/pub/libs/hwloc/lib
export CPPFLAGS = -I/opt/ohpc/admin/pmix/include -I/opt/ohpc/pub/libs/hwloc/include

SLURM_VERSION := 25.05.7
INSTALL_PATH  := /usr
HWLOC_PATH    := /opt/ohpc/pub/libs/hwloc
PMIX_PATH     := /opt/ohpc/admin/pmix
SRCDIR        := $(CURDIR)/slurm-$(SLURM_VERSION)
PKGDIR        := $(CURDIR)/debian/tmp

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
PAM_PATH      = /lib/$(DEB_HOST_MULTIARCH)/security

%:
	dh $@

override_dh_auto_configure:
	test -s SOURCES/slurm-$(SLURM_VERSION).tar.bz2 || \
		wget -q -O SOURCES/slurm-$(SLURM_VERSION).tar.bz2 \
			https://download.schedmd.com/slurm/slurm-$(SLURM_VERSION).tar.bz2
	rm -rf $(SRCDIR)
	tar xf SOURCES/slurm-$(SLURM_VERSION).tar.bz2
	cd $(SRCDIR) && patch -p1 < ../SOURCES/slurm.conf.example.patch || true
	cd $(SRCDIR) && ./configure \
		--prefix=$(INSTALL_PATH) \
		--sysconfdir=/etc/slurm \
		--libdir=$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH) \
		--with-systemdsystemunitdir=/lib/systemd/system \
		--enable-multiple-slurmd \
		--with-pmix=$(PMIX_PATH) \
		--with-hwloc=$(HWLOC_PATH) \
		--with-pam_dir=$(PAM_PATH) \
		--with-munge \
		--disable-slurmrestd \
		--without-x11 || { cat $(SRCDIR)/config.log | tail -100; exit 1; }

override_dh_auto_build:
	cd $(SRCDIR) && $(MAKE) -j$$(nproc)

override_dh_auto_install:
	cd $(SRCDIR) && $(MAKE) DESTDIR=$(PKGDIR) install
	cd $(SRCDIR) && $(MAKE) DESTDIR=$(PKGDIR) install-contrib
	find $(PKGDIR) -name '*.la' -delete
	# Install epilog script
	mkdir -p $(PKGDIR)/etc/slurm
	install -m 755 SOURCES/slurm.epilog.clean $(PKGDIR)/etc/slurm/
	# Split packages using debian/tmp as staging area
	# slurm-ohpc: core binaries and libraries
	mkdir -p $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/bin
	mkdir -p $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)
	mkdir -p $(CURDIR)/debian/slurm-ohpc/etc/slurm
	cp -a $(PKGDIR)$(INSTALL_PATH)/bin/s* $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/bin/ 2>/dev/null || true
	cp -a $(PKGDIR)$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/libslurm*.so* $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/ 2>/dev/null || true
	cp -a $(PKGDIR)$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/ 2>/dev/null || true
	find $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm -name '*.a' -delete 2>/dev/null || true
	rm -f $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm/accounting_storage_mysql.so
	rm -f $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm/job_submit_pbs.so
	rm -f $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm/spank_pbs.so
	mkdir -p $(CURDIR)/debian/slurm-example-configs-ohpc/etc/slurm
	cp -a $(SRCDIR)/etc/*.example $(CURDIR)/debian/slurm-example-configs-ohpc/etc/slurm/ 2>/dev/null || true
	cp -a $(PKGDIR)/etc/slurm/* $(CURDIR)/debian/slurm-example-configs-ohpc/etc/slurm/ 2>/dev/null || true
	# Man pages
	if [ -d $(PKGDIR)$(INSTALL_PATH)/share/man ]; then \
		mkdir -p $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/share; \
		cp -a $(PKGDIR)$(INSTALL_PATH)/share/man $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/share/; \
	fi
	# slurm-devel-ohpc: headers
	mkdir -p $(CURDIR)/debian/slurm-devel-ohpc$(INSTALL_PATH)/include
	cp -a $(PKGDIR)$(INSTALL_PATH)/include/slurm $(CURDIR)/debian/slurm-devel-ohpc$(INSTALL_PATH)/include/ 2>/dev/null || true
	# slurm-slurmctld-ohpc
	mkdir -p $(CURDIR)/debian/slurm-slurmctld-ohpc$(INSTALL_PATH)/sbin
	mkdir -p $(CURDIR)/debian/slurm-slurmctld-ohpc/lib/systemd/system
	cp -a $(PKGDIR)$(INSTALL_PATH)/sbin/slurmctld $(CURDIR)/debian/slurm-slurmctld-ohpc$(INSTALL_PATH)/sbin/ 2>/dev/null || true
	cp -a $(PKGDIR)/lib/systemd/system/slurmctld.service $(CURDIR)/debian/slurm-slurmctld-ohpc/lib/systemd/system/ 2>/dev/null || true
	# slurm-slurmd-ohpc
	mkdir -p $(CURDIR)/debian/slurm-slurmd-ohpc$(INSTALL_PATH)/sbin
	mkdir -p $(CURDIR)/debian/slurm-slurmd-ohpc/lib/systemd/system
	cp -a $(PKGDIR)$(INSTALL_PATH)/sbin/slurmd $(CURDIR)/debian/slurm-slurmd-ohpc$(INSTALL_PATH)/sbin/ 2>/dev/null || true
	cp -a $(PKGDIR)$(INSTALL_PATH)/sbin/slurmstepd $(CURDIR)/debian/slurm-slurmd-ohpc$(INSTALL_PATH)/sbin/ 2>/dev/null || true
	cp -a $(PKGDIR)/lib/systemd/system/slurmd.service $(CURDIR)/debian/slurm-slurmd-ohpc/lib/systemd/system/ 2>/dev/null || true
	# slurm-slurmdbd-ohpc
	mkdir -p $(CURDIR)/debian/slurm-slurmdbd-ohpc$(INSTALL_PATH)/sbin
	mkdir -p $(CURDIR)/debian/slurm-slurmdbd-ohpc/lib/systemd/system
	cp -a $(PKGDIR)$(INSTALL_PATH)/sbin/slurmdbd $(CURDIR)/debian/slurm-slurmdbd-ohpc$(INSTALL_PATH)/sbin/ 2>/dev/null || true
	mkdir -p $(CURDIR)/debian/slurm-slurmdbd-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm
	cp -a $(PKGDIR)$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm/accounting_storage_mysql.so $(CURDIR)/debian/slurm-slurmdbd-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm/ 2>/dev/null || true
	cp -a $(PKGDIR)/lib/systemd/system/slurmdbd.service $(CURDIR)/debian/slurm-slurmdbd-ohpc/lib/systemd/system/ 2>/dev/null || true
	# slurm-libpmi-ohpc
	mkdir -p $(CURDIR)/debian/slurm-libpmi-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)
	cp -a $(PKGDIR)$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/libpmi*.so* $(CURDIR)/debian/slurm-libpmi-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/ 2>/dev/null || true
	# slurm-contribs-ohpc
	mkdir -p $(CURDIR)/debian/slurm-contribs-ohpc$(INSTALL_PATH)/bin
	for f in seff sjobexitmod sjstat smail; do cp -a $(PKGDIR)$(INSTALL_PATH)/bin/$$f $(CURDIR)/debian/slurm-contribs-ohpc$(INSTALL_PATH)/bin/ 2>/dev/null || true; rm -f $(CURDIR)/debian/slurm-ohpc$(INSTALL_PATH)/bin/$$f; done
	# slurm-openlava-ohpc
	mkdir -p $(CURDIR)/debian/slurm-openlava-ohpc$(INSTALL_PATH)/bin
	for f in bjobs bkill bsub lsid; do cp -a $(PKGDIR)$(INSTALL_PATH)/bin/$$f $(CURDIR)/debian/slurm-openlava-ohpc$(INSTALL_PATH)/bin/ 2>/dev/null || true; done
	# slurm-perlapi-ohpc
	mkdir -p $(CURDIR)/debian/slurm-perlapi-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/perl
	cp -a $(PKGDIR)$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/perl/* $(CURDIR)/debian/slurm-perlapi-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/perl/ 2>/dev/null || true
	find $(CURDIR)/debian/slurm-perlapi-ohpc -name perllocal.pod -delete 2>/dev/null || true
	find $(CURDIR)/debian/slurm-perlapi-ohpc -name .packlist -delete 2>/dev/null || true
	# slurm-sackd-ohpc
	mkdir -p $(CURDIR)/debian/slurm-sackd-ohpc$(INSTALL_PATH)/sbin
	mkdir -p $(CURDIR)/debian/slurm-sackd-ohpc/lib/systemd/system
	cp -a $(PKGDIR)$(INSTALL_PATH)/sbin/sackd $(CURDIR)/debian/slurm-sackd-ohpc$(INSTALL_PATH)/sbin/ 2>/dev/null || true
	cp -a $(PKGDIR)/lib/systemd/system/sackd.service $(CURDIR)/debian/slurm-sackd-ohpc/lib/systemd/system/ 2>/dev/null || true
	# slurm-torque-ohpc
	mkdir -p $(CURDIR)/debian/slurm-torque-ohpc$(INSTALL_PATH)/bin
	mkdir -p $(CURDIR)/debian/slurm-torque-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm
	for f in pbsnodes qalter qdel qhold qrerun qrls qstat qsub mpiexec generate_pbs_nodefile; do cp -a $(PKGDIR)$(INSTALL_PATH)/bin/$$f $(CURDIR)/debian/slurm-torque-ohpc$(INSTALL_PATH)/bin/ 2>/dev/null || true; done
	for f in job_submit_pbs.so spank_pbs.so; do cp -a $(PKGDIR)$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm/$$f $(CURDIR)/debian/slurm-torque-ohpc$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH)/slurm/ 2>/dev/null || true; done
	# slurm-pam-slurm-ohpc
	mkdir -p $(CURDIR)/debian/slurm-pam-slurm-ohpc$(PAM_PATH)
	cp -a $(PKGDIR)$(PAM_PATH)/pam_slurm*.so $(CURDIR)/debian/slurm-pam-slurm-ohpc$(PAM_PATH)/ 2>/dev/null || true
	# slurm-slurmrestd-ohpc
	mkdir -p $(CURDIR)/debian/slurm-slurmrestd-ohpc$(INSTALL_PATH)/sbin
	mkdir -p $(CURDIR)/debian/slurm-slurmrestd-ohpc/lib/systemd/system
	cp -a $(PKGDIR)$(INSTALL_PATH)/sbin/slurmrestd $(CURDIR)/debian/slurm-slurmrestd-ohpc$(INSTALL_PATH)/sbin/ 2>/dev/null || true
	cp -a $(PKGDIR)/lib/systemd/system/slurmrestd.service $(CURDIR)/debian/slurm-slurmrestd-ohpc/lib/systemd/system/ 2>/dev/null || true

override_dh_missing:
	dh_missing --list-missing

override_dh_auto_clean:
	rm -rf $(SRCDIR)

override_dh_shlibdeps:
	dh_shlibdeps -l$(INSTALL_PATH)/lib/$(DEB_HOST_MULTIARCH):$(PMIX_PATH)/lib:$(HWLOC_PATH)/lib -- --ignore-missing-info || true

override_dh_strip:
	dh_strip --no-automatic-dbgsym || true

override_dh_auto_test:
