#!/usr/bin/make -f

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/' | head -n 1)

PYTHONS:=$(shell pyversions -vr)

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

override_dh_auto_install:
	python2.7 setup.py install --install-layout=deb --root=$(CURDIR)/debian/tmp
	cp -auxf neutron $(CURDIR)/debian/tmp/usr/lib/python2.7/dist-packages
	rm -rf $(CURDIR)/debian/tmp/usr/etc

override_dh_install:
	dh_install --fail-missing -X/usr/etc -X/usr/bin

	mkdir -p $(CURDIR)/debian/neutron-dhcp-agent/etc/neutron
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-dhcp-agent/etc/neutron/dhcp_agent.ini \
		--wrap-width 140 \
		--namespace neutron.base.agent \
		--namespace neutron.dhcp.agent \
		--namespace oslo.log
	mkdir -p $(CURDIR)/debian/neutron-l3-agent/etc/neutron
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-l3-agent/etc/neutron/l3_agent.ini \
		--wrap-width 140 \
		--namespace neutron.base.agent \
		--namespace neutron.l3.agent \
		--namespace oslo.log
	mkdir -p $(CURDIR)/debian/neutron-linuxbridge-agent/etc/neutron/plugins/ml2
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-linuxbridge-agent/etc/neutron/plugins/ml2/linuxbridge_agent.ini \
		--wrap-width 140 \
		--namespace neutron.ml2.linuxbridge.agent \
		--namespace oslo.log
	mkdir -p $(CURDIR)/debian/neutron-metadata-agent/usr/share/neutron-metadata-agent
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-metadata-agent/usr/share/neutron-metadata-agent/metadata_agent.ini \
		--wrap-width 140 \
		--namespace neutron.metadata.agent \
		--namespace oslo.log
	sed -i -e 's|^# nova_metadata_ip = 127.0.0.1|nova_metadata_ip = 127.0.0.1|'	$(CURDIR)/debian/neutron-metadata-agent/usr/share/neutron-metadata-agent/metadata_agent.ini
	mkdir -p $(CURDIR)/debian/neutron-metering-agent/etc/neutron
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-metering-agent/etc/neutron/metering_agent.ini \
		--wrap-width 140 \
		--namespace neutron.metering.agent \
		--namespace oslo.log
	mkdir -p $(CURDIR)/debian/neutron-common/etc/neutron/plugins/ml2
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-common/etc/neutron/plugins/ml2/ml2_conf.ini \
		--wrap-width 140 \
		--namespace neutron.ml2 \
		--namespace oslo.log
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-common/etc/neutron/plugins/ml2/ml2_conf_sriov.ini \
		--wrap-width 140 \
		--namespace neutron.ml2.sriov \
		--namespace oslo.log
	mkdir -p $(CURDIR)/debian/neutron-common/usr/share/neutron-common
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-common/usr/share/neutron-common/neutron.conf \
		--wrap-width 140 \
		--namespace neutron \
		--namespace neutron.agent \
		--namespace neutron.db \
		--namespace neutron.extensions \
		--namespace neutron.qos \
		--namespace nova.auth \
		--namespace oslo.log \
		--namespace oslo.db \
		--namespace oslo.policy \
		--namespace oslo.concurrency \
		--namespace oslo.messaging \
		--namespace oslo.middleware.cors \
		--namespace oslo.service.sslutils \
		--namespace oslo.service.wsgi \
		--namespace keystonemiddleware.auth_token
	mkdir -p $(CURDIR)/debian/neutron-common/usr/share/neutron-common
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-common/usr/share/neutron-common/openvswitch_agent.ini \
		--wrap-width 140 \
		--namespace neutron.ml2.ovs.agent \
		--namespace oslo.log
	mkdir -p $(CURDIR)/debian/neutron-common/etc/neutron/plugins/ml2
	oslo-config-generator --output-file $(CURDIR)/debian/neutron-common/etc/neutron/plugins/ml2/sriov_agent.ini \
		--wrap-width 140 \
		--namespace neutron.ml2.sriov.agent \
		--namespace oslo.log
	
override_dh_auto_clean:
	dh_auto_clean
	find . -type f -name "*.pyc" | xargs rm -fr
	rm -rf build neutron.egg-info
	rm -f debian/neutron-common.config \
		debian/neutron-common.postinst \
		debian/neutron-server.config \
		debian/neutron-server.postinst \
		debian/neutron-dhcp-agent.postinst \
		debian/neutron-metadata-agent.config \
		debian/neutron-metadata-agent.postinst
	rm -f debian/*.init debian/*.service debian/*.upstart

override_dh_clean:
	dh_clean
	find . -type f -name "*.pyc" -delete

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=build/* ./run_tests.sh -N -P
endif

override_dh_auto_build:
	dh_auto_build -O--buildsystem=python_distutils

	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func neutron-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func neutron-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func neutron-server.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func neutron-server.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func neutron-dhcp-agent.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func neutron-metadata-agent.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func neutron-metadata-agent.config
