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

include /usr/share/dpkg/architecture.mk

empty :=
space := $(empty)$(empty)

# test_pmg_view in test_pmg.py generates an XIO error when shutting down (after successfully running the test)
SKIP_TEST_LIST += test_pmg_view

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_auto_clean:
	rm -f pymatgen/optimization/linear_assignment.c pymatgen/optimization/neighbors.c pymatgen/util/coord_cython.c
	rm -rf pd.json .hypothesis tmp_dir

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
execute_after_dh_auto_build-indep: export http_proxy=127.0.0.1:9
execute_after_dh_auto_build-indep: export https_proxy=127.0.0.1:9
execute_after_dh_auto_build-indep:
	PYTHONPATH=$(shell pybuild --pyver `py3versions --default -v` --print build_dir | awk '{print $$3}' ) \
	  sphinx-build docs/apidoc build/html
endif

override_dh_auto_test:
	SKIP_TESTS=""; \
	list_initialised=0; \
	for t in $(SKIP_TEST_LIST); do \
	    if [ $${list_initialised} = 0 ]; then \
	        SKIP_TESTS=$$t; \
	        list_initialised=1; \
	    else \
	        SKIP_TESTS="$${SKIP_TESTS} or $$t"; \
	    fi; \
	done; \
	if [ "x$${SKIP_TESTS}" != "x" ]; then \
	    SKIP_TESTS="not ( $${SKIP_TESTS} )"; \
	fi; \
	echo "skipping tests: $${SKIP_TESTS}"; \
	for py in `py3versions -sv`; do \
	  pybuilddir=`pybuild --pyver $$py --print build_dir | awk '{print $$3}'`; \
	  testdir=$(CURDIR)/.pybuild/test_python$$py; \
	  cp -a $$pybuilddir $$testdir; \
	  cp -a tests $$testdir; \
	  PMG_TEST_FILES_DIR=/usr/share/doc/pymatgen-test-files/examples/test-files \
	    PYTHONPATH=$$testdir PATH=$$pybuilddir/../scripts:$$PATH \
	    python$$py -m pytest -v $(EXTRA_TEST_FLAGS) --import-mode=prepend --color=no -k "$${SKIP_TESTS}" $$testdir; \
	done

execute_after_dh_python3:
	dh_numpy3
