#!/bin/sh
set -e

alias stripwhitespace="sed -e 's/^[[:blank:]]*//' -e 's/[[:blank:]]*$//'"

installeddebconfversion="$(dpkg -s debconf | sed -ne '/^Version/s/^Version: //p')"
if [ "reconfigure" != "$1" ] && \
	dpkg --compare-versions "${installeddebconfversion}" "le" "1.4" && \
	[ "$exim4postinstisrunning" != "true" ] ; then
	echo "exim4-config.postinst: [WARN] Installed debconf version is broken. Aborting preconfigure." 1>&2
	exit 0
fi

. /usr/share/debconf/confmodule

if [ -n "$EX4DEBUG" ]; then
  echo "now debugging $0 $@"
  set -x
fi

UE4CC="/etc/exim4/update-exim4.conf.conf"

convert_to_long ()
{
	case "$1" in
	internet)
		echo -n "internet site; mail is sent and received directly using SMTP"
		;;
	smarthost)
		echo -n "mail sent by smarthost; received via SMTP or fetchmail"
		;;
	satellite)
		echo -n "mail sent by smarthost; no local mail"
		;;
	local)
		echo -n "local delivery only; not on a network"
		;;
	none)
		echo -n "no configuration at this time"
		;;
	esac
}
convert_to_short ()
{
	case "$1" in
	"internet site; mail is sent and received directly using SMTP")
		echo -n "internet"
		;;
	"mail sent by smarthost; received via SMTP or fetchmail")
		echo -n	"smarthost"
		;;
	"mail sent by smarthost; no local mail")
		echo -n	"satellite"
		;;
	"local delivery only; not on a network")
		echo -n	"local"
		;;
	"no configuration at this time")
		echo -n	"none"
		;;
	esac
}

convert_transport_to_long ()
{
	case "$1" in
	maildir_home)
		echo -n "Maildir format in home directory"
		;;
	mail_spool)
		echo -n "mbox format in /var/mail/"
		;;
	*)
		echo -n "locally customized"
		;;
	esac
}
convert_transport_to_short ()
{
	case "$1" in
	"Maildir format in home directory")
		echo -n "maildir_home"
		;;
	"mbox format in /var/mail/")
		echo -n "mail_spool"
		;;
	*)
		echo -n "custom"
		;;
	esac
}

# store environment variables in debconf db.
storevar2db() {
	dc_eximconfig_configtype="$(printf '%s\n' "${dc_eximconfig_configtype}" | stripwhitespace)"
	dc_local_interfaces="$(printf '%s\n' "${dc_local_interfaces}" | stripwhitespace)"
	dc_other_hostnames="$(printf '%s\n' "${dc_other_hostnames}" | stripwhitespace)"
	dc_readhost="$(printf '%s\n' "${dc_readhost}" | stripwhitespace)"
	dc_relay_domains="$(printf '%s\n' "${dc_relay_domains}" | stripwhitespace)"
	dc_relay_nets="$(printf '%s\n' "${dc_relay_nets}" | stripwhitespace)"
	dc_smarthost="$(printf '%s\n' "${dc_smarthost}" | stripwhitespace)"
	dc_minimaldns="$(printf '%s\n' "${dc_minimaldns}" | stripwhitespace)"
	dc_use_split_config="$(printf '%s\n' "${dc_use_split_config}" | stripwhitespace)"
	dc_hide_mailname="$(printf '%s\n' "${dc_hide_mailname}" | stripwhitespace)"
	# store externally changed values to debconf.db
	db_set exim4/dc_eximconfig_configtype \
		"$(convert_to_long ${dc_eximconfig_configtype})"
	db_set exim4/dc_local_interfaces "${dc_local_interfaces}"
	db_set exim4/dc_other_hostnames "${dc_other_hostnames}"
	db_set exim4/dc_readhost "${dc_readhost}"
	db_set exim4/dc_relay_domains "${dc_relay_domains}"
	db_set exim4/dc_relay_nets "${dc_relay_nets}"
	db_set exim4/dc_smarthost "${dc_smarthost}"
	if [ "${dc_minimaldns}" != "" ]; then
		db_set exim4/dc_minimaldns "${dc_minimaldns}"
	fi
	db_set exim4/use_split_config "${dc_use_split_config}"
	db_set exim4/hide_mailname "${dc_hide_mailname}"
	# do not store "locally customized" in debconf db, otherwise simply
	# removing the custom dc_localdelivery from ue4cc won't work.
	if [ "${dc_localdelivery}" != "" ] &&
		[ "$(convert_transport_to_long ${dc_localdelivery})" != "locally customized" ] ; then
		db_set exim4/dc_localdelivery \
			"$(convert_transport_to_long ${dc_localdelivery})"
	fi
	db_fset "exim4/dc_other_hostnames" mailname "${dc_mailname_in_oh}"
}

