#!/usr/bin/make -f

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS += -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)

CFLAGS += -Wno-error
CXXFLAGS += -Wno-error

PY_DEFAULT := $(shell pyversions --default)
PY_VERSIONS = $(sort $(shell pyversions --requested) $(PY_DEFAULT))

include /usr/share/python/python.mk

PATCH_DIR := debian/patches

configure_flags := --prefix=/usr

# packages where /usr/share/doc/$p is a symlink
USD_SYMLINK_PKGS := python-gst0.10-dbg

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	configure_flags += --build=$(DEB_BUILD_GNU_TYPE)
else
	configure_flags += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

patch: patch-stamp

patch-stamp:
	dh_testdir
	# backup the original files to restore them in the clean target
	-test -r config.sub && cp config.sub config.sub.orig
	-test -r config.guess && cp config.guess config.guess.orig
	-test -r /usr/share/misc/config.sub && \
		cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
		cp -f /usr/share/misc/config.guess config.guess
	touch $@

build-stamp-%: patch-stamp
	dh_testdir
	[ ! -f Makefile ] || $(MAKE) distclean
	PYTHON=`which $*` PKG_CONFIG_PATH=/usr/lib/pkgconfig/$* CFLAGS="$(CFLAGS)" \
		./configure $(configure_flags)
	$(MAKE)
	$(MAKE) install DESTDIR=$(CURDIR)/debian/python-gst0.10 \
		PYEXECDIR=$(call py_libdir,$(subst python,,$*)) \
		PYLIBDIR=$(call py_libdir,$(subst python,,$*)) \
		pyexecdir=$(call py_libdir,$(subst python,,$*)) \
		pylibdir=$(call py_libdir,$(subst python,,$*))
	# move installed files to a per python runtime location to allow
	# comparison of the resulting files across runtimes or shipping
	# multiple versions
	# pkg-config file (this is shipped twice because of varying pyexecdir)
	mkdir -p debian/python-gst0.10-dev/usr/lib/pkgconfig/$*
	mv debian/python-gst0.10/usr/lib/pkgconfig/*.pc debian/python-gst0.10-dev/usr/lib/pkgconfig/$*
	# defs files, should be the same whatever the build-time python runtime
	mkdir -p debian/python-gst0.10-dev/usr/share/gst-python/0.10/defs/$*
	mv debian/python-gst0.10/usr/share/gst-python/0.10/defs/*.defs debian/python-gst0.10-dev/usr/share/gst-python/0.10/defs/$*
	# examples should be in the -dev package
	mkdir -p debian/python-gst0.10-dev/usr/share/gst-python/0.10/examples
	mv debian/python-gst0.10/usr/share/gst-python/0.10/examples/* debian/python-gst0.10-dev/usr/share/gst-python/0.10/examples
	rmdir debian/python-gst0.10/usr/share/gst-python/0.10/examples
	# headers should be in the -dev package too
	mkdir -p debian/python-gst0.10-dev/usr/include/gstreamer-0.10/gst
	mv debian/python-gst0.10/usr/include/gstreamer-0.10/gst/* debian/python-gst0.10-dev/usr/include/gstreamer-0.10/gst
	touch $@

dbg-build-stamp-%: patch-stamp
	dh_testdir
	[ ! -f Makefile ] || $(MAKE) distclean
	PYTHON=`which $*-dbg` PKG_CONFIG_PATH=/usr/lib/pkgconfig/$* CFLAGS="$(CFLAGS) -O0" \
		./configure $(configure_flags)
	$(MAKE)
	$(MAKE) install DESTDIR=$(CURDIR)/debian/python-gst0.10-dbg \
		PYEXECDIR=$(call py_libdir,$(subst python,,$*)) \
		PYLIBDIR=$(call py_libdir,$(subst python,,$*)) \
		pyexecdir=$(call py_libdir,$(subst python,,$*)) \
		pylibdir=$(call py_libdir,$(subst python,,$*))
	touch $@

build-arch: $(addprefix build-stamp-, $(PY_VERSIONS)) $(addprefix dbg-build-stamp-, $(PY_VERSIONS))

build: build-arch

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	# restore files from backup (before unpatching)
	-test -r config.sub.orig && mv -f config.sub.orig config.sub
	-test -r config.guess.orig && mv -f config.guess.orig config.guess
	rm -f patch-stamp build-stamp-* dbg-build-stamp-* */*.pyc *.pyc
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_install
	# check that all builds with various Python runtimes produced the same
	# *.defs files (or fail)
	for v in $(PY_VERSIONS); do \
	    echo "Comparing defs files for the default Python version ($(PY_DEFAULT)) with defs files for Python $$v"; \
	    diff --brief --recursive debian/python-gst0.10-dev/usr/share/gst-python/0.10/defs/$(PY_DEFAULT) debian/python-gst0.10-dev/usr/share/gst-python/0.10/defs/$$v; \
	done
	
	# drop all *.la files
	find debian/python-gst0.10/usr/lib/ -name '*.la' -print0 | xargs -0r rm -f --
	# only keep *.so files and symlinks in the dbg packages
	find debian/python-*-dbg ! -type d ! -name '*.so' -print0 | xargs -0r rm -f --
	find debian/python-*-dbg -depth -empty -exec rmdir -- {} \;
	# suffix *.so files and symlinks with _d in the dbg packages
	for i in $$(find debian/python-*-dbg -name '*.so'); do \
	    b="$$(basename "$$i" .so)"; \
	    mv "$$i" "$$(dirname "$$i")/$${b}_d.so"; \
	done
	# since all Python runtimes produced the same files, only keep one set
	@echo "Cleaning up..."
	mv debian/python-gst0.10-dev/usr/share/gst-python/0.10/defs/$(PY_DEFAULT)/*.defs debian/python-gst0.10-dev/usr/share/gst-python/0.10/defs
	for v in $(PY_VERSIONS); do \
	    rm --force --recursive debian/python-gst0.10-dev/usr/share/gst-python/0.10/defs/$$v; \
	done
	# set executable bit on files with a shebang
	egrep -rlZ '^#!(.*)(python|sh)' debian/python-gst0.10 | xargs -0 chmod a+x --

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -s $(addprefix -N, $(USD_SYMLINK_PKGS)) ChangeLog
	dh_installdocs -s $(addprefix -N, $(USD_SYMLINK_PKGS))
	dh_link -s
	dh_link $(addprefix -p, $(USD_SYMLINK_PKGS)) \
		$(addprefix usr/share/doc/python-gst0.10 \
			usr/share/doc/,$(USD_SYMLINK_PKGS))
	dh_strip -s -Npython-gst0.10-dbg --dbg-package=python-gst0.10-dbg
	dh_compress -s -Xexample
	dh_fixperms -s
	dh_python2 -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary-indep:

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install patch
