#!/usr/bin/make -f

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

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

override_dh_auto_clean:
	dh_auto_clean
	rm -rf .stestr build *.egg-info
	rm -f $(CURDIR)/debian/aetos.postinst debian/aetos.service debian/aetos.init debian/aetos.config
	find $(CURDIR) -name '*.pyc' -delete
	for i in $$(find . -type d -iname __pycache__) ; do rm -rf $$i ; done
	rm -f AUTHORS ChangeLog


override_dh_auto_build:
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func aetos.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func aetos.config
	pkgos-merge-templates aetos aetos endpoint ksat

override_dh_auto_test:
	echo "Do nothing."

override_dh_auto_install:
	echo "Do nothing."

override_dh_install:
	pkgos-dh_auto_install --no-py2 --in-tmp
	# Remove files form /usr/etc.
	rm -rf $(CURDIR)/debian/tmp/usr/etc
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	pkgos-dh_auto_test 'aetos\.tests\.unit\..*'
endif

	mkdir -p $(CURDIR)/debian/aetos/usr/share/aetos
	PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages oslo-config-generator \
		--output-file $(CURDIR)/debian/aetos/usr/share/aetos/aetos.conf \
		--wrap-width 140 \
		--namespace aetos \
		--namespace oslo.log \
		--namespace oslo.middleware.cors \
		--namespace oslo.middleware.healthcheck \
		--namespace oslo.middleware.http_proxy_to_wsgi \
		--namespace oslo.policy \
		--namespace keystonemiddleware.auth_token
	pkgos-fix-config-default $(CURDIR)/debian/aetos/usr/share/aetos/aetos.conf DEFAULT log_dir /var/log/aetos
	pkgos-fix-config-default $(CURDIR)/debian/aetos/usr/share/aetos/aetos.conf DEFAULT log_file aetos.log

	pkgos-fix-config-default $(CURDIR)/debian/aetos/usr/share/aetos/aetos.conf DEFAULT auth_strategy keystone

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

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

	dh_install
	ln -s /etc/aetos/api-paste.ini $(CURDIR)/debian/aetos/usr/lib/python3/dist-packages/aetos/api-paste.ini

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

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