# ipv6: replace double colons in colon-separated host lists with umlaut-o
# replace ':' with ' : ', add leading and ending whitespace.
# return empty string if custom delimiter was used:
	#    local_domains = <; 172.16.0.0/12; 3ffe:ffff:836f::/48
# e.g: 'localhost : 172.16.0.1 :3ffe::ffff::836f::::: foo.org'
#       ===> ' localhost : 172.16.0.1 : 3ffeöffffö836föö : foo.org '
alias coloncolon2oe="env -u LC_ALL LC_CTYPE=C sed -e 's/[[:blank:]]*//g' -e '/^</d' -e 's/:::::/:öö/g' -e 's/::::/öö/g' -e 's/:::/:ö/g' -e 's/::/ö/g' -e 's/:/ : /g' -e 's/^/ /' -e 's/$/ /'"

# try to parse exim3 configuration file - works only if it was generated
# with eximconfig.
parseexim3() {
	dc_mailname="$(head -n 1 /etc/mailname | stripwhitespace)" || true

	# get the .... entries from
	# host_accept_relay = 127.0.0.1 : ::::1 : ......
	# if they exist, ie. parse list, and remove "127.0.0.1", "::::1"
	# and "localhost" from it.
	#
	dc_relay_nets="$(cat $1 | grep -h '^host_accept_relay[[:blank:]]*=' | sed -e 's/^host_accept_relay[[:blank:]]*=//' | coloncolon2oe | env -u LC_ALL LC_CTYPE=C sed -e 's/ 127\.0\.0\\.1 //g' -e 's/ localhost //g' -e 's/ öö1//g' -e 's/[[:blank:]]*//g' -e 's/::/:/g' -e 's/^://' -e 's/:$//' -e 's/ö/::/g')"

	# 'local_domains = $colonhostnames', including mailname and "localhost"
	#
	dc_other_hostnames="$(cat $1 | grep -h '^local_domains[[:blank:]]*=' | sed -e 's/^local_domains[[:blank:]]*=//' | coloncolon2oe | env -u LC_ALL LC_CTYPE=C sed -e 's/ localhost //g' -e "s/ ${dc_mailname} //g" -e 's/[[:blank:]]*//g' -e 's/::/:/g' -e 's/^://' -e 's/:$//' -e 's/ö/::/g')"

	# relay_domains = some.domain
	dc_relay_domains="$(cat $1 | grep -h '^relay_domains = ' | sed -e 's/^relay_domains[[:blank:]]*=[[:blank:]]*//' -e 's/[[:blank:]]*$//')"

	# lookuphost router exist ---> dc_eximconfig_configtype=internet
	#
	if cat $1 | grep -h -A2 '^lookuphost:' | \
		grep -h -A1 '[[:blank:]]*driver = lookuphost' | \
		grep -h -q '[[:blank:]]*transport = remote_smtp' ; then
		dc_eximconfig_configtype=internet
	else
		# smart:-director exists ---> dc_eximconfig_configtype=satellite
		#
		# Later we need the new_address directive to find dc_readhost.
		# ' || true' is required for "set -e"
		dc_readhost="$(cat $1 | grep -h -A2 '^smart:' | grep -h -A1 '^[[:blank:]]*driver = smartuser' | grep -h '^[[:blank:]]*new_address' || true)"

		# smarthost router exists --> dc_eximconfig_configtype is satellite or smarthost
		#
		# we need the route_list directive to find dc_smarthost
		# ' || true' is required for "set -e"
		dc_smarthost="$(< $1 grep -A3 '^smarthost:' | grep -A2 '[[:blank:]]*driver = domainlist' | grep -A1 '^[[:blank:]]*transport = remote_smtp' | grep '^[[:blank:]]*route_list = ".*"' || true)"

		if [ ! -z "${dc_readhost}" ] ; then
			dc_eximconfig_configtype=satellite
		elif [ ! -z "${dc_smarthost}" ] ; then
			dc_eximconfig_configtype=smarthost
		elif ! cat "$1" | grep -h -q '^remote_smtp:'; then
			# dc_eximconfig_configtype=local has no remote_smtp transport.
			dc_eximconfig_configtype=local
		else
			# handcrafted config. We probably misparsed, reset values
			# and exit.
			dc_eximconfig_configtype=''
			dc_local_interfaces='notset'
			dc_other_hostnames=''
			dc_readhost=''
			dc_relay_domains=''
			dc_relay_nets=''
			dc_smarthost=''
			dc_hide_mailname=''
			return 1
		fi
	fi

	case ${dc_eximconfig_configtype} in
		internet|local)
			#paranoia
			dc_readhost=''
			dc_smarthost=''
			dc_hide_mailname='false'
			;;
		satellite)
			#  new_address = ${local_part}@$readhost
			dc_readhost="$(printf '%s\n' "${dc_readhost}" | sed -e 's/^.*@//' -e 's/[[:blank:]]*$//')"
			#  route_list = "* $smtphost bydns_a"
			dc_smarthost="$(printf '%s\n' "${dc_smarthost}" | \
				sed -e 's/^ *route_list = "\* //' -e 's/ bydns_a"//' -e 's/[[:blank:]]*$//')"
			dc_hide_mailname='true'
			dc_relay_domains=''
			;;
		smarthost)
			#  route_list = "* $smtphost bydns_a"
			dc_smarthost="$(printf '%s\n' "${dc_smarthost}" | \
				sed -e 's/^ *route_list = "\* //' -e 's/ bydns_a"//' -e 's/[[:blank:]]*$//')"
			dc_hide_mailname='false'
			dc_relay_domains=''
			;;
	esac
}

