#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

#rel_ext	= $(strip $(shell dpkg-parsechangelog | awk -F- '/^Version:/ {print $$NF}'))
#pkgver	= 2.4.3-$(shell expr $(rel_ext) + 0)
#pkgver	= 2.4.3-3ubuntu1

PYVERS	= $(shell pyversions -vs)

include /usr/share/dpkg/architecture.mk

ifneq ($(DEB_BUILD_MULTIARCH),$(DEB_HOST_MULTIARCH))
  SET_CROSS_ENV = PYTHONPATH=/usr/lib/python$*/plat-$(DEB_HOST_MULTIARCH)

  # FIXME: is this sufficient?
  SET_CROSS_ENV += _PYTHON_HOST_PLATFORM=$(DEB_HOST_GNU_CPU)
endif

p_tk	= python-tk
p_gdbm	= python-gdbm
d_tk	= debian/$(p_tk)
d_gdbm	= debian/$(p_gdbm)

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

build-python%:
	dh_testdir
	ln -sf /usr/lib/libBLT.2.4.so.8.5 $*/libBLT85.so
	ln -sf /usr/lib/libBLT.2.4.so.8.5 $*/libBLT85.so.8.5
	cd $* && $(SET_CROSS_ENV) python$* setup.py build
	touch build$*-stamp

dbg-build-stamp%:
	dh_testdir
	ln -sf /usr/lib/libBLT.2.4.so.8.5 $*/libBLT85.so
	ln -sf /usr/lib/libBLT.2.4.so.8.5 $*/libBLT85.so.8.5
	cd $* && $(SET_CROSS_ENV) python$*-dbg setup.py build
	touch dbg-build$*-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f *stamp
	rm -rf 2.?/build
	rm -rf 2.?/libBLT*
	dh_clean 

install: build install-prereq $(PYVERS:%=install-python%) $(PYVERS:%=dbg-install-python%)

install-prereq:
	dh_testdir
	dh_testroot
	dh_clean -k 

install-python%: build%-stamp
	cd $* && $(SET_CROSS_ENV) python$* setup.py install \
		--root=$(CURDIR)/$(d_tk) --install-layout=deb
	mkdir -p $(d_tk)/usr/lib/python$*/lib-dynload
	set -ex; \
	for i in $(d_tk)/usr/lib/python$*/*-packages/*.so; do \
	  b=$$(basename $$i .so); \
	  d=$${b}.$(DEB_HOST_MULTIARCH).so; \
	  mv $$i $(d_tk)/usr/lib/python$*/lib-dynload/$$d; \
	done
	mkdir -p $(d_gdbm)/usr/lib/python$*/lib-dynload
	mv $(d_tk)/usr/lib/python$*/lib-dynload/gdbm*.so \
		$(d_gdbm)/usr/lib/python$*/lib-dynload/
	rm -rf $(d_tk)/usr/lib/python$*/*-packages

dbg-install-python%:
	cd $* && $(SET_CROSS_ENV) python$*-dbg setup.py install \
		--root=$(CURDIR)/$(d_tk)-dbg --install-layout=deb
	mkdir -p $(d_tk)-dbg/usr/lib/python$*/lib-dynload
	set -ex; \
	for i in $(d_tk)-dbg/usr/lib/python$*/*-packages/*_d.so; do \
	  b=$$(basename $$i _d.so); \
	  d=$${b}.$(DEB_HOST_MULTIARCH)_d.so; \
	  mv $$i $(d_tk)-dbg/usr/lib/python$*/lib-dynload/$$d; \
	done
	mkdir -p $(d_gdbm)-dbg/usr/lib/python$*/lib-dynload
	mv $(d_tk)-dbg/usr/lib/python$*/lib-dynload/gdbm*_d.so \
		$(d_gdbm)-dbg/usr/lib/python$*/lib-dynload/
	rm -rf $(d_tk)-dbg/usr/lib/python$*/*-packages

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	for p in $(p_gdbm) $(p_gdbm)-dbg $(p_tk) $(p_tk)-dbg; do \
	  mkdir -p debian/$$p/usr/share/lintian/overrides; \
	  cp debian/$$p.overrides debian/$$p/usr/share/lintian/overrides/$$p; \
	done
	dh_installdocs -p$(p_gdbm) debian/README.Debian
	dh_installdocs -p$(p_tk) debian/README.Tk
	rm -rf $(d_gdbm)-dbg/usr/share/doc/$(p_gdbm)-dbg
	ln -s $(p_gdbm) $(d_gdbm)-dbg/usr/share/doc/$(p_gdbm)-dbg
	rm -rf $(d_tk)-dbg/usr/share/doc/$(p_tk)-dbg
	ln -s $(p_tk) $(d_tk)-dbg/usr/share/doc/$(p_tk)-dbg
	dh_strip -p$(p_gdbm) --dbg-package=$(p_gdbm)-dbg
	dh_strip -p$(p_tk) --dbg-package=$(p_tk)-dbg
	( \
	  echo 'python:Depends=python (>= 2.7), python (<< 2.8)'; \
	  echo 'python:Provides=python2.7-gdbm'; \
	  echo 'python:Versions=2.7'; \
	) >> debian/$(p_gdbm).substvars
	( \
	  echo 'python:Depends=python-dbg (>= 2.7), python-dbg (<< 2.8)'; \
	  echo 'python:Provides=python2.7-gdbm-dbg'; \
	  echo 'python:Versions=2.7'; \
	) >> debian/$(p_gdbm)-dbg.substvars
	( \
	  echo 'python:Depends=python (>= 2.7), python (<< 2.8)'; \
	  echo 'python:Provides=python2.7-tk'; \
	  echo 'python:Versions=2.7'; \
	) >> debian/$(p_tk).substvars
	( \
	  echo 'python:Depends=python-dbg (>= 2.7), python-dbg (<< 2.8)'; \
	  echo 'python:Provides=python2.7-tk-dbg'; \
	  echo 'python:Versions=2.7'; \
	) >> debian/$(p_tk)-dbg.substvars
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
#	dh_gencontrol -u-v$(pkgver)
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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