#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# Path to tests differs for pybuild and autopkgtest environments
# Architecture determination also differs.
ifeq ($(PYBUILD_AUTOPKGTEST),1)
TESTPATH := tests
TESTARCH := $(shell dpkg --print-architecture)
else
TESTPATH := xarray/tests
include /usr/share/dpkg/buildflags.mk
TESTARCH := $(DEB_TARGET_ARCH)
endif

export PYBUILD_NAME=xarray
PY3VERS:= $(shell py3versions -s)

# Disable  for this release:
# - tests need pytest < 8
# DEB_BUILD_OPTIONS += nodoc

export PYDEVD_DISABLE_FILE_VALIDATION=1

# Needed for pooch temp data directory
export env=/tmp/pooch

export MPLCONFIGDIR=$(CURDIR)/debian/build
export HOME=$(CURDIR)/debian/build

# Stop parallel tests,, broken for some reason?
# export PYBUILD_TEST_ARGS=-r fEs -n auto -k 'not (test_weighted_operations_keep_attr or test_reduce_keepdims)' --pyargs xarray

export PYBUILD_TEST_ARGS := --deselect=$(TESTPATH)/test_weighted.py::test_weighted_operations_keep_attr --deselect=$(TESTPATH)/test_variable.py::TestVariable::test_reduce_keepdims --deselect=$(TESTPATH)/test_backends.py::test_zarr_region_chunk_partial_offset --deselect=$(TESTPATH)/test_backends.py::test_chunk_encoding_with_dask --pyargs xarray

# Some tests fail on 32-bit archs; skip those tests
BIT32_ARCH := i386 armel armhf
ifneq (,$(filter $(TESTARCH), $(BIT32_ARCH)))
export PYBUILD_TEST_ARGS := --deselect=$(TESTPATH)/test_cftimeindex.py::test_asi8 --deselect=$(TESTPATH)/test_datatree.py::TestRepr::test_doc_example $(PYBUILD_TEST_ARGS) --deselect=$(TESTPATH)/test_strategies.py::TestReduction::test_mean
endif

# And a couple of further failures on i386
ifeq (i386,$(TESTARCH))
export PYBUILD_TEST_ARGS := --deselect=$(TESTPATH)/test_groupby.py::test_groupby_reduce_dimension_error --deselect=$(TESTPATH)/test_groupby.py::TestDataArrayGroupBy::test_groupby_reduce_keep_attrs $(PYBUILD_TEST_ARGS)
endif

%:
	dh $@  --buildsystem=pybuild

execute_after_dh_auto_clean:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C doc clean
endif

execute_after_dh_auto_install: $(PYTHON3:%=install-python%)
	find debian/python3-xarray -name '*.idx' -exec chmod -x {} \;
	find debian -name  test.nc -delete 

override_dh_auto_build:
	http_proxy=127.0.0.1:9 https_proxy=127.0.0.1:9 dh_auto_build
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. http_proxy='127.0.0.1:9' sphinx-build -bhtml doc doc/_build/html
endif

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
override_dh_sphinxdoc:
	dh_sphinxdoc --exclude=MathJax.js
	find debian/python-xarray-doc -name '*.html' \
	 -exec grep require.js {} /dev/null \; | cut -f1 -d: | while read r; do \
	sed -e 's%https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4% file:/usr/share/javascript/requirejs%' \
	< $$r > debian/tmp/x ; \
	mv debian/tmp/x $$r ; \
	done
endif