#!/usr/bin/make -f

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

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

override_dh_clean:
	dh_clean
	rm -rf heat.egg-info .testrepository
	rm -rf doc/build
	find . -iname '*.pyc' -delete
	rm -f run_tests.err.log
	rm -f \
		debian/heat-common.postinst \
		debian/heat-common.config
	rm -f debian/*.init debian/*.service debian/*.upstart debian/*.templates
	rm -f debian/heat-common.postinst debian/heat-common.config debian/heat-api.config debian/heat-api.postinst debian/heat-api-cfn.config debian/heat-api-cfn.postinst debian/heat-common.postrm

override_dh_auto_build:
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func heat-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func heat-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func heat-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func heat-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func heat-api-cfn.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func heat-api-cfn.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_postrm heat-common.postrm
	pkgos-merge-templates heat-api heat endpoint
	pkgos-merge-templates heat-api-cfn heat-api-cfn endpoint
	pkgos-merge-templates heat-common heat db rabbit ksat

override_dh_auto_clean:
	python3 setup.py clean

override_dh_auto_install:
	echo "Do nothing..."

override_dh_auto_test:
	echo "Do nothing..."

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

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	pkgos-dh_auto_test --no-py2 'heat\.tests\.(?!db\.test_migrations\.TestHeatMigrationsSQLite\.test_walk_versions|db\.test_migrations\.ModelsMigrationsSyncSQLite\.test_models_sync).*'
endif

	rm -rf $(CURDIR)/debian/tmp/usr/etc
	rm -r $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/heat_integrationtests/.gitignore
	mkdir -p $(CURDIR)/debian/heat-common/usr/share/heat-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages python3-oslo-config-generator \
		--namespace heat.common.config \
		--namespace heat.common.context \
		--namespace heat.common.crypt \
		--namespace heat.engine.clients.os.keystone.heat_keystoneclient \
		--namespace heat.common.wsgi \
		--namespace heat.engine.clients \
		--namespace heat.engine.notification \
		--namespace heat.engine.resources \
		--namespace heat.api.aws.ec2token \
		--namespace keystonemiddleware.auth_token \
		--namespace oslo.messaging \
                --namespace oslo.middleware \
		--namespace oslo.db \
		--namespace oslo.log \
                --namespace oslo.policy \
                --namespace oslo.service.service \
                --namespace oslo.service.periodic_task \
                --namespace oslo.service.sslutil \
		--wrap-width 140 \
		--output-file $(CURDIR)/debian/heat-common/usr/share/heat-common/heat.conf
	pkgos-readd-keystone-authtoken-missing-options $(CURDIR)/debian/heat-common/usr/share/heat-common/heat.conf keystone_authtoken heat

	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslopolicy-sample-generator \
		--output $(CURDIR)/debian/heat-common/usr/share/heat-common/policy.json \
		--format json \
		--namespace heat

	sed -i 's|^[# \t]*connection[\t #]*=.*|connection=sqlite:////var/lib/heat/heatdb|' $(CURDIR)/debian/heat-common/usr/share/heat-common/heat.conf
	# Fixup a few default for the [trustee] config
	pkgos-fix-config-default $(CURDIR)/debian/heat-common/usr/share/heat-common/heat.conf trustee auth_type password
	pkgos-fix-config-default $(CURDIR)/debian/heat-common/usr/share/heat-common/heat.conf trustee user_domain_name default

	dh_install
	dh_missing -Xbin/cinder-keystone-setup --fail-missing

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	PYTHONPATH=. PYTHON=python3 python3 -m sphinx -b html doc/source $(CURDIR)/debian/heat-doc/usr/share/doc/heat-doc/html
	dh_sphinxdoc
endif

override_dh_python3:
	dh_python3 --shebang=/usr/bin/python3
#	sed -i 's|usr/bin/env python|usr/bin/env python3|' $(CURDIR)/debian/python3-heat/usr/lib/python3/dist-packages/heat_integrationtests/install-requirements
