#!/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

SKIP_TEST_LIST += mdconvert pi_stacking.ipynb nmr.ipynb test_image_molecules

# Bug#1129346  IndexError: index exceeds dimension bounds
SKIP_TEST_LIST += test_iterload[nc]

# internet tests
SKIP_TEST_LIST += test_3 from_url test_1vii_url_and_gz test_1vii_load_from_mixture

SKIP_TESTS = $(shell  my_skip_tests=""; \
        list_initialised=0; \
        for t in $(SKIP_TEST_LIST); do \
            if [ $${list_initialised} = 0 ]; then \
                my_skip_tests=$$t; \
                list_initialised=1; \
            else \
                my_skip_tests="$${my_skip_tests} or $$t"; \
            fi; \
        done; \
        if [ "x$${my_skip_tests}" != "x" ]; then \
            echo "not ( $${my_skip_tests} )"; \
        fi; )

export PYBUILD_TEST_ARGS=-k '$(SKIP_TESTS)'

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

execute_after_dh_auto_clean:
	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 mdtraj/core/lib
	rm -rf docs/api/generated
	rm -rf __pycache__ .pybuild
	find . -name "lib*.a" -delete

execute_after_dh_auto_install-indep: export http_proxy=127.0.0.1:9
execute_after_dh_auto_install-indep: export https_proxy=127.0.0.1:9
execute_after_dh_auto_install-indep:
	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

execute_after_dh_python3:
	# setup.py builds the static lib libtheobald.a for each python version
	# but dh_python3 can't tell the difference between the different builds,
	# since py3.11 compiles libtheobald.o with -fwrapv, where py3.12 uses -fno-strict-overflow
	# So clean up manually (assume the binary difference is not an issue)
	rm -f debian/python3-$(PYBUILD_NAME)/usr/lib/python3.*/dist-packages/mdtraj/core/lib/libtheobald.a
	for d in debian/python3-$(PYBUILD_NAME)/usr/lib/python3.*/dist-packages/mdtraj/core/lib; do if [-d $$d]; then rmdir -p --ignore-fail-on-non-empty $$d; fi; done

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
