#!/usr/bin/make -f

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

export OSLO_PACKAGE_VERSION=$(VERSION)

%:
	dh $@  --with python2,systemd

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	PYTHONPATH=$(CURDIR) bash run_tests.sh -N -P
endif

override_dh_clean:
	dh_clean
	rm -f debian/cinder-common.config debian/cinder-common.postinst debian/cinder-api.config debian/cinder-api.postinst debian/cinder-volume.postinst
	rm -rf debian/*.upstart debian/*.service

override_dh_install:
	#tools/config/generate_sample.sh -b . -p cinder -o etc/cinder
	dh_install --fail-missing -Xbin/cinder-all
	install -D -m 0440 debian/cinder-common.sudoers $(CURDIR)/debian/cinder-common/etc/sudoers.d/cinder-common

	oslo-config-generator --output-file $(CURDIR)/etc/cinder/cinder.conf.sample \
		--wrap-width 140 \
		--namespace cinder \
		--namespace oslo.concurrency \
		--namespace oslo.db \
		--namespace oslo.messaging \
		--namespace oslo.policy \
		--namespace keystonemiddleware.auth_token \
		--namespace cinder.openstack.common.eventlet_backdoor \
		--namespace cinder.openstack.common.log \
		--namespace cinder.openstack.common.periodic_task \
		--namespace cinder.openstack.common.policy \
		--namespace oslo_db.concurrency

	#install -D -m 0644 $(CURDIR)/debian/cinder.conf $(CURDIR)/debian/cinder-common/usr/share/cinder-common/cinder.conf
	install -D -m 0644 $(CURDIR)/etc/cinder/cinder.conf.sample $(CURDIR)/debian/cinder-common/usr/share/cinder-common/cinder.conf

	# Disable https in default setup, as this makes our CI fail
	sed -i 's/#auth_protocol=https/auth_protocol=http/' $(CURDIR)/debian/cinder-common/usr/share/cinder-common/cinder.conf
	install -D -m 0644 $(CURDIR)/etc/cinder/logging_sample.conf $(CURDIR)/debian/cinder-common/usr/share/cinder-common/logging.conf
	sed -i 's|#rootwrap_config=<None>|rootwrap_config=/etc/cinder/rootwrap.conf|' $(CURDIR)/debian/cinder-common/etc/sudoers.d/cinder-common
	echo "# Connection to db:" >>$(CURDIR)/debian/cinder-common/usr/share/cinder-common/cinder.conf
	echo "sql_connection = sqlite:////var/lib/cinder/cinderdb" >>$(CURDIR)/debian/cinder-common/usr/share/cinder-common/cinder.conf
	sed -i "s/#auth_strategy=noauth/auth_strategy=keystone/" $(CURDIR)/debian/cinder-common/usr/share/cinder-common/cinder.conf
	sed -i 's|#state_path=$pybasedir|state_path=/var/lib/cinder|' $(CURDIR)/debian/cinder-common/usr/share/cinder-common/cinder.conf

override_dh_fixperms:
	dh_fixperms -Xcinder_tgt.conf
	chmod 0440 $(CURDIR)/debian/cinder-common/etc/sudoers.d/cinder-common

override_dh_auto_build:
	dh_auto_build

	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cinder-common.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cinder-common.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cinder-api.config
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cinder-api.postinst
	/usr/share/openstack-pkg-tools/pkgos_insert_include pkgos_func cinder-volume.postinst
