#!/usr/bin/make -f
#export DH_VERBOSE=1
#export DEB_BUILD_OPTIONS=nocheck

export HOME = $(CURDIR)/build/

export PYTHONWARNINGS=d
export PYTHONHASHSEED=random

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

DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq ($(DEB_BUILD_ARCH),arm64)
export CFLAGS_noopt = $(patsubst -O2,-O0,$(CFLAGS))
else
export CFLAGS_noopt = $(patsubst -O2,-Og,$(CFLAGS))
endif

nproc = $(or $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))),1)
python2 = $(shell pyversions -r)
python2d = $(addsuffix -dbg,$(python2))
python_all = $(python2) $(python2d)
loop = echo -n $(1) | xargs -d ' ' -t -I {} env {} 

pythonpath = $$(_py_=$(strip $(1)); _py_=$${_py_\#python}; ls -d $(CURDIR)/build/lib.*-$${_py_})
default_pythonpath = $(call pythonpath,$(shell pyversions -d))

version = $(shell dpkg-parsechangelog | grep ^Version | cut -d ' ' -f 2)

vigra_version = $(shell dpkg-query -Wf '$${Version}' libvigraimpex-dev | sed -e 's/[+]b[0-9]\+$$//')

# build and build-* targets
# =========================

.PHONY: build
build: build-arch build-indep

.PHONY: build-arch
build-arch: build/build-stamp

build/build-stamp: debian/rules
	$(MAKE) -f debian/rules -j$(nproc) $(addprefix build/build-stamp-,$(python_all))
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	$(call loop, $(python_all)) debian/run-tests.py
endif
	touch $(@)

build/cruft-stamp:
	rm -Rf include/zlib*/ src/zlib*/ src/libpng*/ src/libtiff*/
	rm -Rf include/vigra/
	rm -Rf src/ga/
	cd gamera/backport && rm -f ConfigParser.py optparse.py sets.py textwrap.py
	mkdir -p $(dir $@)
	touch $(@)

build/build-stamp-python%: build/cruft-stamp
	python$(*) setup.py build --nowx
	touch $(@)

build/build-stamp-python%-dbg: build/cruft-stamp
	CFLAGS="$$CFLAGS_noopt" python$(*)-dbg setup.py build --nowx
	touch $(@)

.PHONY: build-indep
# dpkg-buildpackage (<< 1.16.2) called the build target without Build-Depends
# installed. To work around this buggy behaviour, we build arch-independent
# stuff in binary-arch rather than here. See bug #374029.

# binary and binary-* targets
# ===========================

.PHONY: binary
binary: binary-arch binary-indep

build/install-stamp: build/build-stamp
	$(call loop, $(python_all)) setup.py install --prefix=/usr --root=debian/tmp/ --nowx
	sed -i -e '1 s,^#!.*,#!/usr/bin/python,' debian/tmp/usr/bin/*
	rm -rf debian/tmp/usr/lib/python*/*-packages/gamera/src/
	rm -f debian/tmp/usr/bin/gamera_post_install.py
	touch $(@)

.PHONY: binary-arch
binary-arch: build/install-stamp
	dh_testroot
	dh_install -a -X.pyc --fail-missing
	dh_installdocs -a
	dh_installchangelogs -a
	dh_python2 -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_strip -a --dbg-package=python-gamera-dbg
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a -- -Vvigra:Version=$(vigra_version)
	dh_md5sums -a
	dh_builddeb -a -- -Zxz

.PHONY: binary-indep
binary-indep: build/install-stamp build/doc-stamp doc/manpage/gamera_gui.1
	dh_testroot
	dh_installdirs -i
	pngtopnm < gamera/pixmaps/icon.png | ppmtoxpm > debian/gamera-gui/usr/share/pixmaps/gamera-gui.xpm
	dh_install -i -X.pyc --fail-missing
	find debian/gamera-gui/ -name has_gui.py -delete
	find debian/gamera-gui/ -print0 | grep -FzZ "gui/__init__.py" | xargs -0 -r rm
	dh_installdocs -i
	cd debian/gamera-doc/usr/share/doc/gamera-doc/html/ && \
		$(CURDIR)/debian/symlink-helper ../src/ . && \
		rdfind -makehardlinks true -makeresultsfile false .
	dh_installchangelogs -i
	dh_installmenu -i
	dh_installman -i
	dh_python2 -i
	set -e; cd debian/python-gamera-dev/usr/include/; \
		mv $(shell pyversions -d)/gamera .; \
		rm -Rf python2.*/; \
		for version in $(shell pyversions -r); do \
		for suffix in "" _d; do \
			mkdir -p $$version$$suffix; \
			ln -sf ../gamera $$version$$suffix/gamera; \
		done; \
		done
	dh_link -i
	dh_compress -i -X.txt
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i -- -Zxz

doc/manpage/gamera_gui.1: build/build-stamp
	mkdir -p doc/manpage/
	ln -sf $(CURDIR)/debian/manpage-helper-1 doc/manpage/
	ln -sf $(CURDIR)/debian/manpage-helper-2 doc/manpage/gamera_gui
	cd doc/manpage/ && \
		PYTHONPATH=$(default_pythonpath) \
		help2man --version-string=$(version) --name='GUI for the Gamera framework' --no-info ./gamera_gui > gamera_gui.1

build/doc-stamp: build/build-stamp
	cd doc && PYTHONPATH=$(default_pythonpath) python gendoc.py
	touch $(@)

# clean target
# ============

.PHONY: clean
clean: debian/control
	dh_clean
	find -name '*.py[co]' -delete
	rm -Rf doc/html/images/
	rm -Rf doc/manpage/
	rm -Rf build/

# vim:ts=4 sw=4 noet
