#!/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}'))

PYVERS	= $(shell py3versions -vs)
PYVERS	= 3.4

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	= python3-tk
p_gdbm	= python3-gdbm
d_tk	= debian/$(p_tk)
d_gdbm	= debian/$(p_gdbm)

build-arch: build
build-indep: build
build: build-stamp
build-arch: build-stamp
build-indep: build-stamp
build-stamp: $(PYVERS:%=build-stamp-py%) $(PYVERS:%=dbg-stamp-py%)
	touch $@

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

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

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

install: build install-prereq $(PYVERS:%=install-stamp-py%) $(PYVERS:%=dbg-install-py%)
	rm -rf $(d_tk)/usr/share/pycentral
	rm -rf $(d_gdbm)/usr/share/pycentral

install-prereq:
	dh_testdir
	dh_testroot
	dh_clean -k 

install-stamp-py%: build-stamp-py%
	cd $* && $(SET_CROSS_ENV) python$* setup.py install \
		--root=$(CURDIR)/$(d_tk) --install-layout=deb
	mkdir -p $(d_tk)/usr/lib/python$*/lib-dynload
ifeq (0,1)
	set -ex; \
	for i in $(d_tk)/usr/lib/python3/*-packages/*.cpython-$(subst .,,$*)m.so; do \
	  b=$$(basename $$i .cpython-$(subst .,,$*)m.so); \
	  d=$${b}.cpython-$(subst .,,$*)m-$(DEB_HOST_MULTIARCH).so; \
	  mv $$i $(d_tk)/usr/lib/python$*/lib-dynload/$$d; \
	done
else
	mv $(d_tk)/usr/lib/python3/*-packages/*.so \
		$(d_tk)/usr/lib/python$*/lib-dynload/
endif
	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/python3

dbg-install-py%: dbg-stamp-py%
	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
ifeq (0,1)
	set -ex; \
	for i in $(d_tk)-dbg/usr/lib/python3/*-packages/*.cpython-$(subst .,,$*)dm.so; do \
	  b=$$(basename $$i .cpython-$(subst .,,$*)dm.so); \
	  d=$${b}.cpython-$(subst .,,$*)dm-$(DEB_HOST_MULTIARCH).so; \
	  mv $$i $(d_tk)-dbg/usr/lib/python$*/lib-dynload/$$d; \
	done
else
	mv $(d_tk)-dbg/usr/lib/python3/*-packages/*.so \
		$(d_tk)-dbg/usr/lib/python$*/lib-dynload/
endif
	mkdir -p $(d_gdbm)-dbg/usr/lib/python$*/lib-dynload
	mv $(d_tk)-dbg/usr/lib/python$*/lib-dynload/_gdbm*.so \
		$(d_gdbm)-dbg/usr/lib/python$*/lib-dynload/
	rm -rf $(d_tk)-dbg/usr/lib/python3

# 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
	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
ifeq (0,1)
	( \
	  echo 'python3:Depends=python3 (>= 3.4), python3 (<< 3.6)'; \
	  echo 'python3:Versions=3.4, 3.5'; \
	  echo 'python3:Provides=python3.4-gdbm, python3.5-gdbm'; \
	) >> debian/python3-gdbm.substvars
	( \
	  echo 'python3:Depends=python3 (>= 3.4), python3 (<< 3.6)'; \
	  echo 'python3:Versions=3.4, 3.5'; \
	  echo 'python3:Provides=python3.4-tk, python3.5-tk'; \
	) >> debian/python3-tk.substvars
else
	( \
	  echo 'python3:Depends=python3 (>= 3.4), python3 (<< 3.5)'; \
	  echo 'python3:Versions=3.4'; \
	  echo 'python3:Provides=python3.4-gdbm'; \
	) >> debian/python3-gdbm.substvars
	( \
	  echo 'python3:Depends=python3 (>= 3.4), python3 (<< 3.5)'; \
	  echo 'python3:Versions=3.4'; \
	  echo 'python3:Provides=python3.4-tk'; \
	) >> debian/python3-tk.substvars
endif
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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