#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-bindnow

%:
	dh $@ --builddirectory --with bash-completion

override_dh_auto_clean:
	dh_auto_clean
	rm -rvf $(CURDIR)/debian/slurm-smd.install
	rm -rvf $(CURDIR)/debian/slurm-smd-dev.install
	rm -rvf $(CURDIR)/debian/*.service

override_dh_update_autotools_config:

override_dh_autoreconf:

override_dh_auto_configure:
	# Run with --disable-debug to allow optimizations, -g is still provided
	# by dpkg-buildflags.
	dh_auto_configure -- --sysconfdir=/etc/slurm --disable-debug --with-mysql_config --with-slurmrestd --with-pmix --enable-pam --with-pam_dir=/usr/lib/$(DEB_HOST_MULTIARCH)/security --with-systemdsystemunitdir=/lib/systemd/system/ SUCMD=/bin/su SLEEP_CMD=/bin/sleep

override_dh_auto_install:
	dh_auto_install

	rm -r debian/tmp/usr/lib/*/slurm/src

	find debian/tmp/usr/lib/*/ -type f,l -not -name "*.a" -not -name "*.la" -not -name "libpmi*" -not -name "libnss*" -not -name "pam_slurm*" -not -name "*_pbs*" -not -path "*/perl*/*" | sed "s/^debian\/tmp\///" >> debian/slurm-smd.install

	find debian/tmp/usr/include/slurm/ -type f > debian/slurm-smd-dev.install
	find debian/tmp/usr/lib/*/ -type f -not -name "*.so*" -not -name "libpmi*" -not -name "libnss*" -not -name "pam_slurm*" -not -name "*_pbs*" -not -path "*/perl*/*" | sed "s/^debian\/tmp\///" >> debian/slurm-smd-dev.install

	# Build for libpam-slurm-adopt package
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/pam_slurm_adopt install DESTDIR=$(CURDIR)/debian/tmp

	# Build perlapi.
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/perlapi install DESTDIR=$(CURDIR)/debian/tmp PERL_MM_OPT="INSTALLDIRS=vendor"

	# Build for libpmi0 package
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/pmi install DESTDIR=$(CURDIR)/debian/tmp

	# Build for libpmi2-0 package
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/pmi2 install DESTDIR=$(CURDIR)/debian/tmp

	# Build torque wrappers
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/torque install DESTDIR=$(CURDIR)/debian/tmp

	# Build OpenLava/LSF wrappers
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/openlava install DESTDIR=$(CURDIR)/debian/tmp

	# Build nss_slurm
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/nss_slurm install DESTDIR=$(CURDIR)/debian/tmp

	# Place systemd unit files where debian packaging will pick them up.
	mv -v debian/tmp/lib/systemd/system/slurmctld.service debian/slurm-smd-slurmctld.slurmctld.service
	mv -v debian/tmp/lib/systemd/system/slurmd.service debian/slurm-smd-slurmd.slurmd.service
	mv -v debian/tmp/lib/systemd/system/slurmdbd.service debian/slurm-smd-slurmdbd.slurmdbd.service
	mv -v debian/tmp/lib/systemd/system/slurmrestd.service debian/slurm-smd-slurmrestd.slurmrestd.service
	mv -v debian/tmp/lib/systemd/system/sackd.service debian/slurm-smd-sackd.sackd.service

override_dh_installinit:
	dh_installinit -pslurm-smd-slurmctld --name=slurmctld
	dh_installinit -pslurm-smd-slurmd --name=slurmd
	dh_installinit -pslurm-smd-slurmdbd --name=slurmdbd
	dh_installinit -pslurm-smd-slurmrestd --name=slurmrestd
	dh_installinit -pslurm-smd-sackd --name=sackd

override_dh_installsystemd:
	dh_installsystemd -pslurm-smd-slurmctld --name=slurmctld
	dh_installsystemd -pslurm-smd-slurmd --name=slurmd
	dh_installsystemd -pslurm-smd-slurmdbd --name=slurmdbd
	dh_installsystemd -pslurm-smd-slurmrestd --name=slurmrestd
	dh_installsystemd -pslurm-smd-sackd --name=sackd

override_dh_auto_test:
