#!/usr/bin/make -f

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

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

#export http_proxy=127.0.0.1:9
#export https_proxy=127.0.0.1:9
#export HTTP_PROXY=127.0.0.1:9
#export HTTPS_PROXY=127.0.0.1:9

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

override_dh_auto_clean:
	rm -rf .testrepository *.egg-info
	find . -type d -name __pycache__ -exec rm -r {} \+
	rm -rf build
	rm -f tempest.log tempest.conf.sample doc/source/plugins/plugin-registry.rst doc/source/_static/tempest.conf.sample etc/tempest.conf.sample

override_dh_auto_build:
	echo "Do nothing..."

override_dh_auto_install:
	pkgos-dh_auto_install --no-py2

	# Cleanups
	rm -rf $(CURDIR)/debian/python*/usr/etc

	mkdir -p $(CURDIR)/debian/tempest/etc/tempest
	PYTHONPATH=$(CURDIR)/debian/python3-tempest/usr/lib/python3/dist-packages oslo-config-generator --output-file $(CURDIR)/debian/tempest/etc/tempest/tempest.conf \
		--wrap-width 140 \
		--namespace tempest.config \
		--namespace oslo.concurrency \
		--namespace oslo.log
	
	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/rbac-persona-accounts.yaml.sample $(CURDIR)/debian/tempest/etc/tempest/rbac-persona-accounts.yaml
	install -D -m 0664 etc/allow-list.yaml $(CURDIR)/debian/tempest/etc/tempest/allow-list.yaml

	# Fix-up some preferences
	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf DEFAULT verbose false
	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf DEFAULT log_file tempest.log
	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf DEFAULT log_dir /var/log
	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf DEFAULT log_file tempest.log

	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf oslo_concurrency lock_path /var/lock

	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf auth admin_tenant_name admin
	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf auth admin_username admin
	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf auth admin_domain_name Default

	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf compute fixed_network_name demo-net
	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf compute region regionOne

	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf identity disable_ssl_certificate_validation true
	pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf identity auth_version v3
	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf identity region regionOne

	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf image region regionOne

	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf network region regionOne

	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf orchestration region regionOne
	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf orchestration region regionOne

	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf volume region regionOne

	# Otherwise, there's some cinder tempest failures
	#pkgos-fix-config-default $(CURDIR)/debian/tempest/etc/tempest/tempest.conf volume-feature-enabled volume_services true

	# 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/python3/dist-packages/tempest $(CURDIR)/debian/tempest/var/lib/tempest/tempest
	cp .stestr.conf $(CURDIR)/debian/tempest/var/lib/tempest
	install -D -m 775 debian/tempest_shell_wrapper $(CURDIR)/debian/tempest/usr/bin/tempest_debian_shell_wrapper

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

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	for pyvers in $(PYTHON3S) ; do \
		rm -rf .stestr ; \
		PYTHONPATH=$(CURDIR)/debian/python-tempest/usr/lib/python3/dist-packages PYTHON=python$$pyvers stestr --test-path ./tempest/tests run | subunit2pyunit ; \
		stestr slowest ; \
		rm -rf .stestr ; \
	done
endif

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

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