#!/usr/bin/make -f

UPSTREAM_GIT = git://github.com/stackforge/sqlalchemy-migrate.git
include /usr/share/openstack-pkg-tools/pkgos.make

export OSLO_PACKAGE_VERSION=$(VERSION)

include /usr/share/python/python.mk

DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog \
	| sed -rne 's,^Version: ([^-]+).*,\1,p')
DOCS=$(shell find docs/ -name "*.rst" -printf "%p ")
PYLIBDIR=$(call py_libdir,$(shell pyversions -d))

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

override_dh_auto_clean:
	rm -rf dist build
	find . -name '*\.py[co]' -exec rm {} \;
	rm -f docs/reference.txt debian/install
#	cp debian/test_db.cfg $(CURDIR)

override_dh_auto_install:
	# install documentation
	PYTHONPATH=$(CURDIR) sphinx-build -b html -a -E -N \
	  doc/source \
	  debian/python-migrate/usr/share/doc/python-migrate/html/
	cp debian/install.tmpl debian/install
	find migrate/versioning/templates -type f ! -name '*.py' | \
	while read name; do \
		echo "$$name $(PYLIBDIR)/`dirname $$name`" >> debian/install; \
	done
	dh_auto_install

override_dh_installchangelogs:
	dh_installchangelogs -i doc/source/changelog.rst

override_dh_compress:
	dh_compress -i -X.py -X.rst -X.js -Xobjects.inv

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	chmod +x debian/test.sh

	# Create a working "migrate" binary, so that shell calls continue to
	# work even without python-migrate being installed
	mkdir -p debian/bin
	cp debian/migrate-for-tests debian/bin/migrate
	sed -i "s#REPLACE_THIS_PATH#$(CURDIR)#" debian/bin/migrate

	# Create a minimalist egg-info, so that "import migrate"
	# works and gets the correct version
	mkdir -p sqlalchemy_migrate-$(VERSION).egg-info
	echo "Metadata-Version: 1.1" >sqlalchemy_migrate-$(VERSION).egg-info/PKG-INFO
	echo "Name: sqlalchemy-migrate" >>sqlalchemy_migrate-$(VERSION).egg-info/PKG-INFO
	echo "Version: $(VERSION)" >>sqlalchemy_migrate-$(VERSION).egg-info/PKG-INFO

	# Launch the actual tests
	$(CURDIR)/debian/test.sh

	# Clean everything
	rm -rf debian/bin
	rm -rf sqlalchemy_migrate-$(VERSION).egg-info
endif
