#!/usr/bin/make -f

# Verbose mode
#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/"

%:
	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 tests/test_overrides.conf tests/test_overrides.conf.orig
	cp $(CURDIR)/debian/tests/test_overrides.conf $(CURDIR)/tests/test_overrides.conf
	sed -i 's|%CUR_DIR%|$(CURDIR)|g' $(CURDIR)/tests/test_overrides.conf
	bash run_tests.sh -N
	mv $(CURDIR)/tests/test_overrides.conf.orig $(CURDIR)/tests/test_overrides.conf
	rm -rf $(CURDIR)/debian/tests/testing.db $(CURDIR)/debian/tests/keystone-signing
endif

override_dh_auto_build:
	dh_auto_build
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
	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 0600 $(CURDIR)/etc/keystone.conf.sample  $(CURDIR)/debian/keystone/etc/keystone/keystone.conf

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
	dh_clean

get-orig-source:
	uscan --verbose --force-download --rename --destdir=../build-area
