#!/bin/sh

CONFIGFILE=/etc/postsrsd.conf
set -e
. /usr/share/debconf/confmodule

if [ -e $CONFIGFILE ]; then
	DOMAIN=$(grep -Po '(?<=^domains = { ")[^"]+' $CONFIGFILE) || true
	db_set postsrsd/domain "$DOMAIN"
	priority="medium"
else
	# only set the default value if the current value is empty, especially
	# important since we can get called twice before the postinst writes
	# the config file.
	db_get postsrsd/domain
	DOMAIN="$RET"

	if [ -z "$DOMAIN" -a -e /usr/sbin/postconf ]; then
		db_set postsrsd/domain "$(postconf -h mydomain)"
		priority="medium"
	elif [ -z "$DOMAIN" ]; then
		db_set postsrsd/domain "$(hostname -d)"
		priority="high"
	fi
fi

db_input $priority postsrsd/domain || true
db_go
