#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export PERL_ARCHLIB := $(shell perl -MConfig -e 'print substr($$Config{vendorarch}, 1)')
# include defs of all DEB_HOST_*/DEB_BUILD_* vars
include /usr/share/dpkg/architecture.mk

%:
	dh $@ --builddirectory --parallel --with systemd

override_dh_auto_clean:
	dh_auto_clean
	dh_auto_clean --builddirectory build-emulator
	rm -rf $(CURDIR)/debian/tmp-emulator

# We run configure with --disable-debug to avoid the default use of -O0 and
# consequent unprotected source functions, causing lintian report
# hardening-no-fortify-functions
# Notice that -g in CFLAGS is still provided by dpkg-buildflags
override_dh_auto_configure:
	dh_auto_configure -- --sysconfdir=/etc/slurm-llnl --localstatedir=/var/run/slurm-llnl --with-munge --without-blcr --enable-pam --without-rpath --disable-debug
	dh_auto_configure --builddirectory build-emulator -- -sysconfdir=/etc/slurm-llnl --localstatedir=/var/run/slurm-llnl --with-munge --without-blcr --enable-pam --without-rpath --disable-debug --enable-front-end --enable-multiple-slurmd

override_dh_auto_build:
	dh_auto_build
	dh_auto_build --builddirectory build-emulator

override_dh_auto_install:
	dh_auto_install
	
	# clear dependency_libs field in *.la files
	# see lintian non-empty-dependency_libs-in-la-file for reference
	sed -i "/dependency_libs/ s/'.*'/''/" `find debian/tmp/ -name '*.la'`
	# Rename slurm(1) manpage before dh_installman run. It is not possible
	# to rename manpages in debian/*.manpages so hardcore `mv` is done here
	mv debian/tmp/usr/share/man/man1/slurm.1 debian/tmp/usr/share/man/man1/slurm-wlm.1
	
	# run make install for libpam-slurm pkg
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/pam install DESTDIR=$(CURDIR)/debian/tmp
	
	# run make install for libslurm[db]-perl pkgs
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/perlapi install DESTDIR=$(CURDIR)/debian/tmp PERL_MM_OPT="INSTALLDIRS=vendor"
	find debian/tmp/$(PERL_ARCHLIB) -type f -name "*.so" | xargs chrpath -d
	
	# run make install for slurm-wlm-torque pkg
	$(MAKE) -C obj-$(DEB_BUILD_GNU_TYPE)/contribs/torque install DESTDIR=$(CURDIR)/debian/tmp
	# build manpages for pbs wrappers
	for i in debian/tmp/usr/bin/pbsnodes debian/tmp/usr/bin/q*; do \
		pod2man $$i > debian/tmp/usr/share/man/man1/$$(basename $$i).1; \
	done

	# Rename binaries that are also in the slurm-wlm-emulator
	mv debian/tmp/usr/sbin/slurmctld debian/tmp/usr/sbin/slurmctld-wlm
	mv debian/tmp/usr/sbin/slurmd debian/tmp/usr/sbin/slurmd-wlm
	mv debian/tmp/usr/sbin/slurmstepd debian/tmp/usr/sbin/slurmstepd-wlm

	dh_auto_install --builddirectory build-emulator  --destdir=$(CURDIR)/debian/tmp-emulator
	# Rename binaries in the emulator packages
	mv debian/tmp-emulator/usr/sbin/slurmctld debian/tmp-emulator/usr/sbin/slurmctld-wlm-emulator
	mv debian/tmp-emulator/usr/sbin/slurmd debian/tmp-emulator/usr/sbin/slurmd-wlm-emulator
	mv debian/tmp-emulator/usr/sbin/slurmstepd debian/tmp-emulator/usr/sbin/slurmstepd-wlm-emulator
	mv debian/tmp-emulator/usr/bin/sshare	debian/tmp-emulator/usr/bin/sshare-emulator
	mv debian/tmp-emulator/usr/bin/scancel 	debian/tmp-emulator/usr/bin/scancel-emulator
	mv debian/tmp-emulator/usr/bin/sattach 	debian/tmp-emulator/usr/bin/sattach-emulator
	mv debian/tmp-emulator/usr/bin/sbatch 	debian/tmp-emulator/usr/bin/sbatch-emulator
	mv debian/tmp-emulator/usr/bin/sreport 	debian/tmp-emulator/usr/bin/sreport-emulator
	mv debian/tmp-emulator/usr/bin/sbcast 	debian/tmp-emulator/usr/bin/sbcast-emulator
	mv debian/tmp-emulator/usr/bin/srun 	debian/tmp-emulator/usr/bin/srun-emulator
	mv debian/tmp-emulator/usr/bin/squeue 	debian/tmp-emulator/usr/bin/squeue-emulator
	mv debian/tmp-emulator/usr/bin/sprio 	debian/tmp-emulator/usr/bin/sprio-emulator
	mv debian/tmp-emulator/usr/bin/smap 	debian/tmp-emulator/usr/bin/smap-emulator
	mv debian/tmp-emulator/usr/bin/sdiag 	debian/tmp-emulator/usr/bin/sdiag-emulator
	mv debian/tmp-emulator/usr/bin/strigger debian/tmp-emulator/usr/bin/strigger-emulator
	mv debian/tmp-emulator/usr/bin/sinfo 	debian/tmp-emulator/usr/bin/sinfo-emulator
	mv debian/tmp-emulator/usr/bin/salloc 	debian/tmp-emulator/usr/bin/salloc-emulator
	mv debian/tmp-emulator/usr/bin/sacct	debian/tmp-emulator/usr/bin/sacct-emulator
	mv debian/tmp-emulator/usr/bin/sacctmgr debian/tmp-emulator/usr/bin/sacctmgr-emulator
	mv debian/tmp-emulator/usr/bin/scontrol debian/tmp-emulator/usr/bin/scontrol-emulator
	mv debian/tmp-emulator/usr/bin/sstat	debian/tmp-emulator/usr/bin/sstat-emulator

override_dh_strip:
	dh_strip -plibslurm29 --dbg-package=libslurm29-dbg
	dh_strip -plibslurmdb29 --dbg-package=libslurmdb29-dbg
	dh_strip -plibpmi0 --dbg-package=libpmi0-dbg
	dh_strip -pslurm-wlm-basic-plugins --dbg-package=slurm-wlm-basic-plugins-dbg
	dh_strip -pslurm-client --dbg-package=slurm-client-dbg
	dh_strip -pslurmd --dbg-package=slurmd-dbg
	dh_strip -pslurmctld --dbg-package=slurmctld-dbg
	dh_strip -pslurmdbd --dbg-package=slurmctld-dbg
	# we do not care about debug symbols for those pkgs
	dh_strip -plibslurmdb-perl
	dh_strip -plibslurm-perl
	dh_strip -psview
	dh_strip -plibpam-slurm
	dh_strip -pslurm-wlm-emulator
	dh_strip -pslurm-client-emulator

override_dh_auto_test:
