#!/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)
DEBIAN_ARCH:=$(shell dpkg-architecture -qDEB_HOST_ARCH)

# 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_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
# tests fail on arm due to different float results see debian bug 654604
ifeq (,$(filter $(DEBIAN_ARCH), armel armhf))
	set -e -x;\
	for py in $(PYVERS); do \
		PYTHONPATH=$(call pythonpath,$$py) xvfb-run -a \
			--server-args "-screen 0 640x480x24" \
			python$$py tests/runselftest.py ;\
		PYTHONPATH=$(call pythonpath_dbg,$$py) xvfb-run -a \
			--server-args "-screen 0 640x480x24" \
			python$$py-dbg tests/runselftest.py ;\
	done
endif
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
