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

# bootstrapping problem with circular dependency between pymatgen and mp-api
# mp_api is only used by ext/matproj.py, so skip its test at build-time
# (in practice the matproj tests are skipped anyway since an MP API key is not available)
SKIP_TEST_LIST += test_matproj

# SeeK-path has not been packaged for debian
# so skip SeeK tests in io/pwmat/test_inputs.py
SKIP_TEST_LIST += TestGenKpt TestHighSymmetryPoint

# io/qchem is partly broken, skip failing tests
# io/qchem input
SKIP_TEST_LIST += test_from_multi_jobs_file test_from_string test_read_molecule test_read_nbo test_read_negative test_read_plots test_read_write_nbo7
# io/qchem output
SKIP_TEST_LIST += test_all test_custom_smd_write
# io/qchem sets (test_sets.py all fail)
SKIP_TEST_LIST += test_sets
# these io tests fail in chroot
SKIP_TEST_LIST += test_CifWriter test_read_write test_init test_get_site_symmetries
# times out
SKIP_TEST_LIST += BSDOSPlotterTest
# requires write permissions
SKIP_TEST_LIST += test_cdft_dc_parsing
# requires internet
SKIP_TEST_LIST += test_get_structure_by_id
# fragmenter openbabel test fails, see https://github.com/materialsproject/pymatgen/issues/3078
SKIP_TEST_LIST += test_babel_PC_with_RO_depth_0_vs_depth_10
# MoleculeMatcherTest.test_fit in test_molecule_matcher.py fails for some reason
SKIP_TEST_LIST += test_fit

ifneq (,$(findstring $(DEB_HOST_ARCH), arm64))
    SKIP_TEST_LIST += test_get_parchg "(TestAnharmonicQuasiharmociDebyeApprox and test_gruneisen_parameter)"
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), i386))
    SKIP_TEST_LIST += test_get_parchg test_numerical_eoswrapper "(TestAnharmonicQuasiharmociDebyeApprox and test_gruneisen_parameter)"
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), mips64el))
    SKIP_TEST_LIST += test_pca test_standard test_from_diff_fit test_relax_ase_opt_kwargs test_relax_ase_return_traj
    EXTRA_TEST_FLAGS += -v
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), ppc64el))
    SKIP_TEST_LIST += test_get_parchg test_numerical_eoswrapper "(TestAnharmonicQuasiharmociDebyeApprox and test_gruneisen_parameter)"
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), riscv64))
    SKIP_TEST_LIST += test_get_parchg "(TestAnharmonicQuasiharmociDebyeApprox and test_gruneisen_parameter)"
    SKIP_TEST_LIST += test_relax_ase_opt_kwargs test_relax_ase_return_traj test_rotate_sites
    EXTRA_TEST_FLAGS += -v
endif

# many VASP tests (and others) fail on s390x, hppa, powerpc ppc64
BAD_VASP_ARCH_LIST := s390x hppa powerpc ppc64
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(BAD_VASP_ARCH_LIST)$(space)))
    SKIP_TEST_LIST += test_spillage_from_vasprun test_process_parsed_hess test_from_file
    SKIP_TEST_LIST += test_optics test_generate_g_points test_generate_nbmax test_evaluate_wavefunc test_fft_mesh_advanced test_fft_mesh_basic
    SKIP_TEST_LIST += test_get_parchg test_n2_45210 test_n2_spin test_standard test_write_unks test_consistency
    SKIP_TEST_LIST += test_read_fe
    SKIP_TEST_LIST += "(TestAnharmonicQuasiharmociDebyeApprox and test_gruneisen_parameter)"
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), hppa))
    SKIP_TEST_LIST += test_cohp
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), hurd-i386))
    SKIP_TEST_LIST += plot test_structure_environments test_pca test_get_info_cohps_to_neighbors test_get_parchg test_as_dict_from_dict test_ir_spectra
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), powerpc))
    SKIP_TEST_LIST += test_pmg_structure
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), loong64))
    SKIP_TEST_LIST += test_get_parchg
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), sparc64))
    SKIP_TEST_LIST += test_spillage_from_vasprun test_read_fe test_process_parsed_hess \
	test_chdir_behavior test_no_return_and_box test_from_file test_optics test_evaluate_wavefunc \
	test_fft_mesh_advanced test_fft_mesh_basic test_generate_g_points test_generate_nbmax \
	test_get_parchg test_n2_45210 test_n2_spin test_standard test_write_unks test_consistency
endif


%:
	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/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