#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules file for libcap-ng
# Written by Pierre Chifflier <pollux@debian.org>

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

export DEB_BUILD_HARDENING=1

ifneq ($(filter nopython,$(DEB_BUILD_PROFILES)),)
CONFIGURE_FLAGS = --without-python --without-python3
else
PY3DEFAULTVER := $(shell py3versions --default --version)
PY3VERS := $(shell py3versions --supported --version)
PY3VERS := $(filter-out $(PY3DEFAULTVER), $(PY3VERS))
CONFIGURE_FLAGS = --with-python3
endif

override_dh_auto_configure: $(PY3VERS:%=override_dh_auto_configure-%)

override_dh_auto_configure-%:
	dh_auto_configure -- \
		$(CONFIGURE_FLAGS)
	cp -lpr bindings/python3 bindings/python$*

override_dh_auto_build: $(PY3VERS:%=override_dh_auto_build-%)

override_dh_auto_build-%:
	dh_auto_build
	PYTHON=/usr/bin/python$* $(MAKE) -C bindings/python$* py3execdir=/usr/lib/python$*/dist-packages \
	    PYTHON3_CFLAGS="$(shell python$*-config --cflags 2> /dev/null)" PYTHON3_LIBS="$(shell python$*-config --libs 2> /dev/null)" PYTHON3_INCLUDES="$(shell python$*-config --includes 2> /dev/null)"

debian/install-python-stamp:
	dh_testdir
	dh_auto_install
	touch $@

debian/install-python%-stamp: debian/install-python-stamp
	PYTHON=/usr/bin/python$* $(MAKE) -C bindings/python$* py3execdir=/usr/lib/python$*/dist-packages DESTDIR=$(CURDIR)/debian/tmp install \
	    PYTHON3_CFLAGS="$(shell python$*-config --cflags 2> /dev/null)" PYTHON3_LIBS="$(shell python$*-config --libs 2> /dev/null)" PYTHON3_INCLUDES="$(shell python$*-config --includes 2> /dev/null)"
	touch $@

override_dh_install: debian/install-python-stamp $(PY3VERS:%=debian/install-python%-stamp)
	mkdir -p $(CURDIR)/debian/tmp/lib/$(DEB_HOST_MULTIARCH) && \
	mv $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lib*.so.0* $(CURDIR)/debian/tmp/lib/$(DEB_HOST_MULTIARCH)/; \
	rm debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcap-ng.so; \
	ln -s ../../../lib/$(DEB_HOST_MULTIARCH)/libcap-ng.so.0.0.0 debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcap-ng.so; \
	dh_install --exclude=.la

override_dh_auto_clean:
	dh_auto_clean
	$(RM) debian/*-stamp
	-rm -rf debian/tmp-python-cap-ng

override_dh_auto_test:
	# do nothing, some tests are failing because of failing relink
	:

%:
ifeq ($(filter nopython,$(DEB_BUILD_PROFILES)),)
	dh $@ --with=python3,autoreconf
else
	dh $@ --with=autoreconf
endif
