#!/usr/bin/make -f

# Verbose mode
#export DH_VERBOSE=1

%:
	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	
	# TEMP: ignore failures until bug 961871 is resolved
	bash run_tests.sh -N
	mv $(CURDIR)/tests/test_overrides.conf.orig $(CURDIR)/tests/test_overrides.conf
	rm -rf $(CURDIR)/debian/tests/testing.db
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

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

UPSTREAM_GIT=git://github.com/openstack/keystone.git
DEBIAN_VERSION=$(shell dpkg-parsechangelog |  sed -n 's/^Version: \(.*\)-[^-]*/\1/p')
DEBIAN_NAME=keystone

get-orig-source-git:
	git clone $(UPSTREAM_GIT) $(DEBIAN_NAME)
	cd $(DEBIAN_NAME) ; \
	git checkout stable/essex ; \
	python setup.py sdist ; \
	mv dist/keystone-2012.1.tar.gz ../keystone_$(DEBIAN_VERSION).orig.tar.gz
