#!/usr/bin/make -f

export CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
export CPPFLAGS=$(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS)
export QT_SELECT := qt5

# Get the supported Python versions
PY3VERS = $(shell py3versions -r -v)

# Callable functions to determine the correct PYTHONPATH
# (pyinstaller moved from pythonX.Y ->cpython[23] - this is for compatibility)
pythonpath = $$(ls -d $(CURDIR)/.pybuild/pythonX.Y_$(1)/build \
	$(CURDIR)/.pybuild/cpython3_$(1)/build)

# examples are not installed, but included manually in package
export PYBUILD_INSTALL_ARGS = \
	--veusz-resource-dir=debian/tmp/usr/share/veusz \
	--disable-install-examples
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --with python3,sphinxdoc --buildsystem=pybuild

override_dh_auto_build: export http_proxy=127.0.0.1:9
override_dh_auto_build: export https_proxy=127.0.0.1:9
override_dh_auto_build: delete_generated
	dh_auto_build

# desktop files are copied from source package
	cp -p support/veusz.desktop debian/
	cp -p support/veusz.xml debian/veusz.sharedmimeinfo

# regenerate docs
	PYTHONPATH=. sphinx-build -N -bhtml Documents/manual-source build/html
	pod2man --release=`cat VERSION` --center="Veusz"  Documents/man-page/veusz.pod build/veusz.1

override_dh_auto_install:
# do not install examples and put data files in /usr/share/veusz
	dh_auto_install

# veusz follows symlink to find its resources
	for py in $(PY3VERS); do \
		dh_link -ppython3-veusz usr/share/veusz usr/lib/python$${py}/dist-packages/veusz/resources; \
	done
# COPYING is symlinked in veusz package
	rm -f debian/tmp/usr/share/veusz/COPYING

	dh_numpy3
	dh_sip3

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -e -x;\
	for py in $(PY3VERS); do \
		PYTHONPATH=$(call pythonpath,$$py) \
			VEUSZ_RESOURCE_DIR=. \
			QT_QPA_PLATFORM=minimal \
			python$$py tests/runselftest.py ;\
	done
endif

override_dh_compress:
# don't compress Veusz example files
	dh_compress -X.vsz -X.csv

override_dh_clean: delete_generated
	dh_clean

delete_generated:
# delete files generated by rules
	rm -f debian/veusz.sharedmimeinfo debian/veusz.desktop
