#!/bin/sh

set -e

. /usr/share/debconf/confmodule

db_input medium esmtp/overwriteconfig || true
db_go

db_get esmtp/overwriteconfig || true
OverwriteConfig="${RET:-true}"

if test ! -s /etc/esmtprc -o "$OverwriteConfig" = "true"
then
	db_get esmtp/hostname || true
	HostName="$RET"
	
	if ! test "$HostName"
	then
		if test -s /etc/mailname
		then
			db_set esmtp/hostname `cat /etc/mailname`
		else
			db_set esmtp/hostname `hostname --fqdn`
		fi
	fi
	
	db_input medium esmtp/hostname || true
	db_go

	db_input low esmtp/hostport || true
	db_go

	db_input medium esmtp/username || true
	db_go

	db_get esmtp/username || true
	UserName="$RET"

	if test "$UserName"
	then
		db_input medium esmtp/password || true
		db_go
	fi

	db_input low esmtp/starttls || true
	db_go

	db_get esmtp/starttls || true
	StartTLS="$RET"

	if test "$StartTLS" != "disabled"
	then
		db_input low esmtp/certificate_passphrase || true
		db_go
	fi

	db_get esmtp/mda || true
	MDA="$RET"
	
	if test -z "$MDA" -a -x /usr/bin/procmail
	then
		MDA=procmail
	fi
	
	if test -z "$MDA" -a -x /usr/bin/maildrop
	then
		MDA=maildrop
	fi
	
	if test -z "$MDA"
	then
		MDA=none
	fi
	
	db_set esmtp/mda "$MDA"
		
	db_input low esmtp/mda || true
	db_go
fi

# Program End
exit 0
