#!/bin/sh

set -e

ETC=/etc/cloudkitty
CONF=${ETC}/cloudkitty.conf

#PKGOS-INCLUDE#

if [ "$1" = "configure" ] || [ "$1" = "reconfigure" ] ; then
	. /usr/share/debconf/confmodule
	. /usr/share/dbconfig-common/dpkg/postinst

	pkgos_var_user_group cloudkitty

	# Make sure we have a /etc/cloudkitty folder with config files in it.
	pkgos_write_new_conf cloudkitty cloudkitty.conf
	pkgos_write_new_conf cloudkitty api_paste.ini
	if [ -r /etc/cloudkitty/policy.json ] ; then
		mv /etc/cloudkitty/policy.json /etc/cloudkitty/disabled.policy.json.old
	fi
	pkgos_write_new_conf cloudkitty metrics.yml


	pkgos_rabbit_write_conf ${CONF} oslo_messaging_rabbit cloudkitty
	pkgos_write_admin_creds ${CONF} keystone_authtoken cloudkitty

	# Do the db creation using dbconfig-common
	db_get cloudkitty/configure_db
	if [ "$RET" = "true" ] ; then
		pkgos_dbc_postinst ${CONF} database connection cloudkitty $@
		echo "Now doing cloudkitty-dbsync: this may take a while..."
		su cloudkitty -s /bin/sh -c "cloudkitty-storage-init" || true
		su cloudkitty -s /bin/sh -c "cloudkitty-dbsync upgrade" || true
	fi

	db_stop
fi

#DEBHELPER#

exit 0
