#!/usr/bin/make -f
include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

export ACLOCAL_PATH=m4

H2MFLAGS = \
	--manual="CySIgnals Cython package" \
	--source="CySIgnals (Debian $(DEB_VERSION))" \
	--version-string="cysignals-CSI - $(DEB_VERSION)" \
	--locale=C.UTF-8 \
	--no-info

default:
	@uscan --no-conf --dehs --report || true

%:
	dh $@ --buildsystem=pybuild

override_dh_auto_configure-arch:
	$(call adhoc_dh_auto_configure_do,bare)
	$(call adhoc_dh_auto_configure_do,pari)

override_dh_auto_configure-indep:
	$(call adhoc_dh_auto_configure_do,bare)

override_dh_auto_build-arch:
	$(call adhoc_dh_auto_build_do,bare)
	$(call adhoc_dh_auto_build_do,pari)

override_dh_auto_build-indep:
	$(call adhoc_dh_auto_build_do,bare)

override_dh_auto_test-arch:
	$(call adhoc_dh_auto_test_do,bare)
	$(call adhoc_dh_auto_test_do,pari)

override_dh_auto_test-indep:
	$(call adhoc_dh_auto_test_do,bare)

override_dh_prep-indep:
	help2man $(H2MFLAGS) -s 1 \
			-n "debugger information extractor for Python processes" \
			-o cysignals-CSI.1 \
		$(CURDIR)/debian/adhoc/wrappers/cysignals-CSI

override_dh_auto_install-arch:
	$(call adhoc_dh_auto_install_do,bare)
	$(call adhoc_dh_auto_install_do,pari)

override_dh_auto_install-indep:
	$(call adhoc_dh_auto_install_do,bare)

## Shared Objects are actually managed by dh_python:
## so no ad hoc [sub]machinery is needed for them.
override_dh_install-arch:
	true

override_dh_install-indep:
	dh_install --package cysignals-tools --sourcedir=$(CURDIR)/debian/tmp/bare
	dh_install --indep --remaining-packages

override_dh_installdocs:
	dh_installdocs --all $(CURDIR)/debian/README.Debian

DEB_SPHINXDOC_DOCDIR=$(CURDIR)/debian/python-cysignals-doc/usr/share/doc/python-cysignals-doc
override_dh_sphinxdoc-indep:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	$(MAKE) -C docs latexpdf PAPER=a4 LATEXOPTS='-interaction batchmode'
	cp -p $(CURDIR)/docs/build/latex/cysignals.pdf $(DEB_SPHINXDOC_DOCDIR)
	PYTHONPATH=$(CURDIR) python3 -m sphinx -b html $(CURDIR)/docs/source $(DEB_SPHINXDOC_DOCDIR)/html
	dh_sphinxdoc -O--buildsystem=pybuild
endif

override_dh_compress-indep:
	dh_compress -X.pdf -Xexamples

override_dh_auto_clean:
	$(MAKE) distclean

##################################################################################
## ad hoc machinery

## parameter: flavour
adhoc_dh_auto_configure_do = \
	PYSETUP_CONFIGURE_FLAGS=$(PYSETUP_CONFIGURE_FLAGS_$(1)) \
	dh_auto_configure

## parameter: flavour
adhoc_dh_auto_build_do = \
	$(if $(wildcard $(CURDIR)/build/dh-stamp-auto_build-$(1)),, \
		PYSETUP_CONFIGURE_FLAGS=$(PYSETUP_CONFIGURE_FLAGS_$(1)) \
		dh_auto_build -- \
			--before-build "mkdir -v -p {build_dir}/../build-$(1)" \
			--after-build  "\
				mv -v {build_dir}/* {build_dir}/../build-$(1) && \
				echo PYBUILD_interpreter={interpreter} > $(CURDIR)/build/dh-test-auto_build-$(1).sh && \
				echo PYBUILD_build_dir={build_dir}/../build-$(1) >> $(CURDIR)/build/dh-test-auto_build-$(1).sh \
				" $(NEWLINE) \
		$(call adhoc_dh_auto_whatever_do_postamble) $(NEWLINE) \
		touch $(CURDIR)/build/dh-stamp-auto_build-$(1) $(NEWLINE) \
		)

#### parameter: flavour
adhoc_dh_auto_test_do = \
	$(if $(wildcard $(CURDIR)/build/dh-stamp-auto_test-$(1)),, \
	$(info FROZEN: \
		PYSETUP_CONFIGURE_FLAGS=$(PYSETUP_CONFIGURE_FLAGS_$(1)) \
		dh_auto_test -- \
			--before-test "rmdir {build_dir} && ln -vrsf {build_dir}/../build-$(1) {build_dir}" \
			--after-test  "rm -v {build_dir}" ) $(NEWLINE) \
		$(CURDIR)/debian/adhoc/tests/check-doctest $(1) $(NEWLINE) \
		$(CURDIR)/debian/adhoc/tests/check-example $(1) $(NEWLINE) \
		$(call adhoc_dh_auto_whatever_do_postamble) $(NEWLINE) \
		touch $(CURDIR)/build/dh-stamp-auto_test-$(1) $(NEWLINE) \
		)

## parameter: flavour
adhoc_dh_auto_install_do = \
	$(if $(wildcard $(CURDIR)/build/dh-stamp-auto_install-$(1)),, \
		PYSETUP_CONFIGURE_FLAGS=$(PYSETUP_CONFIGURE_FLAGS_$(1)) PYBUILD_DESTDIR=$(CURDIR)/debian/tmp/$(1) \
		dh_auto_install -- \
			--before-install "rmdir {build_dir} && ln -vrsf {build_dir}/../build-$(1) {build_dir}" \
			--after-install  "rm -v {build_dir}" $(NEWLINE) \
		$(call adhoc_dh_auto_whatever_do_postamble) $(NEWLINE) \
		touch $(CURDIR)/build/dh-stamp-auto_install-$(1) $(NEWLINE) \
		)

## parameter: none
adhoc_dh_auto_whatever_do_postamble = \
	rm -vf $(patsubst %.in,%,$(wildcard $(CURDIR)/src/cysignals/*.in))


PYSETUP_CONFIGURE_FLAGS_bare = '--without-pari'
PYSETUP_CONFIGURE_FLAGS_pari = '--with-pari'


define NEWLINE


endef

##################################################################################
