#!/usr/bin/make -f

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

include /usr/share/dpkg/buildflags.mk

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

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

# cftime, cfgrib fail on 32-bit and big-endian archs. Don't test there
NO_CF_ARCH:= i386 armel armhf s390x
DEB_BUILD_OPTIONS +=     $(if $(filter $(DEB_TARGET_ARCH), $(NO_CF_ARCH)),  nocheck,  )

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=-k 'not (test_weighted_operations_keep_attr or test_reduce_keepdims or test_zarr_region_chunk_partial_offset or test_chunk_encoding_with_dask)' --pyargs xarray

%:
	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:
	ln -s /usr/lib/python3/dist-packages/jupyter_sphinx/thebelab/thebelab-helper.js \
		debian/python-xarray-doc/usr/share/doc/python-xarray-doc/html/_static
	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