##########
mailname2otherhostnames() {
	# u-ex4.conf no longer includes mailname in local domains,
	# insert it once into other_hostnames.
	# The debconf script might run twice, we need to store the result
	# in a file to pass it to postinst..
	[ -d /var/lib/exim4 ] || \
		install -d -oroot -groot -m755 /var/lib/exim4
	dc_mailname="$(head -n 1 /etc/mailname | stripwhitespace)" || true
	if [ -z "${dc_other_hostnames}" ] ; then
		dc_other_hostnames="${dc_mailname}"
	else
		dc_other_hostnames="$(printf '%s\n' "${dc_other_hostnames}" | stripwhitespace)":${dc_mailname}
	fi
	printf '%s\n' dc_other_hostnames="'""${dc_other_hostnames}""'" > \
		/var/lib/exim4/addmailname2oh
	db_fset "exim4/dc_other_hostnames" mailname true
	dc_mailname_in_oh="true"
}
mailname2otherhostnamesdcloop() {
# append mailname to other_hostnames, once. on fresh installations.
	if [ "${dc_mailname_in_oh}" != "true" ] ;then
		db_get exim4/mailname
		dc_mailname="$(printf '%s\n' "$RET"| stripwhitespace)"
		db_get "exim4/dc_other_hostnames"
		dc_other_hostnames="$(printf '%s\n' "$RET"| stripwhitespace)"
		if [ -z "${dc_other_hostnames}" ] ; then
			dc_other_hostnames="${dc_mailname}"
		else
			dc_other_hostnames="${dc_other_hostnames}:${dc_mailname}"
		fi
		db_set exim4/dc_other_hostnames "${dc_other_hostnames}"
		db_fset "exim4/dc_other_hostnames" mailname true
		dc_mailname_in_oh="true"
	fi
}
##########


# set to dummy value, so we can differ between unset vs (seen or set outside debconf)
dc_local_interfaces=notset

db_settitle exim4/exim4-config-title 
db_get exim4/dc_eximconfig_configtype
dc_eximconfig_configtype="$(convert_to_short "$RET")"
db_get exim4/use_split_config
dc_use_split_config="$RET"
# Did we append mailname to other_hostnames yet?
db_fget "exim4/dc_other_hostnames" mailname || true
dc_mailname_in_oh="$RET"

