#!/bin/sh -e
#
# Test if the post office is working.

. /usr/share/debian-edu-config/testsuite-lib.sh

if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

# Only Main-Server should implement mail server
if echo "$PROFILE" | grep -q Main-Server ; then
    enabled=true
fi

if [ true != "$enabled" ] ; then
    exit 0
fi

retval=0

#for port in smtp imap2 imaps; do
for port in smtp imaps; do
    netstat_check $port tcp "postoffice" || retval=1
done

# send a test-email to postmaster
# (only allowed recipient for sender w/o Kerberos ticket)
cat << EOF | /usr/lib/sendmail postmaster
Subject: Test-email sent by the debian-edu test-suite

Dear system administrator,

welcome to the mail-system. Basic email delivery seems to work!
If you did not receive this email, report the failure to us.

This email has been sent by the Debian Edu test-suite.

Regards,

    The Debian Edu Developers

EOF

tmpfile=$(tempfile)
smtpserver=postoffice.intern
if swaks --to postmaster@postoffice.intern --server $smtpserver > $tmpfile; then
    echo "success: $0: SMTP to $smtpserver worked, email to postmaster sent."
else
    echo "error: $0: SMTP to $smtpserver did not work."
    sed "s%^%info: $0: swaks: %" < $tmpfile
fi
#rm $tmpfile

exit $retval
