#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --with=systemd

override_dh_auto_configure:
	@# Create build.nocrypto subdirectory and link all files there,
	@# because open-isns does not support out of tree builds.
	[ ! -d build.nocrypto ] || rm -r build.nocrypto
	mkdir build.nocrypto
	find . -maxdepth 1 ! -name ".*" ! -name debian ! -name build.nocrypto -print0 \
		| xargs -0 -I '{}' cp -rs $$PWD/'{}' build.nocrypto/

	@# Don't build with SLP support for now, openslp-dfsg is
	@# orphaned (#565361), and will likely not be part of
	@# Stretch (#795428).
	dh_auto_configure -- --enable-shared --enable-static \
		--with-security --without-slp --with-rundir=/run

	@# Version of libisns0 built without OpenSSL support, so that
	@# GPL programs can link against it.
	dh_auto_configure -Bbuild.nocrypto -- --enable-shared \
		--enable-static --without-security --without-slp \
		--with-rundir=/run

override_dh_auto_build:
	dh_auto_build
	dh_auto_build -Bbuild.nocrypto -- \
		SOLIB=libisns-nocrypto.so.0 \
		SOLIB_SONAME=libisns-nocrypto.so.0 \
		LIB=libisns-nocrypto.a \
		libisns-nocrypto.a libisns-nocrypto.so.0

override_dh_auto_install:
	@# We need to call make install install_hdrs install_lib,
	@# so just add install_hdrs install_lib to the argument
	@# list (install itself is handled by dh_auto_install)
	dh_auto_install -- install_hdrs install_lib
	@# Only install the libraries for the -nocrypto variant.
	make -j1 -C build.nocrypto DESTDIR=$$PWD/debian/tmp \
		SOLIB=libisns-nocrypto.so.0 \
		SOLIB_SONAME=libisns-nocrypto.so.0 \
		LIB=libisns-nocrypto.a \
		install_lib

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build.nocrypto

override_dh_installinit:
	dh_installinit -popen-isns-server --name=isnsd
	dh_installinit -popen-isns-discoveryd --name=isnsdd --no-start
	dh_installinit --remaining-packages

override_dh_makeshlibs:
	dh_makeshlibs -plibisns0 --add-udeb="libisns-nocrypto0-udeb"
	dh_makeshlibs --remaining-packages