if [ -e $UE4CC ] ; then
	. $UE4CC || true
	# on upgrades _once_ add mailname to other_hostnames
	if [ -n "$2" ] && [ "$1" = "configure" ] && \
		[ "${dc_mailname_in_oh}" != "true" ] ; then
			mailname2otherhostnames
	fi
	# set defaults using these values
	storevar2db
else
	# If there are no debconf answers (running first time) and we are
	# making a cross upgrade from exim3, try to parse its config file
	# to seed debconf db.
	if [ "${dc_eximconfig_configtype}" = "" ] && [ -r /etc/exim/exim.conf ] ; then
		# parse old configfile
		#
		# first do unfolding of lines continued with backslash, by:
		# 1. remove comment lines
		# 2. remove blank lines
		# 3. remove "\<optional space><newline><optional space>"
		exim3confunfolded="$(tempfile -p EX3)"
		perl -e \
		'undef $/;
		while (<>) {
			s/^[[:space:]]*#[^\n]*\n//mg;
			s/^\n//mg;
			s/\\[[:space:]]*\n[[:space:]]*//g;
		} continue {
			print or die "blah";
		}' < /etc/exim/exim.conf > "$exim3confunfolded"
		if parseexim3 "$exim3confunfolded" ; then
			# set defaults using these values
			storevar2db
		fi
		rm -f "$exim3confunfolded"
	fi
fi

## set up default values, we cannot do this in templates file because
## config script is called two times before update-exim4.conf.conf exists.
[ "${dc_eximconfig_configtype}" = "" ] && \
	dc_eximconfig_configtype="local" && \
	db_set exim4/dc_eximconfig_configtype "$(convert_to_long ${dc_eximconfig_configtype})"


if [ -e /etc/mailname ] ; then
	dc_mailname="$(head -n 1 /etc/mailname | stripwhitespace)" || true
	# store values
	db_set exim4/mailname "${dc_mailname}"
else
	db_get exim4/mailname
	dc_mailname="$RET"
fi

#Set default mailname
if [ "${dc_mailname}" = "" ] ; then
	dc_mailname="$(hostname --fqdn 2>/dev/null)" || dc_mailname="$(hostname)" && \
	dc_mailname="$(printf '%s\n' "${dc_mailname}" | stripwhitespace)" && \
	db_set exim4/mailname "${dc_mailname}"
fi
fqdn="$(hostname --fqdn 2>/dev/null)" || fqdn=''
db_subst exim4/dc_other_hostnames fqdn "$fqdn"

# initialize env-vars from debconf_db, if they haven't been set yet either by
# parseexim3() or by sourcing $UE4CC
if [ "${dc_readhost}" = "" ] ; then
	db_get exim4/dc_readhost
	dc_readhost="$RET"
fi
if [ "${dc_smarthost}" = "" ] ; then
	db_get exim4/dc_smarthost
	dc_smarthost="$RET"
fi
# Tricky! An empty value for this option is significant, therefore both
# envvar and debconf-value default to "nonset"  instead of "", the
# following line will only change then envvars value if the config-script
# runs the second time without existing $UE4CC or
# if the debconf-db has been preseeded by other means.
if [ "${dc_local_interfaces}" = "notset" ]; then
	db_get exim4/dc_local_interfaces
	dc_local_interfaces="$RET"
fi
if [ "${dc_minimaldns}" = "" ] ; then
	db_get exim4/dc_minimaldns
	dc_minimaldns="$RET"
fi

if [ "${dc_use_split_config}" = "" ] ; then
	if [ "${2}" = "" ] ; then
		# fresh installation
		db_set exim4/use_split_config false
		dc_use_split_config="false"
	else
		db_set exim4/use_split_config true
		dc_use_split_config="true"
	fi
fi

if [ "${dc_localdelivery}" = "" ] ; then
	db_get exim4/dc_localdelivery
	dc_localdelivery="$(convert_transport_to_short "$RET")"
elif [ "$(convert_transport_to_long ${dc_localdelivery})" = "locally customized" ] ; then
	dc_localdelivery="custom"
fi

db_version 2.0

