#!/usr/bin/make -f

PY2VERS=$(shell pyversions -vr debian/control)
PY3VERS=$(shell py3versions -vr)
PYDEF=$(shell pyversions -dv)
PYLIBPATH := $(shell python -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print b.build_platlib")

# Look at #634012 to understand why is needed and what will happen if we set
# compat to 9
unexport LDFLAGS
export ATLAS=None

%:
	dh $@ --with sphinxdoc,python3

override_dh_auto_build:
	dh_auto_build
	for v in $(PY3VERS); do \
		python$$v setup.py build; \
		python$$v-dbg setup.py build; \
	done

override_dh_auto_clean:
	dh_auto_clean
	rm -rf `find -name build -type d`
	rm -rf `find . -name "*.pyc" -type f`
	rm -rf debian/tmp2
	rm -rf doc/fontList.cache numpy/f2py/docs/usersguide/index.html

override_dh_installman:
	dh_installman numpy/f2py/f2py.1
	# link manpage for versioned and dbg incarnations of f2py
	for v in $(PY2VERS); do \
		dh_link /usr/share/man/man1/f2py.1.gz /usr/share/man/man1/f2py$$v.1.gz; \
		dh_link -ppython-numpy-dbg /usr/share/man/man1/f2py.1.gz /usr/share/man/man1/f2py$$v-dbg.1.gz; \
	done
	for v in $(PY3VERS); do \
		dh_link -ppython3-numpy /usr/share/man/man1/f2py.1.gz /usr/share/man/man1/f2py$$v.1.gz; \
		dh_link -ppython3-numpy-dbg /usr/share/man/man1/f2py.1.gz /usr/share/man/man1/f2py$$v-dbg.1.gz; \
	done
	dh_link -ppython-numpy-dbg /usr/share/man/man1/f2py.1.gz /usr/share/man/man1/f2py-dbg.1.gz;

override_dh_install:
	# add shebang information to f2py script
	for v in $(PY2VERS) $(PY3VERS); do \
		sed -i "1s,#!.*python[^ ]*\(.*\),#!/usr/bin/python$$v," debian/tmp/usr/bin/f2py$$v; \
		cp -a debian/tmp/usr/bin/f2py$$v debian/tmp/usr/bin/f2py$$v-dbg ; \
		sed -i "1s,#!.*python[^ ]*\(.*\),#!/usr/bin/python$$v-dbg," debian/tmp/usr/bin/f2py$$v-dbg; \
	done
	# install unversioned f2py script
	cp -a debian/tmp/usr/bin/f2py$(PYDEF) debian/tmp/usr/bin/f2py
	sed -i "1s,#!.*python[^ ]*\(.*\),#!/usr/bin/python," debian/tmp/usr/bin/f2py
	cp -a debian/tmp/usr/bin/f2py$(PYDEF)-dbg debian/tmp/usr/bin/f2py-dbg
	sed -i "1s,#!.*python[^ ]*\(.*\),#!/usr/bin/python-dbg," debian/tmp/usr/bin/f2py-dbg

	# moving those scripts away until after pysupport has run, to not
	# generate depends on pythonx.y
	mkdir debian/tmp2
	mv debian/tmp/usr/bin/* debian/tmp2

	dh_install

	# remove files installed from .install (that's due to the dir listed there)
	find $(CURDIR)/debian/python-numpy/ -name "*_d.so" -delete

	# create symlinks for .h files
	for i in $(PY2VERS); do \
	[ -d $(CURDIR)/debian/python-numpy/usr/include/python$$i ] || \
		mkdir -p $(CURDIR)/debian/python-numpy/usr/include/python$$i; \
		dh_link usr/lib/pymodules/python$$i/numpy/core/include/numpy usr/include/python$$i/numpy; \
	done
	for i in $(PY2VERS); do \
	[ -d $(CURDIR)/debian/python-numpy/usr/include/python$${i}_d ] || \
		mkdir -p $(CURDIR)/debian/python-numpy/usr/include/python$${i}_d; \
		dh_link usr/lib/pymodules/python$$i/numpy/core/include/numpy usr/include/python$${i}_d/numpy; \
	done
	# Python 3
	for i in $(PY3VERS); do \
	[ -d $(CURDIR)/debian/python3-numpy/usr/include/python$${i}mu ] || \
		mkdir -p $(CURDIR)/debian/python3-numpy/usr/include/python$${i}mu; \
		dh_link -ppython3-numpy usr/lib/python3/dist-packages/numpy/core/include/numpy usr/include/python$${i}mu/numpy; \
	done
	for i in $(PY3VERS); do \
	[ -d $(CURDIR)/debian/python3-numpy/usr/include/python$${i}dmu ] || \
		mkdir -p $(CURDIR)/debian/python3-numpy/usr/include/python$${i}dmu; \
		dh_link -ppython3-numpy usr/lib/python3/dist-packages/numpy/core/include/numpy/ usr/include/python$${i}dmu/numpy; \
	done

	# share -dbg and normal package doc dirs
	rm -rf debian/python-numpy-dbg/usr/share/doc/python-numpy-dbg
	dh_link -ppython-numpy-dbg usr/share/doc/python-numpy usr/share/doc/python-numpy-dbg
	rm -rf debian/python3-numpy-dbg/usr/share/doc/python3-numpy-dbg
	dh_link -ppython3-numpy-dbg usr/share/doc/python3-numpy usr/share/doc/python3-numpy-dbg

override_dh_gencontrol:
	python debian/versions.helper >> debian/python-numpy.substvars
	python debian/versions3.helper >> debian/python3-numpy.substvars
	dh_gencontrol

override_dh_compress:
	dh_compress -X.js -Xobjects.inv -X.txt

override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -ppython-numpy --dbg-package=python-numpy-dbg
	dh_strip -ppython3-numpy --dbg-package=python3-numpy-dbg
	# dh_strip seemingly doesn't catch the PEP 3149-style debug names
	rm debian/python3-numpy/usr/lib/python3*/*-packages/*/*/*.cpython-*d*.so
	
	# fix python-debug-in-wrong-location lintian warning
	cd debian/python-numpy-dbg/usr/lib/debug/usr/lib && mv pyshared pymodules
endif


override_dh_pysupport:
	dh_pysupport

	# copy back f2py* bin, in their ultimate location
	mv debian/tmp2/f2py2.? debian/tmp2/f2py debian/python-numpy/usr/bin/
	mkdir -p debian/python-numpy-dbg/usr/bin/
	mv debian/tmp2/f2py2.?-dbg debian/tmp2/f2py-dbg debian/python-numpy-dbg/usr/bin/
	
	mkdir -p debian/python3-numpy/usr/bin/
	mv debian/tmp2/f2py3.? debian/python3-numpy/usr/bin/
	mkdir -p debian/python3-numpy-dbg/usr/bin/
	mv debian/tmp2/f2py3.?-dbg debian/python3-numpy-dbg/usr/bin/

	# GIGANTIC HACK - add back libnpymath.a
	# get the file names, generate the new location, create the dir if missing, copy
	# the file and then add it to the public list
	for file in ./debian/tmp/usr/lib/python2.*/*-packages/numpy/core/lib/libnpymath.a ; do \
		newfile=$$(echo $$file | sed 's|tmp|python-numpy|' | sed "s|lib/python|lib/pyshared/python|" | sed 's|[^/]*-packages/||') ;\
		mkdir -p $$(dirname $$newfile) ;\
		cp -a $$file $$newfile ;\
		installedfile=$$(echo $$newfile | sed 's|./debian/python-numpy||') ;\
		# add to public modules list ;\
		echo $$installedfile >> ./debian/python-numpy/usr/share/python-support/python-numpy.public ;\
	done

override_dh_sphinxdoc:
	dh_sphinxdoc -i

build: build-arch build-indep ;

build-arch:
	dh build


build-indep: 
	# build doc only for default python version
	(export MPLCONFIGDIR=. ; make -C doc html PYTHONPATH=../$(PYLIBPATH))
	rst2html numpy/f2py/docs/usersguide/index.txt > numpy/f2py/docs/usersguide/index.html

update_intersphinx_mapping:
	wget http://docs.python.org/dev/objects.inv -O debian/python.org_objects.inv

override_dh_auto_install:
	dh_auto_install
	
	# Install for Python 3
	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
	
	# yes, we need to run tests after install
	# The reason is that numpy tries hard to not let you run it from the
	# source directory, so we need to mess with the import order so to
	# select the code in the installation path
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	-for v in $(PY2VERS) ; do \
		echo "-- running tests for "$$v" plain --" ; \
		python$$v -c "import sys ; sys.path.insert(0, '$(CURDIR)/debian/tmp/usr/lib/python$$v/dist-packages/') ; import numpy; numpy.test()" ; \
		echo "-- running tests for "$$v" debug --" ; \
		python$$v-dbg -c "import sys ; sys.path.insert(0, '$(CURDIR)/debian/tmp/usr/lib/python$$v/dist-packages/') ; import numpy; numpy.test()" ; \
	done
	# Python 3.2 maps to python3/ dir alone? bah
	-for v in $(PY3VERS) ; do \
		echo "-- running tests for "$$v" plain --" ; \
		python$$v -c "import sys ; sys.path.insert(0, '$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/') ; import numpy; numpy.test()" ; \
		echo "-- running tests for "$$v" debug --" ; \
		python$$v-dbg -c "import sys ; sys.path.insert(0, '$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/') ; import numpy; numpy.test()" ; \
	done
endif
