#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

include /usr/share/dpkg/default.mk
include /usr/share/dpkg/buildtools.mk

# The build system builds the Lua module for Lua 5.4 only. We build it for these as well,
# because LuaJIT, which PowerDNS uses, loads modules for Lua 5.1.
LUA_EXTRA_VERSIONS = 5.1

# If the Perl module were built by the Makefile.am, then it would use the build arch's Perl
# instead of properly cross-compiling. Therefore we add --disable-perl and use debhelper
# hooks. Linking is done using LDFROM instead of LIBS, as MakeMaker would pull LIBS into
# the RPATH of the module.
PERL_MM = --buildsystem=perl_makemaker --sourcedirectory=src/perl

# Build without the manual pages with the nodoc profile, which is needed to cross-compile,
# because asciidoc cannot be installed for another architecture
ifneq (,$(filter nodoc,$(DEB_BUILD_PROFILES) $(DEB_BUILD_OPTIONS)))
CONFIGURE_MAN_PAGES = --disable-man-pages
endif

export PYBUILD_SYSTEM=custom
export PYBUILD_CLEAN_ARGS=dh_auto_clean
export PYBUILD_CONFIGURE_ARGS=intltoolize --force --automake; \
	PYTHON={interpreter} dh_auto_configure -- \
	$(CONFIGURE_MAN_PAGES) --disable-perl --with-systemd
export PYBUILD_BUILD_ARGS=dh_auto_build
export PYBUILD_INSTALL_ARGS=dh_auto_install --destdir={destdir}; \
	mkdir -p {destdir}/usr/lib/python{version}/dist-packages; \
	mv {destdir}/usr/lib/python3/dist-packages/_location.so {destdir}/usr/lib/python{version}/dist-packages/_location.so; \
	rm -f {destdir}/usr/lib/python3/dist-packages/_location.la {destdir}/usr/lib/*/libloc.la
export PYBUILD_TEST_ARGS=dh_auto_test

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_auto_configure:
	dh_auto_configure $(PERL_MM) -- INC=-I$(CURDIR)/src LIBS= \
		'LDFROM=$$(OBJECT) -L$(CURDIR)/src/.libs -lloc'

# The module does not link against Lua, which the program that loads it provides,
# so that it does not pull in Lua 5.1 when it is being loaded by LuaJIT
execute_after_dh_auto_build:
	set -e; for version in $(LUA_EXTRA_VERSIONS); do \
		mkdir -p debian/lua$$version; \
		$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC \
			-include config.h -Isrc $$($(PKG_CONFIG) --cflags lua$$version) \
			-o debian/lua$$version/location.so src/lua/*.c -Lsrc/.libs -lloc; \
	done

	dh_auto_build $(PERL_MM)

execute_after_dh_auto_install:
	set -e; for version in $(LUA_EXTRA_VERSIONS); do \
		install -D -m 644 debian/lua$$version/location.so \
			debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lua/$$version/location.so; \
	done

	dh_auto_install $(PERL_MM) --destdir=debian/tmp

# Run the tests of the Lua module with all other versions and the Perl module, too,
# unless we cannot run them due to cross-compilation
execute_after_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	set -e; for version in $(LUA_EXTRA_VERSIONS); do \
		LD_LIBRARY_PATH=$(CURDIR)/src/.libs \
		LUA_CPATH="$(CURDIR)/debian/lua$$version/?.so;;" \
		TEST_DATABASE=$(CURDIR)/data/database.db \
		TEST_SIGNING_KEY=$(CURDIR)/data/signing-key.pem \
			lua$$version tests/lua/main.lua; \
	done

	$(MAKE) testdata.db
	LD_LIBRARY_PATH=$(CURDIR)/src/.libs \
	database=$(CURDIR)/testdata.db \
	keyfile=$(CURDIR)/examples/public-key.pem \
		dh_auto_test $(PERL_MM)
endif
endif

execute_after_dh_auto_clean:
	rm -rf $(foreach version,$(LUA_EXTRA_VERSIONS),debian/lua$(version))

	dh_auto_clean $(PERL_MM)
