#!/usr/bin/make -f

BASE_PACKAGE = azure-storage

# Package all submodules
MODULES = $(subst /setup.py,,$(wildcard */setup.py))

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

# Mark the nspkg modules as "py2", other packages as "pyall"
PYVER_MODULES = $(foreach module, $(filter %-nspkg,$(MODULES)), py2-$(module)) $(foreach module, $(filter-out %-nspkg,$(MODULES)), pyall-$(module))


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

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


# dh_auto_clean overrides

dh_auto_clean-py2-%:
	PYBUILD_DISABLE_python3=1 PYBUILD_NAME=$* dh_auto_clean --sourcedirectory=$*
	if [ -f $(module)/setup.py.orig ]; then \
		mv $(module)/setup.py.orig $(module)/setup.py; \
	fi

dh_auto_clean-pyall-%:
	PYBUILD_NAME=$* dh_auto_clean --sourcedirectory=$*
	if [ -f $(module)/setup.py.orig ]; then \
		mv $(module)/setup.py.orig $(module)/setup.py; \
	fi

override_dh_auto_clean: $(foreach pyvermodule, $(PYVER_MODULES), dh_auto_clean-$(pyvermodule))


# dh_auto_configure overrides

dh_auto_configure-py2-%:
	cp $*/setup.py $*/setup.py.orig
	sed 's/~=/>=/g' $*/setup.py.orig > $*/setup.py
	PYBUILD_DISABLE_python3=1 PYBUILD_NAME=$* dh_auto_configure --sourcedirectory=$*
	echo $* python-$(BASE_PACKAGE) >> debian/python-$(BASE_PACKAGE).pydist
	echo $* python-$(BASE_PACKAGE) >> debian/pydist-overrides

dh_auto_configure-pyall-%:
	cp $*/setup.py $*/setup.py.orig
	sed 's/~=/>=/g' $*/setup.py.orig > $*/setup.py
	PYBUILD_NAME=$* dh_auto_configure --sourcedirectory=$*
	echo $* python-$(BASE_PACKAGE) >> debian/python-$(BASE_PACKAGE).pydist
	echo $* python-$(BASE_PACKAGE) >> debian/pydist-overrides
	echo $* python3-$(BASE_PACKAGE) >> debian/python3-$(BASE_PACKAGE).pydist
	echo $* python3-$(BASE_PACKAGE) >> debian/py3dist-overrides

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


# dh_auto_build overrides

dh_auto_build-py2-%:
	PYBUILD_DISABLE_python3=1 PYBUILD_NAME=$* dh_auto_build --sourcedirectory=$*

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

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


# dh_auto_install overrides

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

dh_auto_install-py2-%: $(CURDIR)/debian/python-$(BASE_PACKAGE)
	PYBUILD_DISABLE_python3=1 PYBUILD_NAME=$* dh_auto_install --sourcedirectory=$*

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

dh_auto_install-pyall-%: $(CURDIR)/debian/python3-$(BASE_PACKAGE) $(CURDIR)/debian/python-$(BASE_PACKAGE)
	PYBUILD_NAME=$* dh_auto_install --sourcedirectory=$*

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

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

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


# dh_auto_test overrides

override_dh_auto_test:
# Don't test at build time: generating PYTHONPATH would be quite messy
