#!/usr/bin/make -f

#export DH_VERBOSE=1

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

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

export OSLO_PACKAGE_VERSION=$(VERSION)

%:
	dh $@ --with python2

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# create a dummy db for testing
	rm -rf $(CURDIR)/debian/tests/testing.db
	migrate version_control sqlite:////$(CURDIR)/debian/tests/testing.db \
		$(CURDIR)/keystone/common/sql/migrate_repo
	# run migrations
	PYTHONPATH=$(CURDIR)  migrate upgrade \
		sqlite:////$(CURDIR)/debian/tests/testing.db \
		$(CURDIR)/keystone/common/sql/migrate_repo

	# tests/test_overrides.conf can be used to setup the environment
	# for unit testing.
	cp keystone/tests/test_overrides.conf keystone/tests/test_overrides.conf.orig
	cp $(CURDIR)/debian/tests/test_overrides.conf $(CURDIR)/keystone/tests/test_overrides.conf
	sed -i 's|%CUR_DIR%|$(CURDIR)|g' $(CURDIR)/keystone/tests/test_overrides.conf
	PYTHONPATH=$(CURDIR) bash run_tests.sh -N || true
	mv $(CURDIR)/keystone/tests/test_overrides.conf.orig $(CURDIR)/keystone/tests/test_overrides.conf
	rm -rf $(CURDIR)/debian/tests/testing.db $(CURDIR)/debian/tests/keystone-signing
endif

override_dh_auto_build:
	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

ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	python setup.py build_sphinx
else
	mkdir -p $(CURDIR)/doc/build/html
	mkdir -p $(CURDIR)/doc/build/man
endif

override_dh_install:
	dh_install --fail-missing
	rm -rf debian/python-keystone/usr/lib/python*/*/doc
	rm -rf debian/python-keystone/usr/lib/python*/*/tools
	rm -rf debian/python-keystone/usr/lib/python*/*/examples
	install -D -m 0600 $(CURDIR)/debian/logging.conf $(CURDIR)/debian/keystone/etc/keystone/logging.conf
	install -D -m 0640 etc/keystone.conf.sample $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf
	sed -i -e "s|^[ \t#]*template_file[ \t#]*=.*|template_file = /etc/keystone/default_catalog.templates|" $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf
	sed -i -e "s|^[ \t#]*log_dir[ \t#]*=.*|log_dir = /var/log/keystone|" $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf
	sed -i -e "s|^[ \t#]*log_file[ \t#]*=.*|log_file = keystone.log|" $(CURDIR)/debian/keystone/usr/share/keystone/keystone.conf
	cp -f etc/keystone.conf.sample debian/keystone/usr/share/doc/keystone/keystone.conf.sample

	install -D -m 0644 httpd/keystone.py $(CURDIR)/debian/python-keystone/usr/share/keystone/wsgi.py
	dh_python2 /usr/share/keystone

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 -rf $(CURDIR)/admin.log $(CURDIR)/keystone-legacy-auth.log
	rm -f $(CURDIR)/debian/keystone.config $(CURDIR)/debian/keystone.postinst
	find $(CURDIR)/keystone/tests -name '*.db' -delete
	rm -rf $(CURDIR)/vendor
	rm -f $(CURDIR)/debian/tests/testing.db
	dh_clean