db_capb backup
# initial state
STATE=1
# state to continue for all values
CONTSTATE=50
# last valid state
STATELIMIT=51
EX4QUESTION=""
while [ "$STATE" != 0 ] && [ "$STATE" -le "$STATELIMIT" ]; do
	case "$STATE" in
		1)
		# this is now empty
		EX4QUESTION=""
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		;;

		2)
		EX4QUESTION="exim4/dc_eximconfig_configtype"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		CONFIGTYPEQUESTION=2
		db_input medium "$EX4QUESTION" || true
		;;
		3)
		EX4QUESTION=""
		db_get exim4/dc_eximconfig_configtype
		dc_eximconfig_configtype="$(convert_to_short "$RET")"
		BACKSTATE=$(($STATE - 1))
		case "${dc_eximconfig_configtype}" in
			none)
			# dont ask more questions
			#FORWSTATE=$(($STATELIMIT + 1))
			FORWSTATE=4
			;;
			internet)
			FORWSTATE=8
			;;
			smarthost)
			FORWSTATE=20
			;;
			satellite)
			FORWSTATE=30
			;;
			local)
			FORWSTATE=40
			;;
			*)
			# Should not happen. Break loop
			FORWSTATE=$(($STATELIMIT + 1))
			;;
		esac
		;;

		4)
		# show additional info for type=none
		EX4QUESTION="exim4/no_config"
		BACKSTATE=$CONFIGTYPEQUESTION
		FORWSTATE=$(($STATE + 1))
		# same priority as configtype because of danger of loop?
		db_input medium "$EX4QUESTION" || true
		;;
		5)
		EX4QUESTION=""
		BACKSTATE=$CONFIGTYPEQUESTION
		db_get exim4/no_config || true
		if [ "$RET" = "false" ]; then
			FORWSTATE=$CONFIGTYPEQUESTION
		else
			#break loop
			FORWSTATE=$CONTSTATE
		fi
		;;

		8)
		# internet site
		EX4QUESTION="exim4/mailname"
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$CONFIGTYPEQUESTION
		FORWSTATE=$(($STATE + 1))
		;;
		9)
		EX4QUESTION="exim4/dc_local_interfaces"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		if [ "${dc_local_interfaces}" = "notset" ] ; then
			dc_local_interfaces=''
			db_set exim4/dc_local_interfaces ''
		fi
		db_input medium "$EX4QUESTION" || true
		;;
		10)
		EX4QUESTION="exim4/dc_other_hostnames"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		mailname2otherhostnamesdcloop
		db_input medium "$EX4QUESTION" || true
		;;
		11)
		EX4QUESTION="exim4/dc_relay_domains"
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		;;
		12)
		EX4QUESTION="exim4/dc_relay_nets"
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		;;
		13)
		EX4QUESTION="exim4/dc_minimaldns"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_input low "$EX4QUESTION" || true
		;;
		14)
		EX4QUESTION="exim4/dc_localdelivery"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$CONTSTATE
		# do not ask question if user has set a different transport 
		# than maildir or mailspool
		if [ "${dc_localdelivery}" != "custom" ] ; then
			db_input low "$EX4QUESTION" || true
		fi
		;;

		
		20)
		# internet site with smarthost
		EX4QUESTION="exim4/mailname"
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$CONFIGTYPEQUESTION
		FORWSTATE=$(($STATE + 1))
		;;
		21)
		EX4QUESTION="exim4/dc_local_interfaces"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		if [ "${dc_local_interfaces}" = "notset" ] ; then
			dc_local_interfaces='127.0.0.1 ; ::1'
			db_set exim4/dc_local_interfaces '127.0.0.1 ; ::1'
		fi
		db_input medium "$EX4QUESTION" || true
		;;
		22)
		EX4QUESTION="exim4/dc_other_hostnames"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		mailname2otherhostnamesdcloop
		db_input medium "$EX4QUESTION" || true
		;;
		23)
		EX4QUESTION="exim4/dc_relay_domains"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_get "$EX4QUESTION" || true
		dc_relay_domains="$RET"
		# Only ask for relay domains if non-empty, since this only
		# really works for internet site hosts. (See comment near
		# smarthost router definition in configuration file.)
		if [ "${dc_relay_domains}" != "" ] ; then
			db_input medium "$EX4QUESTION" || true
		fi
		;;
		24)
		EX4QUESTION="exim4/dc_relay_nets"
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		;;
		25)
		EX4QUESTION="exim4/dc_smarthost"
		if [ "${dc_smarthost}" = "" ] ; then
			# default to mail.mailname
			dc_smarthost="mail.${dc_mailname}"
			db_set exim4/dc_smarthost "${dc_smarthost}"
		fi
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		;;
		26)
		EX4QUESTION="exim4/hide_mailname"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_get exim4/mailname || true
		dc_mailname="$RET"
		db_get exim4/dc_other_hostnames || true
		dc_other_hostnames="$RET"
		db_subst exim4/hide_mailname mailname ${dc_mailname}
		db_subst exim4/hide_mailname dc_other_hostnames \
			${dc_other_hostnames}
               db_get $EX4QUESTION || true
               dc_hide_mailname="$RET"
		if [ "${dc_hide_mailname}" = "" ] ; then
			dc_hide_mailname='false'
			db_set exim4/hide_mailname "${dc_hide_mailname}"
		fi
		db_input medium "$EX4QUESTION" || true
		;;
		27)
		EX4QUESTION="exim4/dc_readhost"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_get exim4/hide_mailname
		dc_hide_mailname="$RET"
		if [ "${dc_hide_mailname}" = "true" ];then
			if [ "${dc_readhost}" = "" ] ; then
				# default to mailname
				dc_readhost="${dc_mailname}"
				db_set exim4/dc_readhost "${dc_readhost}"
			fi
			db_input medium "$EX4QUESTION" || true
		else
			EX4QUESTION=""
		fi
		;;
		28)
		EX4QUESTION="exim4/dc_minimaldns"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_input low "$EX4QUESTION" || true
		;;
		29)
		EX4QUESTION="exim4/dc_localdelivery"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$CONTSTATE
		# do not ask question if user has set a different transport 
		# than maildir or mailspool
		if [ "${dc_localdelivery}" != "custom" ] ; then
			db_input low "$EX4QUESTION" || true
		fi
		;;
		
		30)
		# satellite
		EX4QUESTION="exim4/mailname"
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$CONFIGTYPEQUESTION
		FORWSTATE=$(($STATE + 1))
		;;
		31)
		EX4QUESTION="exim4/dc_local_interfaces"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		if [ "${dc_local_interfaces}" = "notset" ] ; then
			dc_local_interfaces='127.0.0.1 ; ::1'
			db_set exim4/dc_local_interfaces '127.0.0.1 ; ::1'
		fi
		db_input medium "$EX4QUESTION" || true
		;;
		32)
		EX4QUESTION="exim4/dc_other_hostnames"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		mailname2otherhostnamesdcloop
		db_input medium "$EX4QUESTION" || true
		;;
		33)
		EX4QUESTION="exim4/dc_readhost"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		# satellite requires a mail-hub, no use asking whether there
		# is one.
		db_set exim4/hide_mailname "true"
		dc_hide_mailname="true"
		if [ "${dc_readhost}" = "" ] ; then
			# default to domain of mailname
			dc_readhost="${dc_mailname}"
			db_set exim4/dc_readhost "${dc_readhost}"
		fi
		db_input medium "$EX4QUESTION" || true
		;;
		34)
		EX4QUESTION="exim4/dc_smarthost"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_get exim4/dc_readhost
		dc_readhost="$RET"
		if [ "${dc_smarthost}" = "" ] ; then
			# default to read_host
			dc_smarthost="${dc_readhost}"
			db_set exim4/dc_smarthost "${dc_smarthost}"
		fi
		db_input medium "$EX4QUESTION" || true
		;;
		35)
		EX4QUESTION="exim4/dc_relay_domains"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_get "$EX4QUESTION" || true
		dc_relay_domains="$RET"
		# Only ask for relay domains if non-empty, since this only
		# really works for internet site hosts. (See comment near
		# smarthost router definition in configuration file.)
		if [ "${dc_relay_domains}" != "" ] ; then
			db_input medium "$EX4QUESTION" || true
		fi
		;;
		36)
		EX4QUESTION=""
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		# satellite-system! Only ask for relay_nets if they are
		# nonempty - we cannot distinguish whether they were set outside debconf
		# or whether the user switched dc_eximconfig_configtype
		db_get exim4/dc_relay_nets || true
		dc_relay_nets="$RET"
		if [ "${dc_relay_nets}" != "" ] ; then
			db_input medium exim4/dc_relay_nets || true
			FORWSTATE=$(($STATE + 1))
		fi
		;;	
		37)
		EX4QUESTION="exim4/dc_minimaldns"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_input low "$EX4QUESTION" || true
		;;
		38)
		EX4QUESTION="exim4/dc_localdelivery"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$CONTSTATE
		# Only ask question if it is set to a value that is both supported
		# by debconf (maildir_home and mail_spool) and is not set to the
		# default value (maildir_home) since this is only used for
		# real-*.
		if [ "${dc_localdelivery}" = "maildir_home" ] ; then
			db_input low "$EX4QUESTION" || true
		fi
		;;
		
		40)
		# local mail only
		EX4QUESTION="exim4/mailname"
		db_input medium "$EX4QUESTION" || true
		BACKSTATE=$CONFIGTYPEQUESTION
		FORWSTATE=$(($STATE + 1))
		;;
		41)
		EX4QUESTION=""
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		if [ "${dc_local_interfaces}" = "notset" ] ; then
			dc_local_interfaces='127.0.0.1 ; ::1'
			db_set exim4/dc_local_interfaces '127.0.0.1 ; ::1'
		fi
		db_input medium exim4/dc_local_interfaces || true
		;;

		42)
		EX4QUESTION="exim4/dc_other_hostnames"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		mailname2otherhostnamesdcloop
		db_input medium "$EX4QUESTION" || true
		;;
		
		43)
		EX4QUESTION=""
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		# local mail only -system! Only ask for relay_nets/relay_domains if they are
		# nonempty - we cannot distinguish whether they were set outside debconf
		# or whether the user switched dc_eximconfig_configtype
		db_get exim4/dc_relay_nets || true
		dc_relay_nets="$RET"
		db_get exim4/dc_relay_domains || true
		dc_relay_domains="$RET"
		
		if [ "${dc_relay_nets}" != "" ] ; then
			db_input medium exim4/dc_relay_nets || true
		fi
		if [ "${dc_relay_domains}" != "" ] ; then
			db_input medium exim4/dc_relay_domains || true
		fi
		;;	
		44)
		EX4QUESTION="exim4/dc_minimaldns"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$(($STATE + 1))
		db_input low "$EX4QUESTION" || true
		;;
		45)
		EX4QUESTION="exim4/dc_localdelivery"
		BACKSTATE=$(($STATE - 1))
		FORWSTATE=$CONTSTATE
		# do not ask question if user has set a different transport 
		# than maildir or mailspool
		if [ "${dc_localdelivery}" != "custom" ] ; then
			db_input low "$EX4QUESTION" || true
		fi
		;;
		50)
		EX4QUESTION="exim4/use_split_config"
		BACKSTATE=$PREVSTATE
		FORWSTATE=$(($STATELIMIT + 1))
		db_input medium "$EX4QUESTION" || true
		;;
	esac
	PREVSTATE=$STATE
	if db_go; then
		STATE=$FORWSTATE
	else
		STATE=$BACKSTATE
	fi
