#!/bin/sh -e

# Source debconf library.
. /usr/share/debconf/confmodule
db_version 2.0

# This conf script is capable of backing up
db_capb backup

ucf -p /etc/pybit/debian-db.pl
if [ -f /etc/pybit/debian-db.pl ]; then
  rm /etc/pybit/debian-db.pl
fi
# source debconf stuff
. /usr/share/debconf/confmodule
# source dbconfig-common stuff
. /usr/share/dbconfig-common/dpkg/config.pgsql

# create an output file which ucf will watch
# (and ensure isn't recreated) until we need to do it
dbc_generate_include=perl:/etc/pybit/debian-db.pl
# minimal permissions
dbc_generate_include_owner="root:root"
dbc_generate_include_perms="600"
# do the dbconfig stuff
dbc_go pybit-web $@
# now do our config
STATE=1
while [ "$STATE" != 0 -a "$STATE" != 3 ]; do
	case "$STATE" in
	1)
		db_input high pybit-web/rabbitmqhost || true
	;;
	2)
		# Check their answer.
		db_get pybit-web/rabbitmqhost
		if [ "$RET" = "" ]; then
			db_set pybit-web/rabbitmqhost localhost
			db_input high pybit-web/missinghost || true
			db_go
		fi
		db_input medium pybit-web/hostname || true
		db_input medium pybit-web/port || true
	;;
	esac
	if db_go; then
		STATE=$(($STATE + 1))
	else
		STATE=$(($STATE - 1))
	fi
done
