#! /usr/bin/make -f

# export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk

export UPSTREAM_VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+.*//')

SUBPROJECTS = \
extensions/classification \
extensions/datacube \
extensions/eo \
extensions/file \
extensions/grid \
extensions/item_assets \
extensions/label \
extensions/mgrs \
extensions/mlm \
extensions/pointcloud \
extensions/projection \
extensions/raster \
extensions/render \
extensions/sar \
extensions/sat \
extensions/scientific \
extensions/storage \
extensions/table \
extensions/timestamps \
extensions/version \
extensions/view \
extensions/xarray_assets

export PYBUILD_BEFORE_TEST=cp -R {dir}/extensions {dir}/tests {dir}/pytest-pystac/pytest_pystac {dir}/pyproject.toml {build_dir}
export PYBUILD_AFTER_TEST=$(RM) -r {build_dir}/extensions {build_dir}/tests {build_dir}/pyproject.toml
export PYBUILD_TEST_ARGS=-p pytest_pystac.plugin -m "not noproxy" {build_dir}/extensions/*/tests {build_dir}/tests {build_dir}/pytest_pystac

%:
	dh $@ --buildsystem=pybuild

execute_after_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	env PYTHONPATH=$(CURDIR) http_proxy='http://127.0.0.1:9/' https_proxy='https://127.0.0.1:9/' \
	sphinx-build -N -E -T -b html $(CURDIR)/docs $(CURDIR)/.pybuild/docs/html
	rm -rf $(CURDIR)/.pybuild/docs/html/.doctrees
endif

override_dh_auto_build:
	dh_auto_build -- --dir core --name pystac-core
	$(foreach proj,$(SUBPROJECTS),\
	    dh_auto_build -- --dir $(proj) --name pystac-ext-$(subst _,-,$(subst extensions/,,$(proj))); \
	)
	dh_auto_build -- --dir . --name pystac

override_dh_auto_test:
	echo "Delay autopkgtest"

override_dh_auto_install:
	dh_auto_install -- --dir core --name pystac-core --dest-dir=debian/tmp
	$(foreach proj,$(SUBPROJECTS),\
	    dh_auto_install -- --dir $(proj) --name pystac-ext-$(subst _,-,$(subst extensions/,,$(proj))) --dest-dir=debian/tmp || exit; \
	)
	dh_auto_install -- --dir . --name pystac --dest-dir=debian/tmp

execute_after_dh_auto_install:
	dh_auto_test
	find debian/tmp/ -name __pycache__ -exec $(RM) -r {} +
