#! /usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE = 1

PYVERS = $(shell pyversions -r debian/control)

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

export BASECFLAGS := $(CFLAGS) $(CPPFLAGS)

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp: $(PYVERS:%=build-ext-%-stamp)
	touch $@

build-ext-python%-stamp:
	dh_testdir

	python$* setup.py build
# Add _normal suffix to build dir:
	d=$$(echo build/temp.*-$*_ucs4); mv $$d $${d}_normal
	rm -rf build/lib.* build/build-*

	python$*-dbg setup.py build -g
# Add _debug suffix to build dir:
	d=$$(echo build/temp.*-$*_ucs4); mv $$d $${d}_debug
	rm -rf build/lib.* build/build-*

	sh -e debian/installtemplates.sh $*
	touch $@

clean: clean1

clean1:
	dh_testdir
	dh_testroot
	rm -f build*stamp
	rm -f debian/*.doc-base
# Can't run setup.py clean because it removes *.pyd, which makes
# "unrepresentable changes to source files".
	find -name "*~" -o -name "*.pyc" | xargs -r rm -f
	rm -rf build debian/python*-tmp
	dh_clean

install: build install-prereq $(PYVERS:%=install-ext-%)
# Replace all "#!" calls to python with /usr/bin/python and make the
# programs executable:
	find debian -mindepth 2 -type f | while read i; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/python\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done

install-prereq:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

install-ext-python%:
	dh_installdirs \
	    -ppython-egenix-mx-base-dev \
	    usr/include/python$*/mx

# Remove the _normal suffix:
	d=$$(echo build/temp.*-$**_normal); mv $$d $${d%*_normal}
	python$* setup.py install --root=debian/python$*-tmp --install-layout=deb
# ...and restore it.
	d=$$(echo build/temp.*-$*_ucs4); mv $$d $${d}_normal

# Remove debug versions of extension modules.
	find debian/python$*-tmp -name '*_d.so' | xargs rm -f

# Collect files.
	dh_install -ppython-egenix-mxbeebase \
                   -ppython-egenix-mxbeebase-doc \
                   -ppython-egenix-mxdatetime \
                   -ppython-egenix-mxdatetime-doc \
                   -ppython-egenix-mxproxy \
                   -ppython-egenix-mxproxy-doc \
                   -ppython-egenix-mxqueue \
                   -ppython-egenix-mxqueue-doc \
                   -ppython-egenix-mxstack \
                   -ppython-egenix-mxstack-doc \
                   -ppython-egenix-mxtexttools \
                   -ppython-egenix-mxtexttools-doc \
                   -ppython-egenix-mxtools \
                   -ppython-egenix-mxtools-doc \
                   -ppython-egenix-mxuid \
                   -ppython-egenix-mxuid-doc \
                   -ppython-egenix-mxurl \
                   -ppython-egenix-mxurl-doc \
                   --sourcedir=debian/python$*-tmp

# Move C header files and other stuff to the right location.
	sh -e debian/movefiles.sh $*

# Install the debug build.

# Remove the _debug suffix.
	d=$$(echo build/temp.*-$**_debug); mv $$d $${d%*_debug}
	python$*-dbg setup.py install --root=debian/python-egenix-mx-base-dbg --install-layout=deb
# ...and restore it.
	d=$$(echo build/temp.*-$*_ucs4); mv $$d $${d}_debug

	find debian/python-egenix-mx-base-dbg \
	    -type f ! -name '*_d.so' | xargs rm -f
	find debian/python-egenix-mx-base-dbg -depth -empty -exec rmdir {} \;
	mkdir -p debian/python-egenix-mx-base-dbg/usr/include/python$*_d
	ln -s ../python$*/mx \
	      debian/python-egenix-mx-base-dbg/usr/include/python$*_d/mx

# Architecture-independent files are built here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i

	dh_installdocs -i -A debian/README.Debian
	dh_installchangelogs -i
	dh_link -i
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_python2 -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Architecture-dependent files are built here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a -A debian/README.Debian
	dh_installexamples -a
	dh_installchangelogs -a
	dh_strip -a -Npython-egenix-mx-base-dbg --dbg-package=python-egenix-mx-base-dbg
	rm -rf debian/python-egenix-mx-base-dbg/usr/share/doc/python-egenix-mx-base-dbg
	ln -s python-egenix-mxtools \
	      debian/python-egenix-mx-base-dbg/usr/share/doc/python-egenix-mx-base-dbg
	dh_link -a
	dh_lintian -a
	dh_compress -a
	dh_fixperms -a
	dh_python2 -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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