#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export PYBUILD_NAME=fltk
%:
	dh $@ --with python3 --buildsystem=pybuild

V  := $(shell py3versions -r | sed 's/python//g')
V0 := $(word 1,$V)

override_dh_auto_install:
	dh_auto_install

	# The swig-generated "fltk.py" was different from different architectures, which
	# breaks Multi-Arch:same coinstallation. The "fltk.py" filename is now annotated
	# with the architecture, and we pull in the correct one at runtime. So the
	# different fltk.py end up in different files
	set -e;										 \
        for v in $V; do									 \
	  D=debian/python3-fltk/usr/lib/python$$v/dist-packages/fltk;			 \
	  mv $$D/fltk.py $$D/_fltk_${DEB_HOST_MULTIARCH}.py &&				 \
	  cat $$D/__init__.py | grep -vF 'from .fltk import *' >  $$D/__init__.new.py && \
	  cat debian/import-fltk-arch-specific.py              >> $$D/__init__.new.py && \
	  mv $$D/__init__.new.py $$D/__init__.py;					 \
	done

ifneq (,$(filter python3-fltk-doc,$(shell dh_listpackages)))
	# I'm building the docs, so presumably we're not cross-building, and I
	# can run pydoc3 on the just-built binaries. This is declared in the
	# Build-Profiles field in debian/control
	#
	# Pick the first requested python version: I could have more than one if
	# multiple Python versions are currently installed. I don't care which I
	# use, but I must pick one
	#
	# I also remove links to absolute build-time paths
	Din=python3-fltk/usr/lib/python$$V0/dist-packages/fltk;				\
	Dout=python3-fltk-doc/usr/share/doc/python3-fltk;				\
	mkdir -p debian/$$Dout;								\
	( cd debian/$$Din &&								\
	    pydoc$$V0 -w ./ &&								\
	    mv fltk.html _fltk.html							\
	       ../../../../../../$$Dout; );						\
	perl -p -i -e 's{(<a href="file:).*?(/usr/.*?">).*?(/usr/)}{$$1$$2$$3}'		\
	  debian/$$Dout/fltk.html							\
	  debian/$$Dout/_fltk.html
endif





# There aren't any tests defined in this package, and the autodetection in
# pybuild fails with errors
override_dh_auto_test:
	true
