#!/usr/bin/make -f

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

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

override_dh_auto_clean:
	rm -f etc/manila/manila.conf
	rm -f debian/*.service debian/*.upstart debian/*.init
	rm -f debian/manila-common.postinst debian/manila-common.config debian/manila-api.config debian/manila-api.postinst debian/manila-common.postrm
	rm -rf debian/manila-common.postrm debian/*.templates debian/po
	rm -rf build .stestr *.egg-info doc/source/_static
	find . -iname '*.pyc' -delete
	for i in $$(find . -type d -iname __pycache__) ; do rm -rf $$i ; done

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

override_dh_auto_install:
	echo "Do nothing..."

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

	rm -rf $(CURDIR)/debian/tmp/usr/etc

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	pkgos-dh_auto_test --no-py2 'manila\.tests\.((?!.*cmd\.test_manage\.ManilaCmdManageTestCase\.test_run_ipython.*|.*share\.drivers\.dell_emc.*|.*test_hacking\.HackingTestCase\.test_logging_with_tuple_argument.*|.*api\.v2\.test_share_groups\.ShareGroupAPITest\.test_share_group_force_delete_with_different_roles_1.*|.*api\.v2\.test_share_groups\.ShareGroupAPITest\.test_share_group_force_delete_with_different_roles_2.*|.*api\.v2\.test_share_network_subnets\.ShareNetworkSubnetControllerTest\.test_list_subnet.*|.*api\.v2\.test_share_network_subnets\.ShareNetworkSubnetControllerTest\.test_show_subnet.*|.*api\.v2\.test_share_network_subnets\.ShareNetworkSubnetControllerTest\.test_subnet_create.*|.*api\.views\.test_share_network_subnets\.ViewBuilderTestCase\.test_build_share_network_subnet.*|.*api\.views\.test_share_network_subnets\.ViewBuilderTestCase\.test_build_share_network_subnets.*|.*share\.drivers\.infinidat.*|.*share\.drivers\.netapp\.dataontap\.client\.test_client_cmode_rest\.NetAppRestCmodeClientTestCase\.test_remove_preferred_dcs.*))'
endif

	# Generate the config file
	mkdir -p $(CURDIR)/debian/manila-common/usr/share/manila-common
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--namespace manila \
		--namespace oslo.concurrency \
		--namespace oslo.db \
		--namespace oslo.log \
		--namespace oslo.messaging \
		--namespace oslo.policy \
		--namespace oslo.reports \
		--namespace oslo.service.periodic_task \
		--namespace oslo.service.service \
		--namespace oslo.service.sslutils \
		--namespace oslo.service.wsgi \
		--namespace keystonemiddleware.auth_token \
		--output-file $(CURDIR)/debian/manila-common/usr/share/manila-common/manila.conf
	pkgos-readd-keystone-authtoken-missing-options $(CURDIR)/debian/manila-common/usr/share/manila-common/manila.conf keystone_authtoken manila

	pkgos-fix-config-default $(CURDIR)/debian/manila-common/usr/share/manila-common/manila.conf keystone_authtoken auth_protocol http
	pkgos-fix-config-default $(CURDIR)/debian/manila-common/usr/share/manila-common/manila.conf oslo_concurrency lock_path /var/lock/manila

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

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


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

	# and copy the remaining
	set -e ; for i in api-paste.ini rootwrap.conf ; do \
		cp $(CURDIR)/etc/manila/$$i $(CURDIR)/debian/manila-common/usr/share/manila-common ; \
	done
	cp $(CURDIR)/etc/manila/logging_sample.conf $(CURDIR)/debian/manila-common/usr/share/manila-common/logging.conf
	cp -rfv $(CURDIR)/etc/manila/rootwrap.d $(CURDIR)/debian/manila-common/usr/share/manila-common

	install -D -m 0440 debian/manila-common.sudoers $(CURDIR)/debian/manila-common/etc/sudoers.d/manila-common

	dh_install
	dh_missing --fail-missing
	rm -rf $(CURDIR)/debian/manila-api/usr/lib

override_dh_auto_test:
	echo "Do nothing..."

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

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