#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)
include /usr/share/openstack-pkg-tools/pkgos.make

export OSLO_PACKAGE_VERSION=$(shell dpkg-parsechangelog | grep Version: | cut -d' ' -f2 | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/~/.0/' -e 's/+dfsg1//' | head -n 1)

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

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	antlr3 congress/datalog/Congress.g
	ln -s .testr.conf.old .testr.conf
	PYTHONPATH=$(CURDIR) bash run_tests.sh -N -P
	rm -f .testr.conf
endif

override_dh_clean:
	dh_clean
	rm -f debian/congress-common.config debian/congress-common.postinst debian/congress-server.config debian/congress-api.postinst
	rm -rf debian/*.upstart debian/*.service
	rm -f .testr.conf

override_dh_install:
	dh_install --fail-missing

	mkdir -p $(CURDIR)/debian/congress-common/usr/share/congress-common
	PYTHONPATH=. oslo-config-generator --output-file $(CURDIR)/debian/congress-common/usr/share/congress-common/congress.conf \
		--wrap-width 79 \
		--namespace congress \
		--namespace oslo.log \
		--namespace oslo.policy \
		--namespace oslo.db \
		--namespace oslo.middleware.cors \
		--namespace keystonemiddleware.auth_token
	sed -i 's/[ \t#]*auth_protocol[ \t]*=.*/auth_protocol = http/' $(CURDIR)/debian/congress-common/usr/share/congress-common/congress.conf
	install -D -m 0644 $(CURDIR)/etc/api-paste.ini $(CURDIR)/debian/congress-common/usr/share/congress-common/api-paste.ini
	install -D -m 0644 $(CURDIR)/etc/policy.json $(CURDIR)/debian/congress-common/usr/share/congress-common/policy.json
	cp congress/db/migration/alembic.ini $(CURDIR)/debian/python-congress/usr/lib/python2.7/dist-packages/congress/db/migration/

override_dh_auto_build:
	set -e && for pyvers in $(PYTHONS); do \
		python$$pyvers setup.py install --install-layout=deb \
			--root $(CURDIR)/debian/tmp; \
        done

	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func congress-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func congress-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func congress-server.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func congress-server.postinst
