#!/usr/bin/make -f
# Simplified rules using dh-sequencer.
# When dh-sequencer is used, the usual debhelper sequence is driven by
# the dh command; only overrides are required for custom build steps.

export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DEBPREFIX=$(CURDIR)/debian/tmp/usr
PYVER   := $(shell py3versions -vd)
# Whenever libwxgtk3.0-gtk3-dev installed and wx-config present, need custom opts
WXCONFIG_OPTS=$(shell wx-config --toolkit=gtk3 2>/dev/null 1>&2 && echo '--with-wx-config="wx-config --toolkit=gtk3"' || echo '')

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial/libhdf5.so),)
  export DEB_CPPFLAGS_MAINT_APPEND := -I/usr/include/hdf5/serial
  export DEB_LDFLAGS_MAINT_APPEND := -Wl,-L/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial
endif

# default dh sequencer target
%:
	dh $@ --with python3,numpy3

override_dh_auto_clean:
	dh_auto_clean
	# extra cleanup for artefacts automake doesn't know about
	rm -f build-stamp a.out config.status libtool config.log stimfittest.log
	rm -rf src/pystfio/.libs src/stimfit/.libs src/stimfit/py/.libs
	rm -rf ./.libs ./_libs ./.deps ./.stimfit
	rm -rf $(CURDIR)/man
	find -name '*.o' | xargs -r rm -f
	find -name '*.lo' | xargs -r rm -f
	find -name '*.a' | xargs -r rm -f
	find -name '*.la' | xargs -r rm -f
	find -name '*.so' | xargs -r rm -f
	rm -f $(CURDIR)/test.h5

override_dh_auto_configure:
	# first build: python-enabled
	PYTHON=/usr/bin/python$(PYVER) PYTHON_VERSION=$(PYVER) \
		./configure --enable-python --enable-debian --with-biosig \
		--prefix=$(DEBPREFIX) $(WXCONFIG_OPTS)
	
override_dh_auto_build:
	# build & test python-enabled tree
	$(MAKE)
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) check
endif
	# second build: module-only (no tests needed)
	PYTHON=/usr/bin/python$(PYVER) PYTHON_VERSION=$(PYVER) \
		./configure --enable-module --enable-debian --with-biosig \
		--prefix=$(DEBPREFIX) $(WXCONFIG_OPTS)
	$(MAKE)

override_dh_auto_test:
	# tests for the python-enabled build are already run above;
	# skip dh_auto_test so we don’t accidentally test the module-only tree
	:

override_dh_auto_install:
	# install both configurations into debian/tmp
	PYTHON=/usr/bin/python$(PYVER) PYTHON_VERSION=$(PYVER) \
		./configure --enable-python --enable-debian --with-biosig \
		--prefix=$(DEBPREFIX) $(WXCONFIG_OPTS)
	$(MAKE) install
	PYTHON=/usr/bin/python$(PYVER) PYTHON_VERSION=$(PYVER) \
		./configure --enable-module --enable-debian --with-biosig \
		--prefix=$(DEBPREFIX) $(WXCONFIG_OPTS)
	$(MAKE) install
	mkdir -p $(CURDIR)/man && cp ./debian/stimfit.1 ./man/stimfit.1

	dh_installdirs

override_dh_strip:
	# first strip debug symbols for python3-stfio's shared objects explicitly
	# this places them in python3-stfio-dbgsym and avoids polluting stimfit-dbgsym
	dh_strip -ppython3-stfio
	# run a second pass for the remaining binaries, but skip the python module
	# files so they are not pulled into stimfit-dbgsym as well
	dh_strip \
		--exclude=usr/lib/python*/*-packages/stfio/_*.so
