#!/usr/bin/make -f

BASE_PACKAGE = azure

# Package all submodules except azure-sdk-tools, azure-devtools, *nspkg
MODULES = $(filter-out sdk/core/azure-core-tracing-opencensus sdk/core/azure-core-tracing-opentelemetry sdk/cosmos/azure-cosmos sdk/storage/azure-storage-blob sdk/storage/azure-storage-queue %-nspkg tools/azure-devtools tools/azure-sdk-tools tools/azure-sdk-tools/packaging_tools tools/azure-sdk-tools/devtools_testutils,$(subst /setup.py,,$(wildcard */*/*/setup.py)))

# rules with wildcards do not like '/', so we have to escape. Use three dots as it's unlikely to be used in a name.
MODULES_ESCAPE = $(subst /,...,$(MODULES))

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS))$(filter nodoc,$(DEB_BUILD_PROFILES)))
	HELPERS = python3,sphinxdoc
else
	HELPERS = python3
endif

%:
	dh $@ --with $(HELPERS) --buildsystem=pybuild


override_dh_auto_clean:
	dh_auto_clean
	set -e; $(foreach module, $(MODULES), \
	if [ -f $(module)/setup.py.orig ]; then \
		mv $(module)/setup.py.orig $(module)/setup.py; \
	fi;)


# dh_auto_configure overrides

dh_auto_configure-%:
	cp $(subst ...,/,$*)/setup.py $(subst ...,/,$*)/setup.py.orig
	sed 's/~=/>=/g' $(subst ...,/,$*)/setup.py.orig > $(subst ...,/,$*)/setup.py
	sed -i '/^\s*azure-storage-/d' $(subst ...,/,$*)/setup.py
	PYBUILD_NAME=$(notdir $(subst -,_,$(subst ...,/,$*))) dh_auto_configure --sourcedirectory=$(subst ...,/,$*)
	echo $(notdir $(subst ...,/,$*)) python3-$(BASE_PACKAGE) >> debian/python3-$(BASE_PACKAGE).pydist
	echo $(notdir $(subst ...,/,$*)) python3-$(BASE_PACKAGE) >> debian/py3dist-overrides

override_dh_auto_configure: $(foreach pyvermodule, $(MODULES_ESCAPE), dh_auto_configure-$(pyvermodule))


# dh_auto_build overrides

dh_auto_build-%:
	PYBUILD_NAME=$(notdir $(subst -,_,$(subst ...,/,$*))) dh_auto_build --sourcedirectory=$(subst ...,/,$*)

override_dh_auto_build: $(foreach pyvermodule, $(MODULES_ESCAPE), dh_auto_build-$(pyvermodule))
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS))$(filter nodoc,$(DEB_BUILD_PROFILES)))
	PYTHONPATH=. http_proxy='127.0.0.1:9' python3 -m sphinx -N -bhtml doc/sphinx doc/sphinx/_build/html # HTML generator
endif


# dh_auto_install overrides

$(CURDIR)/debian/python3-$(BASE_PACKAGE):
	mkdir -p $@

dh_auto_install-%: $(CURDIR)/debian/python3-$(BASE_PACKAGE)
	PYBUILD_NAME=$(notdir $(subst -,_,$(subst ...,/,$*))) dh_auto_install --sourcedirectory=$(subst ...,/,$*)

	cp -rv $(CURDIR)/debian/python3-$(notdir $(subst ...,/,$*))/usr $(CURDIR)/debian/python3-$(BASE_PACKAGE)
	rm -r $(CURDIR)/debian/python3-$(notdir $(subst ...,/,$*))

dh_auto_install-sdk...core...$(BASE_PACKAGE):
	PYBUILD_NAME=$(BASE_PACKAGE) dh_auto_install --sourcedirectory=sdk/core/$(BASE_PACKAGE)
	# Make the build reproducible
	sort --stable -o debian/python3-$(BASE_PACKAGE).pydist debian/python3-$(BASE_PACKAGE).pydist

override_dh_auto_install: $(foreach pyvermodule, $(MODULES_ESCAPE), dh_auto_install-$(pyvermodule))

# dh_auto_test overrides

override_dh_auto_test:
# Don't test at build time: the circular dependency on python-azure-storage
# makes this way too messy
