#!/usr/bin/make -f
# -*- makefile -*-
include /usr/share/dpkg/pkg-info.mk

#  Apparently WGET agent is not favored by upstream's MoinMoin
WGET_AGENT_STRING="Lynx/2.8.8dev.4 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.8.6"
WGET=wget -q --user-agent=$(WGET_AGENT_STRING)
WIKI_URL=http://wiki.cython.org

# Python versions
PYDEF=$(shell pyversions -dv)
PY2VERS=$(shell pyversions -vr)
PY3VERS=$(shell py3versions -vr)

# In case matplotlib or anything else inquires
export HOME=$(CURDIR)/build
export MPLCONFIGDIR=$(CURDIR)/build

%:
	dh $@ --with python2,python3 --buildsystem python_distutils

override_dh_auto_build:
	dh_auto_build

	: # Build for Python 3
	set -e; for v in $(PY3VERS); do \
	 python$$v setup.py build; \
	 python$$v-dbg setup.py build; \
	done

override_dh_auto_install:
	dh_auto_install

	: # Install for Python 3
	set -e; for v in $(PY3VERS); do \
	 python$$v setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb; \
	 python$$v-dbg setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb; \
	done

	: # Force the creation of cython3 and cygdb3 executables.
	: # Shebang lines are fixed in override_dh_install.
	cp debian/tmp/usr/bin/cython debian/tmp/usr/bin/cython3
	cp debian/tmp/usr/bin/cygdb debian/tmp/usr/bin/cygdb3

override_dh_installman:
	: # Generate a manpage using help2man
	PYTHONPATH=`/bin/ls -d $(CURDIR)/build/lib.*-$(PYDEF)` \
	  help2man --no-info --no-discard-stderr \
		-n "compile Cython code (.pyx) into C to build a Python extension" \
		bin/cython >| build/cython.1
	: # Verify that manpage generated nicely -- no Errors reported
	grep -q 'ImportError:' build/cython.1 && exit 1 || :
	dh_installman build/cython.1

override_dh_install:
	dh_install

	: # fix shebang
	sed -i -e '1 s,#!.*python.*,#!/usr/bin/python,' debian/cython/usr/bin/*
	sed -i -e '1 s,#!.*python.*,#!/usr/bin/python3,' debian/cython3/usr/bin/*

	: # remove files installed from cython.install that belong in -dbg
	find $(CURDIR)/debian/cython/ -name "*_d.so" -delete
	find $(CURDIR)/debian/cython3/ -name "*.cpython-3?d*.so" -delete

	: # share -dbg and normal package doc dirs
	rm -rf debian/cython-dbg/usr/share/doc/cython-dbg
	dh_link -pcython-dbg usr/share/doc/cython usr/share/doc/cython-dbg
	rm -rf debian/cython3-dbg/usr/share/doc/cython3-dbg
	dh_link -pcython3-dbg usr/share/doc/cython3 usr/share/doc/cython3-dbg
	# add debian version to watermark of generated files
	set -e; for v in $(PY2VERS); do \
	  sed -i -e "s/ on / (Debian $(DEB_VERSION)) on /" \
	  debian/cython/usr/lib/python$$v/dist-packages/Cython/Compiler/Version.py; \
	done
	sed -i -e "s/ on / (Debian $(DEB_VERSION)) on /" \
	  debian/cython3/usr/lib/python3/dist-packages/Cython/Compiler/Version.py


override_dh_installdocs:
	: # Build Documentation
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
ifneq (,$(findstring -a,$(DH_INTERNAL_OPTIONS)))
	: # not building documentation in -a
	: # just faking having documentation so that dh-installdocs doesn't fail
	mkdir -p docs/build/html
else
	make -C docs html
	: # Use jquery/underscore from Debian package, so prune shipped one
	-rm docs/build/html/_static/jquery.js docs/build/html/_static/underscore.js
endif
endif
	dh_installdocs --package=cython --package=cython-dbg --link-doc=cython debian/upstream
	dh_installdocs --package=cython3 --package=cython3-dbg --link-doc=cython3 debian/upstream
	dh_installdocs --package=cython-doc debian/upstream


override_dh_installchangelogs:
	dh_installchangelogs --all CHANGES.rst


override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; for P in $(PY2VERS) $(PY3VERS); do \
	 PYTHONPATH=`/bin/ls -d $(CURDIR)/build/lib.*-$$P` \
	  /usr/bin/python$$P runtests.py --no-refnanny -v -v --exclude="(parallel|Debugger)" --work-dir=build/work-dir 2>&1; \
	done
endif

override_dh_python2:
	dh_python2 -pcython

override_dh_python3:
	dh_python3 -pcython3


override_dh_strip:
	dh_strip -pcython --dbg-package=cython-dbg
	dh_strip -pcython3 --dbg-package=cython3-dbg

override_dh_auto_clean:
	rm -rf build
	find -name "*.py[oc]" -type f -delete

