#!/usr/bin/make -f

include /usr/share/openstack-pkg-tools/pkgos.make

%:
	dh $@ --buildsystem=python_distutils --with python2,sphinxdoc,systemd

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	echo "Warning: tests don't work with gabbi >= 1.25, this has to be fixed"
#	@echo "===> Running tests"
#	set -e ; set -x ; for i in 2.7 ; do \
#		PYMAJOR=`echo $$i | cut -d'.' -f1` ; \
#		echo "===> Testing with python$$i (python$$PYMAJOR)" ; \
#		rm -rf .testrepository ; \
#		testr-python$$PYMAJOR init ; \
#		TEMP_REZ=`mktemp -t` ; \
#		PYTHONPATH=$(CURDIR) PYTHON=python$$i testr-python$$PYMAJOR run --subunit --parallel 'cloudkitty\.tests.*' | tee $$TEMP_REZ | subunit2pyunit || true; \
#		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats || true; \
#		rm -f $$TEMP_REZ ; \
#		testr-python$$PYMAJOR slowest ; \
#        done
endif

override_dh_auto_build:
	dh_auto_build -O--buildsystem=python_distutils

	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cloudkitty-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cloudkitty-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cloudkitty-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cloudkitty-api.postinst

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	# Warning: building doc fails.
	sphinx-build $(SPHINXOPTS) -b html doc/source $(CURDIR)/debian/cloudkitty-doc/usr/share/doc/cloudkitty-doc/html || true
	dh_sphinxdoc -O--buildsystem=python_distutils
endif

override_dh_installman:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	echo "No man pages for now"
#	sphinx-build $(SPHINXOPTS) -b man doc/source doc/build/man
#	dh_installman -O--buildsystem=python_distutils
endif

override_dh_install:
	rm -rf $(CURDIR)/debian/tmp
	python2.7 setup.py install --install-layout=deb --root $(CURDIR)/debian/tmp
	dh_install -O--buildsystem=python_distutils --fail-missing
	# Generate all config files
	mkdir -p $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common/cloudkitty.conf \
		--namespace cloudkitty.common.config \
		--namespace oslo.concurrency \
		--namespace oslo.db \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.middleware.http_proxy_to_wsgi \
		--namespace oslo.middleware.cors \
		--namespace oslo.policy \
		--namespace keystonemiddleware.auth_token

	# Set everything to use [keystone_authtoken] by default, otherwise it points to a non-working config
	pkgos-fix-config-default $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common/cloudkitty.conf ceilometer_collector auth_section keystone_authtoken
	pkgos-fix-config-default $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common/cloudkitty.conf gnocchi_collector auth_section keystone_authtoken
	pkgos-fix-config-default $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common/cloudkitty.conf keystone_fetcher auth_section keystone_authtoken

	# Set the lock_path
	pkgos-fix-config-default $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common/cloudkitty.conf oslo_concurrency lock_path /var/lock/cloudkitty

	# Fixed default backend
	pkgos-fix-config-default $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common/cloudkitty.conf tenant_fetcher backend keystone

	for i in api_paste.ini policy.json ; do \
		install -D -m 0664 etc/cloudkitty/$$i $(CURDIR)/debian/cloudkitty-common/usr/share/cloudkitty-common/$$i ; \
	done

override_dh_auto_clean:
	find . -iname '*.pyc' -delete
	rm -f debian/cloudkitty-common.config debian/cloudkitty-common.postinst
	rm -f debian/cloudkitty-api.config debian/cloudkitty-api.postinst
	rm -f debian/*.init debian/*.upstart debian/*.service
	rm -rf build
