#!/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)

# Get the supported Python versions
PYVERS = $(shell pyversions -r -v)

# Callable functions to determine the correct PYTHONPATH
pythonpath = $$(ls -d $(CURDIR)/build/lib.*-$(1))
pythonpath_dbg = $$(ls -d $(CURDIR)/build/lib.*-$(1)-pydebug 2>/dev/null || ls -d $(CURDIR)/lib.*$(1)-pydebug)

%:
	dh $@ --with python2

override_dh_auto_build: delete_generated
# icons for menus
	gm convert windows/icons/veusz_32.png debian/veusz.xpm
# regenerate docs
	(cd Documents; ./generate_manual.sh)
# -- --force works around bug #589759
	dh_auto_build -- --force

override_dh_auto_install:
# do not install examples and put data files in /usr/share/veusz
	dh_auto_install -- --veusz-resource-dir=debian/veusz/usr/share/veusz --disable-install-examples

# veusz follows symlink to find its resource directory
	for py in $(PYVERS); do \
		dh_link usr/share/veusz usr/lib/python$${py}/dist-packages/veusz/resources; \
	done

	dh_numpy
	dh_sip

override_dh_auto_test:
# Run self tests. The current directory is used as the resource
# directory as veusz hasn't been installed when this is run.
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -e -x;\
	for py in $(PYVERS); do \
		LC_ALL=C \
		PYTHONPATH=$(call pythonpath,$$py) \
		VEUSZ_RESOURCE_DIR=. \
			xvfb-run -a \
			--server-args "-screen 0 640x480x24" \
			python$$py tests/runselftest.py ;\
		LC_ALL=C \
		PYTHONPATH=$(call pythonpath_dbg,$$py) \
		VEUSZ_RESOURCE_DIR=. \
			xvfb-run -a \
			--server-args "-screen 0 640x480x24" \
			python$$py-dbg tests/runselftest.py ;\
	done
endif

override_dh_strip:
	dh_strip --dbg-package=veusz-helpers-dbg

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

override_dh_clean: delete_generated
	dh_clean

delete_generated:
# delete files generated by rules
	rm -f debian/veusz.xpm
	rm -f Documents/manual.html Documents/manual.pdf \
		Documents/manual.txt \
		Documents/veusz.1 Documents/veusz_listen.1
# delete any self test failures
	rm -f tests/*.temp.selftest
