#!/usr/bin/make -f

UPSTREAM_GIT=https://github.com/gnocchixyz/python-gnocchiclient.git
include /usr/share/openstack-pkg-tools/pkgos.make

export SETUPTOOLS_SCM_PRETEND_VERSION=$(shell dpkg-parsechangelog -SVersion | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/~git.*//' -e 's/~/.0/' -e 's/+dfsg1//' -e 's/+ds1//' | head -n 1)
export PYBUILD_NAME=gnocchiclient

%:
	dh $@ --buildsystem=pybuild --with python3,sphinxdoc

override_dh_auto_clean:
	rm -rf build
	find .  -type d -name __pycache__ -exec rm -r {} \+
	dh_auto_clean

override_dh_install:
	dh_install
	# Generate bash completion
	mkdir -p $(CURDIR)/debian/python3-gnocchiclient/usr/share/bash-completion/completions
	PYTHONPATH=$(CURDIR)/.pybuild/cpython3_`py3versions -d -v`_gnocchiclient/build \
		PATH=$(PATH):$(CURDIR)/debian/python3-gnocchiclient/usr/bin \
		gnocchi complete \
		>$(CURDIR)/debian/python3-gnocchiclient/usr/share/bash-completion/completions/gnocchi
	sed -i 's#complete -F _python3_gnocchi python3-gnocchi#complete -F _python3_gnocchi gnocchi#' \
		$(CURDIR)/debian/python3-gnocchiclient/usr/share/bash-completion/completions/gnocchi

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	set -e ; set -x ; for i in $(PYTHON3S) ; do \
		PYTHON=python$$i PYTHONPATH=. python$$i -m pytest gnocchiclient/tests/unit ; \
	done
endif

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	python3 setup.py install --install-layout=deb --root $(CURDIR)/debian/tmp
	export LANG=C.UTF-8 ; export LC_ALL=C.UTF-8 ; set -e ; set -x ; if ! PATH=$$PATH:$(CURDIR)/debian/bin PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages python3 -m sphinx -b html doc/source $(CURDIR)/debian/python-gnocchiclient-doc/usr/share/doc/python-gnocchiclient-doc/html ; then \
		cat /tmp/sphinx-err* ; \
		exit 1 ; \
	fi
	dh_sphinxdoc
endif

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3
