#!/usr/bin/make -f

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

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

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	antlr3 congress/datalog/Congress.g
	PYTHONPATH=$(CURDIR) bash run_tests.sh -N -P
	rm -f congress/datalog/CongressLexer.py congress/datalog/CongressParser.py
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 congress/datalog/CongressLexer.py congress/datalog/CongressParser.py

override_dh_auto_install:
	echo "Please do nothing."

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

override_dh_install:
	antlr3 congress/datalog/Congress.g
	sed -i '1 s/#.*/# Generated by antlr3/' congress/datalog/CongressParser.py
	sed -i '1 s/#.*/# Generated by antlr3/' congress/datalog/CongressLexer.py
	set -e ; for pyvers in $(PYTHONS); do \
		python$$pyvers setup.py install --install-layout=deb \
			--root $(CURDIR)/debian/tmp; \
        done
	rm -rf $(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages/congress/datalog/Python*

	mkdir -p $(CURDIR)/debian/congress-common/usr/share/congress-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python2.7/dist-pacakges 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 oslo.messaging \
		--namespace keystonemiddleware.auth_token
	sed -i 's/[ \t#]*auth_protocol[ \t]*=.*/auth_protocol = http/' $(CURDIR)/debian/congress-common/usr/share/congress-common/congress.conf

	dh_install --fail-missing

	cp congress/db/migration/alembic.ini $(CURDIR)/debian/python-congress/usr/lib/python2.7/dist-packages/congress/db/migration/
