#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)
#PYTHON3S:=$(shell py3versions -vr)

UPSTREAM_GIT = git://github.com/openstack/gnocchi.git
include /usr/share/openstack-pkg-tools/pkgos.make

export OSLO_PACKAGE_VERSION=$(VERSION)
UNIT_TEST_BLACKLIST = test_carbonara.CarbonaraCmd.*

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

override_dh_auto_install:
	set -e ; for pyvers in $(PYTHONS); do \
		python$$pyvers setup.py install --install-layout=deb \
			--root $(CURDIR)/debian/python-gnocchi; \
	done
	mkdir -p $(CURDIR)/debian/python-gnocchi/usr/lib/python2.7/dist-packages/gnocchi/indexer
	cp -auxf gnocchi/indexer/alembic $(CURDIR)/debian/python-gnocchi/usr/lib/python2.7/dist-packages/gnocchi/indexer
#	set -e ; for pyvers in $(PYTHON3S); do \
#		python$$pyvers setup.py install --install-layout=deb \
#			--root $(CURDIR)/debian/python3-gnocchi; \
#	done
	rm -rf $(CURDIR)/debian/python*-gnocchi/usr/lib/python*/dist-packages/*.pth

	mkdir -p $(CURDIR)/debian/gnocchi-common/usr/share/gnocchi-common
	oslo-config-generator --output-file $(CURDIR)/debian/gnocchi-common/usr/share/gnocchi-common/gnocchi.conf \
		--wrap-width 140 \
		--namespace gnocchi \
		--namespace oslo.db \
		--namespace oslo.log \
		--namespace oslo.policy \
		--namespace keystonemiddleware.auth_token
	sed -i 's|^[ \t#]*url[ \t#]*=.*|url = sqlite:////var/lib/gnocchi/gnocchidb|' $(CURDIR)/debian/gnocchi-common/usr/share/gnocchi-common/gnocchi.conf
	sed -i 's|^[# \t]*auth_protocol[\t #]*=.*|auth_protocol = http|' $(CURDIR)/debian/gnocchi-common/usr/share/gnocchi-common/gnocchi.conf

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

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	@echo "===> Running tests"
	set -e ; set -x ; for i in 2.7 ; do \
		PYMAJOR=`echo $$i | cut -d'.' -f1` ; \
		echo "===> Starting PGSQL" ; \
		BINDIR=`pg_config --bindir` ; \
		PG_MYTMPDIR=`mktemp -d` ; \
		chown postgres:postgres $$PG_MYTMPDIR || true ; \
		export PGHOST=$$PG_MYTMPDIR ; \
		chmod +x debian/start_pg.sh ; \
		debian/start_pg.sh $$PG_MYTMPDIR ; \
		export GNOCCHI_TEST_INDEXER_URL="postgresql:///template1?host=$$PG_MYTMPDIR&port=9823" ; \
		export GNOCCHI_TEST_STORAGE_DRIVER=file ; \
		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 'gnocchi\.tests\.(?!.*('"$(UNIT_TEST_BLACKLIST)"'))' | tee $$TEMP_REZ | subunit2pyunit ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f $$TEMP_REZ ; \
		testr-python$$PYMAJOR slowest ; \
		echo "===> Stopping PGSQL" ; \
		$$BINDIR/pg_ctl stop -D $$PG_MYTMPDIR ; \
	done
endif

override_dh_sphinxdoc:
#		echo "===> Starting PGSQL" ; \
#		BINDIR=`pg_config --bindir` ; \
#		PG_MYTMPDIR=`mktemp -d` ; \
#		chown postgres:postgres $$PG_MYTMPDIR || true ; \
#		export PGHOST=$$PG_MYTMPDIR ; \
#		chmod +x debian/start_pg.sh ; \
#		debian/start_pg.sh $$PG_MYTMPDIR ; \
#		export GNOCCHI_TEST_INDEXER_URL="postgresql:///template1?host=$$PG_MYTMPDIR&port=9823" ; \
#	PYTHONPATH=. sphinx-build -b html doc/source debian/python-gnocchi-doc/usr/share/doc/python-gnocchi-doc/html ; \
#	dh_sphinxdoc -O--buildsystem=python_distutils ; \
#		$$BINDIR/pg_ctl stop -D $$PG_MYTMPDIR
	echo "Do nothing"

override_dh_clean:
	dh_clean -O--buildsystem=python_distutils
	rm -rf build debian/gnocchi-common.postinst debian/gnocchi-common.config debian/gnocchi-api.config debian/gnocchi-api.postinst

# Commands not to run
override_dh_installcatalogs:
override_dh_installemacsen override_dh_installifupdown:
override_dh_installinfo override_dh_installmenu override_dh_installmime:
override_dh_installmodules override_dh_installpam override_dh_installppp override_dh_installudev override_dh_installwm:
override_dh_installxfonts override_dh_gconf override_dh_icons override_dh_perl override_dh_usrlocal:
override_dh_installgsettings:
