#!/usr/bin/make -f

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

export OSLO_PACKAGE_VERSION=$(VERSION)

LAST_CHANGE = $(shell dpkg-parsechangelog -S Date)
BUILD_DATE  = $(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")

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

override_dh_auto_clean:
	dh_auto_clean -O--buildsystem=python_distutils
	rm -f .testrepository

override_dh_auto_install:
	dh_auto_install -O--buildsystem=python_distutils
	mkdir -p $(CURDIR)/debian/tempest/etc/tempest
	PYTHONPATH=. oslo-config-generator --output-file $(CURDIR)/debian/tempest/etc/tempest/tempest.conf \
		--wrap-width 140 \
		--namespace tempest.config \
		--namespace oslo.concurrency \
		--namespace oslo.i18n \
		--namespace oslo.log \
		--namespace oslo.serialization \
		--namespace oslo.utils
	
	install -D -m 0664 etc/logging.conf.sample $(CURDIR)/debian/tempest/etc/tempest/logging.conf
	install -D -m 0664 etc/accounts.yaml.sample $(CURDIR)/debian/tempest/etc/tempest/accounts.yaml
	install -D -m 0664 etc/whitelist.yaml $(CURDIR)/debian/tempest/etc/tempest/whitelist.yaml
	install -D -m 0664 etc/javelin-resources.yaml.sample $(CURDIR)/debian/tempest/etc/tempest/javelin-resources.yaml

	#cp -rfv etc/schemas $(CURDIR)/debian/tempest/etc/tempest
	cp tools/colorizer.py $(CURDIR)/debian/tempest/usr/lib/python2.7/dist-packages/tempest
	# Fix-up some preferences

	sed -i 's/^[ \t#]*lock_path[ \t]*=.*/lock_path=\/var\/lock/' $(CURDIR)/debian/tempest/etc/tempest/tempest.conf
	sed -i 's/^[ \t#]*username[ \t]*=.*/username=admin/' $(CURDIR)/debian/tempest/etc/tempest/tempest.conf
	sed -i 's/^[ \t#]*tenant_name[ \t]*=,*/tenant_name=admin/' $(CURDIR)/debian/tempest/etc/tempest/tempest.conf
	sed -i 's/^[ \t#]*cli_dir[ \t]*=.*/cli_dir=\/usr\/bin/' $(CURDIR)/debian/tempest/etc/tempest/tempest.conf
	sed -i 's/^[ \t#]*allow_tenant_isolation[ \t]*=.*/allow_tenant_isolation=true/' $(CURDIR)/debian/tempest/etc/tempest/tempest.conf
	sed -i 's/^[ \t#]*image_ssh_user=.*/image_ssh_user=debian/' $(CURDIR)/debian/tempest/etc/tempest/tempest.conf

	# Do not use verbose by default: it is a way too verbose and not
	# really helpful, as failed tests will anyway dump the trace.
	sed -i 's/^[ \t#]*verbose[ \t]*=.*/verbose = false/' $(CURDIR)/debian/tempest/etc/tempest/tempest.conf

	# Package stuff in /var/lib/tempest, since we need read/write access
	# to run the testr stuff
	mkdir -p $(CURDIR)/debian/tempest/var/lib/tempest
	ln -s /usr/lib/python2.7/dist-packages/tempest $(CURDIR)/debian/tempest/var/lib/tempest/tempest
	cp .testr.conf $(CURDIR)/debian/tempest/var/lib/tempest
	install -D -m 775 debian/tempest_shell_wrapper $(CURDIR)/debian/tempest/usr/bin/tempest

	rm -fr $(CURDIR)/debian/tempest/usr/etc

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	rm -rf .testrepository
	testr init
	set -e && \
		TEMP_REZ=`mktemp -t` && \
		export OS_TEST_PATH=./tempest/tests && \
		PYTHONPATH=$(CURDIR) testr run --parallel --subunit 'tests\.(?!.*test_pretty_tox.*)' | tee $$TEMP_REZ | subunit2pyunit || true ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats || true ; \
		rm -f $$TEMP_REZ ;
	testr slowest
endif

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. sphinx-build -D html_last_updated_fmt="$(BUILD_DATE)" -b html doc/source $(CURDIR)/debian/tempest/usr/share/doc/tempest/html
	dh_sphinxdoc -O--buildsystem=python_distutils
endif
