#!/usr/bin/make -f

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

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

override_dh_auto_build:
	dh_auto_build
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func barbican-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func barbican-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func barbican-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func barbican-common.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 "===> 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 'barbican\.tests\.(?!(.*common.test_utils\.WhenTestingAcceptEncodingGetter\.test_get_correct_fullname_for_class.*|.*common\.test_utils\.WhenTestingGenerateFullClassnameForInstance\.test_returns_qualified_name.*|.*plugin\.interface\.test_certificate_manager\.WhenTestingCertificateEventPluginManager\.test_get_plugin_by_name.*|.*plugin\.interface\.test_certificate_manager\.WhenTestingCertificatePluginManager\.test_get_plugin_by_ca_id.*|.*plugin\.interface\.test_certificate_manager\.WhenTestingCertificatePluginManager\.test_get_plugin_by_name.*|.*plugin\.interface\.test_certificate_manager\.WhenTestingCertificatePluginManager\.test_refresh_ca_list.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_delete_secret_assert_called.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_generate_asymmetric_key_assert_called.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_generate_symmetric_key_assert_called.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_get_secret_opaque.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_get_secret_private_key.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_get_secret_public_key.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_get_secret_symmetric.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_store_private_key_secret_assert_called.*|.*plugin\.test_kmip\.WhenTestingKMIPSecretStore\.test_store_symmetric_secret_assert_called.*|.*tasks\.test_keystone_consumer\.WhenUsingKeystoneEventConsumerProcessMethod\.test_existing_project_entities_cleanup_for_plain_secret.*))' | tee $$TEMP_REZ | subunit2pyunit ; \
		cat $$TEMP_REZ | subunit-filter -s --no-passthrough | subunit-stats ; \
		rm -f $$TEMP_REZ ; \
		testr-python$$PYMAJOR slowest ; \
	done
endif

override_dh_auto_install:
	echo "Do nothing here..."

override_dh_install:
	python setup.py install --root=debian/tmp --install-layout=deb

	# Generate the barbican.conf config using installed python-barbican files.
	mkdir -p $(CURDIR)/debian/barbican-common/usr/share/barbican-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/barbican-common/usr/share/barbican-common/barbican.conf \
		--wrap-width 140 \
		--namespace barbican.certificate.plugin \
		--namespace barbican.certificate.plugin.snakeoil \
		--namespace barbican.common.config \
		--namespace barbican.plugin.crypto \
		--namespace barbican.plugin.crypto.p11 \
		--namespace barbican.plugin.crypto.simple \
		--namespace barbican.plugin.dogtag \
		--namespace barbican.plugin.secret_store \
		--namespace barbican.plugin.secret_store.kmip \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.middleware.cors \
		--namespace oslo.middleware.http_proxy_to_wsgi \
		--namespace oslo.policy \
		--namespace oslo.service.periodic_task \
		--namespace oslo.service.sslutils \
		--namespace oslo.service.wsgi

	# Same with policy.conf
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages oslopolicy-sample-generator \
		--output-file $(CURDIR)/debian/barbican-common/usr/share/barbican-common/policy.json \
		--namespace barbican

	dh_install --fail-missing

	# Restore sanity...
	pkgos-fix-config-default $(CURDIR)/debian/barbican-common/usr/share/barbican-common/barbican.conf keystone_notifications enable True

	# Installing the other config files.
	for i in api_audit_map.conf barbican-api-paste.ini barbican-functional.conf vassals/barbican-api.ini ; do \
		install -D -m 0664 etc/barbican/$$i $(CURDIR)/debian/barbican-common/usr/share/barbican-common/$$i ; \
	done

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. sphinx-build -b html doc/source $(CURDIR)/debian/barbican-doc/usr/share/doc/barbican-doc/html
	dh_sphinxdoc
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -f debian/*.init debian/*.service debian/*.upstart
	rm -rf build
	rm -rf barbican.sqlite
	rm -f debian/barbican-api.postinst debian/barbican-api.config debian/barbican-common.postinst debian/barbican-common.config
