#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
export DH_VERBOSE = 1

export PYBUILD_NAME=mdtraj

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --with sphinxdoc --buildsystem=pybuild

override_dh_auto_clean:
	dh_auto_clean || /bin/true
	for f in mdtraj/formats/binpos/binpos.c \
	  mdtraj/formats/dcd/dcd.c		\
	  mdtraj/formats/dtr/dtr.cpp		\
	  mdtraj/formats/tng/tng.c		\
	  mdtraj/formats/xtc/trr.c		\
	  mdtraj/formats/xtc/xtc.c		\
	  mdtraj/geometry/drid.cpp		\
	  mdtraj/geometry/neighborlist.cpp	\
	  mdtraj/geometry/neighbors.cpp		\
	  mdtraj/geometry/src/_geometry.cpp	\
	  mdtraj/rmsd/_lprmsd.cpp		\
	  mdtraj/rmsd/_rmsd.cpp			\
	  mdtraj/version.py;                    \
	do rm -f $$f; done
	rm -rf __pycache__ .pybuild
	find . -name "lib*.a" -delete

override_dh_auto_build-indep: export http_proxy=127.0.0.1:9
override_dh_auto_build-indep: export https_proxy=127.0.0.1:9
override_dh_auto_build-indep:
	dh_auto_build
	cp -a $(CURDIR)/examples $(CURDIR)/examples.orig
	mkdir -p $(CURDIR)/build/html/examples/iterload
	ln -s $(CURDIR)/examples/data $(CURDIR)/build/html/examples/iterload/data
	sed "s|http://www.rcsb.org/pdb/files|$(CURDIR)/debian/external|g" -i $(CURDIR)/examples/*.ipynb
	PYTHONPATH=$(shell pybuild --pyver `py3versions --default -v` --print build_dir | awk '{print $$3}' ) \
	    MPLCONFIGDIR=$(CURDIR)/build/matplotlib \
		make -C docs BUILDDIR=$(CURDIR)/build  html
	for img in `find $(CURDIR)/build/html/examples -name *.png`; do \
	    mv $$img $(CURDIR)/build/html/examples; \
	done
	sed "s|$(CURDIR)/build/html/examples/|./|g; s|.ipynb|.ipynb.gz|g" -i $(CURDIR)/build/html/examples/*.html
	sed "s|$(CURDIR)/debian/external|http://www.rcsb.org/pdb/files|g" -i $(CURDIR)/build/html/examples/*.html
	rm $(CURDIR)/build/html/examples/iterload/data
	rm -rf $(CURDIR)/examples
	mv $(CURDIR)/examples.orig $(CURDIR)/examples

override_dh_python3:
	dh_python3
	dh_numpy3
	# FIXME: No idea why a copy of this static lib (is it needed at all???) remains here.  There is another one in usr/lib/python3/dist-packages
	rm -f debian/python3-$(PYBUILD_NAME)/usr/lib/python3.1*/dist-packages/mdtraj/core/lib/libtheobald.a

override_dh_sphinxdoc-indep:
	dh_sphinxdoc -i
	sed "s|src=\"https://img.shields.io/badge/DOI-10.1016%2Fj.bpj.2015.08.015-blue.svg\"|src=\"_static/doi-10.1016_j.bpj.2015.08.015-blue.svg\"|g" -i debian/python-mdtraj-doc/usr/share/doc/python-mdtraj-doc/html/index.html
