#!/usr/bin/make -f

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

export LC_ALL=C

PKGNAME:=python-pyicu
PKG3NAME=python3-icu
PYVERS:=$(shell pyversions -vr)
PY3VERS:=$(shell py3versions -vr)

-include /usr/share/python/python.mk


build: build-arch
build-indep:
# We have nothing to do by default.
build-arch: build-stamp
build-stamp:
	dh_testdir
	
	# setup.py modifies test source files when building for Python 3, so
	# build out of tree.
	rm -rf build/py2 build/py3
	mkdir -p build/py2 build/py3
	cp -a $(filter-out build/py2 build/py3 debian,$(wildcard *)) build/py2/
	cp -a $(filter-out build/py2 build/py3 debian,$(wildcard *)) build/py3/
	
	set -e; \
	for py in $(PYVERS) $(PY3VERS); do  \
	    (cd build/py$${py%%.*}; \
	    python$$py setup.py build; \
	    python$$py-dbg setup.py build); \
	done

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -ex ;\
	for test in `ls test/*.py`; do \
	  for py in $(PYVERS) $(PY3VERS); do  \
	    (cd build/py$${py%%.*}; \
	    PYTHONPATH=`ls -d $$(pwd)/build/lib.*-$$py` \
	      python$$py $$test ;\
	    PYTHONPATH=`ls -d $$(pwd)/build/lib_d.*-$$py || ls -d $$(pwd)/build/lib.*-$$py-pydebug` \
	      python$$py-dbg $$test) ;\
	  done ;\
	done
endif

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f install-stamp build-stamp
	find . -name "*.pyc" -print0 | xargs -0 rm -f
	find . -name __pycache__ -print0 | xargs -0 rm -rf
	rm -rf build
	dh_clean

install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	set -e; \
	for py in $(PYVERS); do  \
	    (cd build/py2; \
	    python$$py setup.py install --root=$(CURDIR)/debian/$(PKGNAME) --prefix=/usr $(py_setup_install_args) ;\
	    python$$py-dbg setup.py install --root=$(CURDIR)/debian/$(PKGNAME)-dbg --prefix=/usr $(py_setup_install_args)) ;\
	done
	set -e; \
	for py in $(PY3VERS); do  \
	    (cd build/py3; \
	    python$$py setup.py install --root=$(CURDIR)/debian/$(PKG3NAME) --prefix=/usr $(py_setup_install_args) ;\
	    python$$py-dbg setup.py install --root=$(CURDIR)/debian/$(PKG3NAME)-dbg --prefix=/usr $(py_setup_install_args)) ;\
	done
	
	dh_installdocs
	dh_installexamples
	touch install-stamp

binary-indep: build-indep install
# We have nothing to do by default.

binary-arch: build-arch install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES
	dh_compress -X.py
	dh_strip -p$(PKGNAME) --dbg-package=$(PKGNAME)-dbg
	dh_strip -p$(PKG3NAME) --dbg-package=$(PKG3NAME)-dbg
	dh_fixperms
	dh_python2
	dh_python3
	rm -rf debian/$(PKGNAME)-dbg/usr/share/doc/$(PKGNAME)-dbg
	ln -s $(PKGNAME) debian/$(PKGNAME)-dbg/usr/share/doc/$(PKGNAME)-dbg
	rm -rf debian/$(PKG3NAME)-dbg/usr/share/doc/$(PKG3NAME)-dbg
	ln -s $(PKG3NAME) debian/$(PKG3NAME)-dbg/usr/share/doc/$(PKG3NAME)-dbg
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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