#!/usr/bin/make -f

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

OCTAVIA_CONF=$(CURDIR)/debian/octavia-common/usr/share/octavia-common/octavia.conf

%:
	dh $@ --buildsystem=pybuild --with python3,sphinxdoc

override_dh_auto_clean:
	python3 setup.py clean
	find . -iname '*.pyc' -delete
	rm -f AUTHORS
	rm -f ChangeLog
	rm -rf debian/po
	rm -f debian/octavia-agent.octavia-agent.init debian/octavia-api.init debian/octavia-health-manager.init debian/octavia-housekeeping.init debian/octavia-worker.init debian/octavia-driver-agent.init
	rm -f debian/octavia-agent.octavia-agent.service debian/octavia-api.service debian/octavia-health-manager.service debian/octavia-housekeeping.service debian/octavia-worker.service
	rm -rf octavia.sqlite
	rm -rf octavia.egg-info
	rm -rf .eggs
	rm -rf doc/source/contributor/devref/flow_diagrams
	rm -rf doc/source/contributor/modules
	rm -f doc/source/configuration/_static/octavia.policy.yaml.sample
	rm -f doc/source/contributor/devref/erd.svg
	rm -f debian/octavia-api.postinst debian/octavia-api.config debian/octavia-common.postinst debian/octavia-common.config
	rm -f debian/octavia-api.templates debian/octavia-common.postrm octavia-driver-agent.service octavia-health-manager.config octavia-health-manager.postinst
	rm -f debian/octavia-health-manager.postinst debian/octavia-driver-agent.service debian/octavia-common.templates debian/octavia-health-manager.config
	rm -rf doc/source/contributor/devref/flow_diagrams_v2
	rm -rf build .stestr doc/source/configuration/_static
	find . -iname '*.pyc' -delete
	for i in $$(find . -type d -iname __pycache__) ; do rm -rf $$i ; done

override_dh_auto_build:
	cp -auxf debian/po.in debian/po
	set -e ; for i in $(PYTHON3S) ; do \
		python3 setup.py build --force ; \
	done

	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func octavia-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func octavia-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func octavia-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func octavia-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func octavia-health-manager.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func octavia-health-manager.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_postrm octavia-common.postrm
	pkgos-merge-templates octavia-api octavia endpoint
	pkgos-merge-templates octavia-common octavia db rabbit ksat

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	pkgos-dh_auto_test --no-py2 'octavia\.tests\.unit\.(?!.*cmd\.test_status\.TestUpgradeChecks\.test__check_amphorav2_warning|.*cmd\.test_status\.TestUpgradeChecks\.test__check_persistence_sqlite|amphorae\.drivers\.haproxy\.test_rest_api_driver_1_0\.TestAmphoraAPIClientTest\.test_.*_missing)'
endif

override_dh_auto_install:
	echo "Do nothing here..."

override_dh_install:
	for i in $(PYTHON3S) ; do \
		python$$i setup.py install --root=debian/tmp --install-layout=deb ; \
	done

	mkdir -p $(CURDIR)/debian/octavia-agent/usr/bin
	cp elements/amphora-agent/static/usr/local/bin/lvs-masquerade.sh $(CURDIR)/debian/octavia-agent/usr/bin/octavia-agent-lvs-masquerade

	# Generate the octavia.conf config using installed python-octavia files.
	mkdir -p $(CURDIR)/debian/octavia-common/usr/share/octavia-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/octavia-common/usr/share/octavia-common/octavia.conf \
		--wrap-width 140 \
		--namespace octavia \
		--namespace oslo.db \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.middleware.cors \
		--namespace oslo.middleware.http_proxy_to_wsgi \
		--namespace oslo.middleware.healthcheck \
		--namespace oslo.policy \
		--namespace keystonemiddleware.audit \
		--namespace keystonemiddleware.auth_token \
		--namespace cotyledon \
		--namespace castellan.config

	# Use the policy.d folder
	pkgos-fix-config-default $(CURDIR)/debian/octavia-common/usr/share/octavia-common/octavia.conf oslo_policy policy_dirs /etc/octavia/policy.d

	# Readd missing options in keystone_authtoken
	pkgos-readd-keystone-authtoken-missing-options $(OCTAVIA_CONF) keystone_authtoken octavia

	# Fix default values
	pkgos-fix-config-default $(OCTAVIA_CONF) oslo_messaging topic octavia_prov
	pkgos-fix-config-default $(OCTAVIA_CONF) service_auth project_domain_name Default
	pkgos-fix-config-default $(OCTAVIA_CONF) service_auth user_domain_name Default
	pkgos-fix-config-default $(OCTAVIA_CONF) service_auth auth_type password
	pkgos-fix-config-default $(OCTAVIA_CONF) controller_worker amphora_driver amphora_haproxy_rest_driver
	pkgos-fix-config-default $(OCTAVIA_CONF) controller_worker compute_driver compute_nova_driver
	pkgos-fix-config-default $(OCTAVIA_CONF) controller_worker network_driver allowed_address_pairs_driver

	# Same with policy.conf
	mkdir -p $(CURDIR)/debian/octavia-common/etc/octavia/policy.d
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output-file $(CURDIR)/debian/octavia-common/etc/octavia/policy.d/00_default_policy.yaml \
		--format yaml \
		--namespace octavia

	# Comment out deprecated policies
	sed -i 's/^"/#"/' $(CURDIR)/debian/octavia-common/etc/octavia/policy.d/00_default_policy.yaml

	# Restore sanity...
	pkgos-fix-config-default $(CURDIR)/debian/octavia-common/usr/share/octavia-common/octavia.conf keystone_notifications enable True

	rm -rf $(CURDIR)/debian/tmp/usr/etc
	find debian/tmp/  -type d -name __pycache__ -exec rm -r {} \+

	dh_install

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	python3 -m sphinx $(SPHINXOPTS) -b html doc/source $(CURDIR)/debian/octavia-doc/usr/share/doc/octavia-doc/html
	dh_sphinxdoc
endif

override_dh_missing:
	dh_missing --fail-missing

override_dh_fixperms:
	dh_fixperms
	chmod +x $(CURDIR)/debian/octavia-agent/var/lib/octavia/ping-wrapper.sh

# We use override_dh_installmenu because it's done in the sequence with dh,
# and we already override dh_installinit in pkgos.make.
# Obviously, we will never use a Desktop menu in such a package, so that's
# not a problem.
override_dh_installmenu:
	dh_installinit -poctavia-agent --name=octavia-agent
	dh_installinit -poctavia-agent --name=octavia-agent-certs-ramfs
	dh_installinit -poctavia-agent --name=octavia-agent-netns

override_dh_installsystemd:
	dh_installsystemd -poctavia-api
	dh_installsystemd -poctavia-health-manager
	dh_installsystemd -poctavia-housekeeping
	dh_installsystemd -poctavia-worker
	dh_installsystemd -poctavia-agent --name=octavia-agent-netns
	dh_installsystemd -poctavia-agent --name=octavia-agent-certs-ramfs
	dh_installsystemd -poctavia-agent --name=octavia-agent
	dh_installsystemd -poctavia-driver-agent --name=octavia-driver-agent