#!/usr/bin/make -f

# auth_token middleware now requires access to HOME, otherwise
# test suite cause FTBFS.  See (LP: #1031022)
export HOME="$(CURDIR)/debian/tests/"

# This is to avoid git clone of keystoneclient when running unit tests.
# See https://review.openstack.org/122768/
export KSCTEST_PATH=/usr/lib/python3/dist-packages/keystoneclient

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

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

override_dh_clean:
	rm -rf $(CURDIR)/build $(CURDIR)/keystone.egg-info $(CURDIR)/.cache
	rm -rf $(CURDIR)/keystone/test/keystone.sql.log $(CURDIR)/keystone/test/keystone.ldap.log
	rm -rf $(CURDIR)/doc/build
	# NOTE(adam_g) The following stuff is built in /doc/source.  Safe for now, but
	# should be fixed upstream or updated here post-essex
	rm -rf $(CURDIR)/doc/source/keystone*.rst
	rm -rf $(CURDIR)/doc/source/modules.rst
	rm -rf $(CURDIR)/doc/source/sourcecode $(CURDIR)/doc/source/api
	rm -f $(CURDIR)/doc/source/_static/keystone.conf.sample
	rm -f $(CURDIR)/doc/source/_static/keystone.policy.yaml.sample
	rm -rf $(CURDIR)/admin.log $(CURDIR)/keystone-legacy-auth.log
	rm -f $(CURDIR)/debian/keystone.config $(CURDIR)/debian/keystone.postinst debian/keystone.service debian/keystone.init
	find $(CURDIR)/keystone/tests -name '*.db' -delete
	rm -rf $(CURDIR)/vendor keystone/tests/unit/tmp keystone/tests/tmp
	rm -rf $(CURDIR)/.stestr '"'
	rm -f $(CURDIR)/debian/tests/testing.db
	rm -f $(CURDIR)/debian/keystone.keystone*.init $(CURDIR)/debian/keystone.keystone*.service $(CURDIR)/debian/keystone.keystone*.upstart
	find $(CURDIR) -name '*.pyc' -delete
	for i in $$(find . -type d -iname __pycache__) ; do rm -rf $$i ; done
	rm -f AUTHORS ChangeLog
	dh_clean

override_dh_auto_build:
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func keystone.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func keystone.postinst

override_dh_auto_clean:
	python3 setup.py clean

override_dh_auto_test:
	echo "Do nothing..."

override_dh_auto_install:
	echo "Doing nothing..."

override_dh_install:
	for i in $(PYTHON3S) ; do \
		PYTHON=python$$i python$$i setup.py install --install-layout=deb --root $(CURDIR)/debian/tmp ; \
	done

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1078623
	# and https://bugs.launchpad.net/keystone/+bug/2076992
	# test_v3_auth\.TrustAPIBehavior\.test_redelegate_with_role_by_name
	# test_v3_auth\.TrustAPIBehavior\.test_redelegation_terminator
	# test_v3_auth\.TrustAPIBehavior\.test_roles_subset
	# test_v3_auth\.TestTrustAuthFernetTokenProvider\.test_redelegate_with_role_by_name
	# test_v3_auth\.TestTrustAuthFernetTokenProvider\.test_redelegation_terminator
	# test_v3_auth\.TestTrustAuthFernetTokenProvider\.test_roles_subset
	# Not indenpotent:
	# test_cli\.CliBootStrapTestCase\.test_bootstrap_is_not_idempotent_when_password_does_change

	@echo "===> Running tests"
	mkdir -p $(CURDIR)/keystone/tests/tmp
	pkgos-dh_auto_test --no-py2 'keystone\.tests\.unit\.(?!(.*test_v3_federation\.SAMLGenerationTests\.test_sign_assertion_exc.*|.*test_sql_upgrade.FullMigration.*|.*test_hacking_checks\.TestCheckForMutableDefaultArgs\.test.*|.*common\.test_notifications\.NotificationsTestCase.*|.*test_v3_auth\.TestAuthInfo\.test_get_method_names_duplicates.*|.*test_backend_sql\.SqlCatalog\.test_get_service_returns_not_found.*|.*tests\.test_core\.TestTestCase\.test_deprecation_warnings_are_raised_as_exceptions_in_tests.*|.*test_v3_federation.*SAMLGenerationTests.*test_sign_assertion_logs_message_if_xmlsec1_is_not_installed.*|test_v3_auth\.TrustAPIBehavior\.test_redelegate_with_role_by_name|test_v3_auth\.TrustAPIBehavior\.test_redelegation_terminator|test_v3_auth\.TrustAPIBehavior\.test_roles_subset|test_v3_auth\.TestTrustAuthFernetTokenProvider\.test_redelegate_with_role_by_name|test_v3_auth\.TestTrustAuthFernetTokenProvider\.test_redelegation_terminator|test_v3_auth\.TestTrustAuthFernetTokenProvider\.test_roles_subset|test_v3_federation\.SAMLGenerationTests\.test_saml_signing|test_v3_assignment\.AssignmentInheritanceTestCase\.test_project_id_specified_if_include_subtree_specified|test_cli\.CliBootStrapTestCase\.test_bootstrap_is_not_idempotent_when_password_does_change))'
endif

	rm -rf $(CURDIR)/debian/tmp/usr/etc
	dh_install
	dh_missing --fail-missing
	rm -rf debian/python3-keystone/usr/lib/python*/*/doc
	rm -rf debian/python3-keystone/usr/lib/python*/*/tools
	rm -rf debian/python3-keystone/usr/lib/python*/*/examples
	install -D -m 0600 $(CURDIR)/debian/logging.conf $(CURDIR)/debian/keystone/etc/keystone/logging.conf
	mkdir -p $(CURDIR)/debian/keystone/usr/share/keystone
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf \
		--wrap-width 140 \
		--namespace keystone \
		--namespace oslo.cache \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.policy \
		--namespace oslo.db \
		--namespace oslo.middleware \
		--namespace osprofiler
	pkgos-fix-config-default $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf catalog template_file /etc/keystone/default_catalog.templates
	pkgos-fix-config-default $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf DEFAULT log_dir /var/log/keystone
	pkgos-fix-config-default $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf DEFAULT log_file keystone.log
	pkgos-fix-config-default $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf federation sso_callback_template /etc/keystone/sso_callback_template.html

	mkdir -p $(CURDIR)/debian/keystone/etc/keystone/policy.d
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output-file $(CURDIR)/debian/keystone/etc/keystone/policy.d/00_default_policy.yaml \
		--format yaml \
		--namespace keystone

	# Comment out deprecated policies
	sed -i 's/^"/#"/' $(CURDIR)/debian/keystone/etc/keystone/policy.d/00_default_policy.yaml

	# Use the policy.d folder
	pkgos-fix-config-default $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf oslo_policy policy_dirs /etc/keystone/policy.d

	install -D -m 0644 debian/keystone.apparmor $(CURDIR)/debian/keystone/etc/apparmor.d/usr.bin.keystone-all
	dh_apparmor --profile-name=usr.bin.keystone-all

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

override_dh_installman:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. PYTHON=python3 python3 -m sphinx -b man doc/source doc/build/man
	dh_installman
endif

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

#override_dh_installmenu:
#	dh_installinit --name=keystone-admin
#	dh_installinit --name=keystone-public