done
if  [ "$STATE" = 0 ] ; then
	# User pressed back on the first question, go back to previous
	# package if run by base-config 2.0. #222773.
	exit 30
fi

# make sure dc_local_interfaces is set to sane value
if [ "${dc_local_interfaces}" = "notset" ] ; then
	dc_local_interfaces=''
	db_set exim4/dc_local_interfaces ''
fi

if [ ! -e /etc/aliases ] || \
	! grep -q '^root:[[:space:]]*[[:alnum:]]' /etc/aliases ; then
	db_get exim4/dc_postmaster
	if [ -z "$RET" ] && db_get passwd/username; then
		db_set exim4/dc_postmaster "${RET}"
	fi
	db_input medium exim4/dc_postmaster || true
	db_go || true
fi

db_get exim4/dc_postmaster || true
dc_postmaster="$(printf '%s\n' "$RET" | stripwhitespace)"

# this has been commented out for a long time, remove by the end of 2007
# if [ "${dc_eximconfig_configtype}" = "satellite" ] || [ "${dc_eximconfig_configtype}" = "local" ] ; then
# 	# reset dc_relay_domains dc_relay_nets and dc_other_hostnames
# 	db_set exim4/dc_relay_domains ""
# 	db_set exim4/dc_relay_nets ""
# 	db_set exim4/dc_other_hostnames ""
# fi
