#!/bin/bash
set -e

debconf-set-selections <<< "postfix postfix/mailname string localhost" 2>&1
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'" 2>&1

hostname localhost
apt-get install -y postfix 2>&1
hostname --fqdn > /etc/mailname
postconf -e maillog_file=/var/log/postfix.log

rc=0

service postfix restart 2>&1 || {
  systemctl status postfix.service
  rc=1
}

postfix check 2>&1 || rc=$?
postfix set-permissions 2>&1 || rc=$?

python3 `dirname $0`/test-postfix.py 2>&1 || rc=$?

echo "===== postfix.log contents:"
cat /var/log/postfix.log 2>&1 || :

exit $rc
