#!/usr/bin/make -f

BASE_PACKAGE = azure

# Package all submodules except azure-sdk-tools
MODULES = $(filter-out azure-sdk-tools,$(subst /setup.py,,$(wildcard */setup.py)))

# The actual python module (egg-info) names have underscores instead of dashes
PYDIST_MODULES = $(subst -,_,$(MODULES))

# Filter out the nspkg modules
PY3_MODULES = $(foreach module, $(filter-out %-nspkg,$(MODULES)), $(module))

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 $*/setup.py $*/setup.py.orig
	sed 's/~=/>=/g' $*/setup.py.orig > $*/setup.py
	sed -i '/azure-storage-/d' $*/setup.py
	PYBUILD_NAME=$* dh_auto_configure --sourcedirectory=$*
	echo $* python3-$(BASE_PACKAGE) >> debian/python3-$(BASE_PACKAGE).pydist
	echo $* python3-$(BASE_PACKAGE) >> debian/py3dist-overrides

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


# dh_auto_build overrides

dh_auto_build-%:
	PYBUILD_NAME=$* dh_auto_build --sourcedirectory=$*

override_dh_auto_build: $(foreach pyvermodule, $(PY3_MODULES), 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/ doc/_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=$* dh_auto_install --sourcedirectory=$*

	cp -rv $(CURDIR)/debian/python3-$*/usr $(CURDIR)/debian/python3-$(BASE_PACKAGE)
	rm -r $(CURDIR)/debian/python3-$*

dh_auto_install-$(BASE_PACKAGE):
	PYBUILD_NAME=$(BASE_PACKAGE) dh_auto_install --sourcedirectory=$(BASE_PACKAGE)

override_dh_auto_install: $(foreach pyvermodule, $(PY3_MODULES), 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
