#!/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=mdanalysis
export PYBUILD_DIR=package

# parallel and openmp tests are unstable, often timing out
SKIP_TEST_LIST += parallel multiprocess openmp

# gsd tends to induce test time-outs, https://github.com/MDAnalysis/mdanalysis/issues/4209
SKIP_TEST_LIST += gsd GSD

# NSGrid is flaky and causes distance tests to often (randomly) fail
# https://github.com/MDAnalysis/mdanalysis/issues/4906
SKIP_TEST_LIST += test_distances

# mdahole2, pathsimanalysis are not yet packaged for debian
# (duecredit test journal.pcbi.1004568 accesses pathsimanalysis)
SKIP_TEST_LIST += test_all_import[.analysis.hole2] journal.pcbi.1004568

ifneq (,$(findstring $(DEB_HOST_ARCH), i386))
  export DEB_CFLAGS_MAINT_APPEND += -ffloat-store -fexcess-precision=fast

  SKIP_TEST_LIST += test_multiprocess_COG[u4] test_ramachandran
  SKIP_TEST_LIST += test_clustering_three_ensembles_two_identical test_hbond_analysis
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), s390x))
  SKIP_TEST_LIST += test_start_stop_step test_count_by_time \
        test_rmsd test_mass_weighted test_custom_weighted test_ref_mobile_mass_mismapped \
        test_time test_dt test_total_time test_iter test_last_frame test_frame_jump \
        test_next_gives_second_frame test_set_time test_write_istart \
        test_write_trajectory_atomgroup test_write_trajectory_universe test_Timestep_time \
        test_rename_aux test_namdbin ref_reader25 ref_reader_extra_args25
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), hppa))
  SKIP_TEST_LIST += Parallel parallel Serial \
	test_frame_bool_fail FrameAnalysis test_start_stop_step test_AnalysisFromFunction_args_content \
	test_bat_multifragments_atomgroup test_distance_box test_n_initial_contacts \
	test_density_analysis_conversion_default_unit \
	test_trajectory[client_DSSP test_atomgroup[client_DSSP test_trajectory_with_hydrogens[client_DSSP \
	test_get_distance_matrix TestHydrogenBondAnalysisTIP3P \
	test_cosine_content TestRMSD \
	"(TestChainReader and test_time)" TestChainReaderFormats TestChainReaderContinuous test_issue_4008 \
	TestDCDReader test_write_istart TestDCDWriter test_writer_dt test_other_writer \
	test_writer_no_h5py TestLAMMPSDCDReader TestMemoryReader TestNAMDBINReader TestNCDFWriter \
	test_xdr test_write_frame_none test_tpr_fragments test_reader_wo_timeseries test_libmdaxdr \
	test_universe_creation_from_coordinates test_universe_unpickle_in_new_process test_creating_multiple_universe_without_offset \
	test_universe_with_minimal test_streamplot test_frame_slice[run_kwargs TestDensityAnalysis \
	test_diffusionmap test_dihedrals test_gnm test_random_walk_u_simple test_rdf_s TestRMSF \
	TestXVGReader TestXVGFileReader "(test_copying and test_reader_)" test_timestep_copied test_positions_share_memory \
	NAMDBINWriter test_NullWriter TestBaseTimestepInterface test_center_compounds_ TestSelectionsTPR \
	test_group_traj_access TestAllCoordinatesKwarg TestUpdatingSelection \
	test_multiprocess_COG[u1 test_readers_pickle[ref_reader17] test_readers_pickle[ref_reader20] test_readers_pickle[ref_reader30] \
	test_nojump_iterate_twice test_posavging_
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), ppc64))
  SKIP_TEST_LIST += test_start_stop_step TestHydrogenBondAnalysisTIP3P \
	TestRMSD "(TestChainReader and test_time)" TestDCDReader TestDCDWriter \
	test_write_istart TestLAMMPSDCDReader "(TestMemoryReader and (test_rename_aux or test_iter_as_aux))" \
	TestNAMDBINReader NAMDBINWriter "(test_copying and test_reader)" \
	test_timestep_copied[ref_reader25] test_positions_share_memory[ref_reader25]
endif

SKIP_TESTS=$(shell 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 \
            echo "not ( $${skip_tests} )"; \
        fi )

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

execute_after_dh_auto_clean:
	rm -f .duecredit.p
	rm -f package/MDAnalysis/authors.py
	rm -f package/MDAnalysis/analysis/encore/clustering/affinityprop.c \
	  package/MDAnalysis/analysis/encore/cutils.c \
	  package/MDAnalysis/analysis/encore/dimensionality_reduction/stochasticproxembed.c \
	  package/MDAnalysis/coordinates/timestep.cpp \
	  package/MDAnalysis/lib/_augment.cpp \
	  package/MDAnalysis/lib/_cutil.cpp \
	  package/MDAnalysis/lib/c_distances.c \
	  package/MDAnalysis/lib/c_distances_openmp.c \
	  package/MDAnalysis/lib/formats/cython_util.c \
	  package/MDAnalysis/lib/formats/libdcd.c \
	  package/MDAnalysis/lib/formats/libmdaxdr.c \
	  package/MDAnalysis/lib/nsgrid.cpp \
	  package/MDAnalysis/lib/qcprot.c
	rm -rf package/MDAnalysis.egg-info
	rm -f testsuite/MDAnalysisTests/data/.*_offsets.npz  testsuite/MDAnalysisTests/data/*/.*_offsets.npz
	rm -rf testsuite/.pytest_cache

override_dh_auto_test:
	set -e; \
	for py in $(shell py3versions -rv); do \
	  echo "=== testing with python$$py ==="; \
	  pydir=`pybuild -p $$py --system=distutils --print {build_dir}`; \
	  MPLBACKEND=agg PYTHONPATH=$$pydir python$$py -mpytest -v -k "$(SKIP_TESTS)" --disable-pytest-warnings testsuite; \
	  rm -rf $$pydir/MDAnalysis/.hypothesis; \
	  rm -rf $$pydir/MDAnalysis/.duecredit.p; \
	done

execute_after_dh_python3:
	dh_numpy3
	rm -rf debian/python3-mdanalysis/usr/lib/python3.*

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}) make html -C package/doc/sphinx/ BUILDDIR=$(CURDIR)/.pybuild  # HTML generator
