#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DH_OPTIONS=${EXTRA_DH_OPTIONS}

DPKG_EXPORT_BUILDFLAGS=1
include /usr/share/dpkg/buildflags.mk

LDFLAGS += -Wl,-O1 -Wl,--as-needed

SPLIT=10

PYTHON3S := $(shell py3versions -vr)
DEFAULT_PYTHON := $(shell py3versions -vd)
ALLPYTHONS := $(PYTHON3S)

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

PYQT_MODULES := chart

PY_PACKAGE_NAMES := $(PYQT_MODULES:%=pyqt5.qt%)

export QT_SELECT=qt5

SHARED_CONFIGURE_OPTIONS=--verbose \
	-q /usr/bin/qmake -c -j $(SPLIT)


.PRECIOUS: build-%/configure-stamp dbg-build-%/configure-stamp

configure: $(ALLPYTHONS:%=build-%/configure-stamp) $(ALLPYTHONS:%=dbg-build-%/configure-stamp)

build-%/configure-stamp:
	dh_testdir
	mkdir -p build-$*
	cd build-$* && ln -sf ../sip .
	cd build-$* && python$* ../configure.py $(SHARED_CONFIGURE_OPTIONS) \
			-d /usr/lib/python$*/dist-packages \
			--sip-incdir /usr/include/python$*$(if $(filter 3.%, $*),m,) \
			--no-sip-files
	sed -i '/strip/d' build-$*/Makefile
	sed -i 's/^CFLAGS[^=]*=/& $$(DEBIAN_CFLAGS) /g' build-$*/*/Makefile
	sed -i 's/^CXXFLAGS[^=]*=/& $$(DEBIAN_CXXFLAGS) /g' build-$*/*/Makefile
	sed -i 's/^LFLAGS[^=]*=/& $$(DEBIAN_LDFLAGS) /g' build-$*/*/Makefile
	touch $@

dbg-build-%/configure-stamp:
	dh_testdir
	mkdir -p dbg-build-$*
	cd dbg-build-$* && ln -sf ../sip .
	cd dbg-build-$* && python$*-dbg ../configure.py $(SHARED_CONFIGURE_OPTIONS) \
			-d /usr/lib/python$*/dist-packages \
			--sip-incdir /usr/include/python$*$(if $(filter 3.%, $*),dm,_d) \
			--debug --no-sip-files
	sed -i '/strip/d' dbg-build-$*/Makefile
	sed -i 's/-isystem/-I/' dbg-build-$*/Makefile
	sed -i 's/^CFLAGS[^=]*=/& $$(DEBIAN_CFLAGS) /g' dbg-build-$*/*/Makefile
	sed -i 's/^CXXFLAGS[^=]*=/& $$(DEBIAN_CXXFLAGS) /g' dbg-build-$*/*/Makefile
	sed -i 's/^LFLAGS[^=]*=/& $$(DEBIAN_LDFLAGS) /g' dbg-build-$*/*/Makefile
	touch $@


build: build-arch build-indep
build-arch: $(ALLPYTHONS:%=build-%/build-stamp) $(ALLPYTHONS:%=dbg-build-%/build-stamp)
build-indep: build-$(DEFAULT_PYTHON)/build-stamp

build-%/build-stamp: build-%/configure-stamp
	dh_testdir
	dh_auto_build --parallel -B build-$* -- DEBIAN_CFLAGS="$(CPPFLAGS) $(CFLAGS)" DEBIAN_CXXFLAGS="$(CPPFLAGS) $(CXXFLAGS)" DEBIAN_LDFLAGS="$(LDFLAGS)"
	touch $@

dbg-build-%/build-stamp: dbg-build-%/configure-stamp
	dh_testdir
	dh_auto_build --parallel -B dbg-build-$* -- DEBIAN_CFLAGS="$(CPPFLAGS) $(CFLAGS)" DEBIAN_CXXFLAGS="$(CPPFLAGS) $(CXXFLAGS)" DEBIAN_LDFLAGS="$(LDFLAGS)"
	touch $@

install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_prep -i

	dh_installdirs -i
	dh_install -i
	dh_installexamples -i
	# Use python3 shebangs
#	find $(CURDIR)/debian/pyqt5chart-dev/usr/share/doc/pyqt5chart-dev/examples -name '*.py' -exec sed -i 's/\/env python/\/python3/g' {} +
	dh_link -i

install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep -a
	dh_installdirs -a
	# This is needed to enforce that the install-arch-% rules are
	# not run in parallel. Both rules install into the same directory,
	# and therefore might try to access the same files at the same time.
	# Therefore, instead of depending on the install-arch-% rules,
	# we invoke them explicitly.
	for p in $(ALLPYTHONS) ; do \
		$(MAKE) -f debian/rules install-arch-$$p;\
	done

	dh_install -a
	dh_missing --fail-missing

	find debian/python*-dbg ! -type d \
		! \( -name '*.so' -o -name '*config_*d*.py' \) | xargs rm -f

d3_qt5_dbg = debian/python3-pyqt5chart-dbg

install-arch-3.%:
	$(MAKE) -C build-3.$* install INSTALL_ROOT=$(CURDIR)/debian/tmp DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C dbg-build-3.$* install INSTALL_ROOT=$(CURDIR)/$(d3_qt5_dbg) DESTDIR=$(CURDIR)/$(d3_qt5_dbg)

	for module in $(PYQT_MODULES); do \
		mkdir -p debian/python3-pyqt5.qt$$module-dbg/usr/lib/python3.$*/dist-packages/PyQt5; \
	done
	mv $(d3_qt5_dbg)/usr/lib/python3.$*/dist-packages/QtChart.so \
		debian/python3-pyqt5.qtchart-dbg/usr/lib/python3.$*/dist-packages/PyQt5
	mkdir -p debian/tmp/usr/lib/python3.$*/dist-packages/PyQt5
	mv debian/tmp/usr/lib/python3.$*/dist-packages/QtChart.so \
		debian/tmp/usr/lib/python3.$*/dist-packages/PyQt5/QtChart.so

clean:
	dh_testdir
	dh_testroot
	rm -rf $(ALLPYTHONS:%=build-%) $(ALLPYTHONS:%=dbg-build-%)
	find . -type f -name .DS_Store -delete
	find . -type f -name *.pyc -delete
	find . -type d -name __pycache__ -delete
	dh_clean

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installman
	dh_installchangelogs NEWS
	dh_installdocs
	dh_sip3
ifeq (,$(findstring -i, $(DH_OPTIONS)))
	set -e; \
	for p in $(PY_PACKAGE_NAMES:%=python3-%); do \
		DH_OPTIONS= dh_strip -p$$p --dbg-package=$$p-dbg; \
		rm -rf debian/$$p-dbg/usr/share/doc/$$p-dbg; \
		mkdir -p debian/$$p-dbg/usr/share/doc; \
		ln -s $$p debian/$$p-dbg/usr/share/doc/$$p-dbg; \
	done
endif
	dh_python3 --no-dbg-cleaning
	dh_fixperms
	dh_compress -X/examples
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: install-indep
	$(MAKE) -f debian/rules DH_OPTIONS="-i ${EXTRA_DH_OPTIONS}" binary-common

# Build architecture dependant packages using the common target.
binary-arch: install-arch
	$(MAKE) -f debian/rules DH_OPTIONS="-a ${EXTRA_DH_OPTIONS}" binary-common


binary: binary-arch binary-indep

.PHONY: build-indep build-arch build clean binary-indep binary-arch binary configure
