#!/usr/bin/make -f

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

ifneq ($(filter parallel=%,$(DEB_BUILD_OPTIONS)),)
    makeflags = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

pythonpath = $$(_py_=$(strip $(1)); _py_=$${_py_\#python}; ls -d $(CURDIR)/build/lib.*-$${_py_})
pythonpath_dbg = $$(_py_=$(strip $(1)); _py_=$${_py_\#python}; ls -d $(CURDIR)/build/lib_d.*-$${_py_} 2>/dev/null || ls -d $(CURDIR)/build/lib.*-$${_py_}-pydebug)

default_pythonpath = $(call pythonpath,$(shell pyversions -d))

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


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

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

build-arch:
	dh build

build-indep:
	# Nothing to do in build-indep, "thanks" to bug #374029.

.PHONY: override_dh_auto_build
override_dh_auto_build:
	rm -Rf include/zlib*/ src/zlib*/ src/libpng*/ src/libtiff*/
	rm -Rf include/vigra/
	rm -Rf src/ga/
	$(MAKE) -f debian/rules $(makeflags) build/stamp
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	set -x -e; \
	for python in $(shell pyversions -r); do \
		PYTHONPATH=$(call pythonpath,$$python) $$python debian/run-tests.py; \
		PYTHONPATH=$(call pythonpath_dbg,$$python) $$python-dbg debian/run-tests.py; \
	done
endif

build/stamp: $(patsubst %,build/stamp-python%,$(shell pyversions -rv))
build/stamp: $(patsubst %,build/stamp-python%-dbg,$(shell pyversions -rv))
build/stamp:
	touch $(@)

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


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

.PHONY: binary binary-arch binary-indep
binary binary-arch binary-indep:
	dh $(@)

.PHONY: override_dh_installman
ifneq ($(shell dh_listpackages | grep -x -F gamera-gui),)
override_dh_installman: doc/manpage/gamera_gui.1
	dh_installman
endif

.PHONY: override_dh_installdocs
override_dh_installdocs: doc/stamp
	dh_installdocs
ifneq ($(shell dh_listpackages | grep -x -F gamera-doc),)
	cd debian/gamera-doc/usr/share/doc/gamera-doc/html/ && \
		$(CURDIR)/debian/symlink-helper ../src/ . && \
		fdupes -r --linkhard .
endif

doc/manpage/gamera_gui.1: 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
	chmod +x 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

doc/stamp: build/stamp
ifneq ($(shell dh_listpackages | grep -x -F gamera-doc),)
	cd doc && PYTHONPATH=$(default_pythonpath) python gendoc.py
endif
	touch $(@)

.PHONY: override_dh_install
override_dh_install:
	dh_install
ifneq ($(shell dh_listpackages | grep -x -F gamera-gui),)
	find debian/gamera-gui/ -name has_gui.py -delete
	pngtopnm < gamera/pixmaps/icon.png | ppmtoxpm > debian/gamera-gui/usr/share/pixmaps/gamera-gui.xpm
endif

.PHONY: override_dh_link
override_dh_link:
ifneq ($(shell dh_listpackages | grep -x -F python-gamera-dev),)
	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
endif
	dh_link

.PHONY: override_dh_strip
override_dh_strip:
ifeq ($(filter nostrip,$(DEB_BUILD_OPTIONS)),)
	dh_strip --dbg-package=python-gamera-dbg
	cd debian/*-dbg/usr/lib/debug/usr/lib && mv pyshared pymodules
endif

.PHONY: override_dh_builddeb
override_dh_builddeb:
	dh_builddeb -- -Zxz

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

.PHONY: clean
clean:
	dh clean

.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	dh_auto_clean
	rm -Rf doc/html/images/
	rm -Rf doc/manpage/
	rm -Rf build/

# vim:ts=4 sw=4 noet
