#!/usr/bin/env bash
# Copyright (c) Microsoft Corporation.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, see <https://www.gnu.org/licenses/>.

rm -f /etc/dovecot/conf.d/10-auth.conf

cat >"/etc/dovecot/local.conf" <<-EOF
mail_driver = maildir
mail_path = ~/Maildir
mail_inbox_path = ~/Maildir

auth_mechanisms = gssapi
auth_gssapi_hostname = `hostname -f`
auth_krb5_keytab = /etc/dovecot.keytab

passdb static {
  driver = static
  passdb_static_password = test
}

userdb static {
  driver = static
  fields {
    uid=nobody
    gid=nogroup
    home=/srv/dovecot-dep8/%{user | username}
  }
}
EOF

mkdir -p /srv/dovecot-dep8
chown nobody:nogroup /srv/dovecot-dep8

systemctl restart dovecot.service

echo "Sending a test message via the LDA"
/usr/lib/dovecot/dovecot-lda -f "test@example.com" -d dep8 <<EOF
Return-Path: <test@example.com>
Message-Id: <dep8-test-1@debian.org>
From: Test User <test@example.com>
To: dep8 <dep8@example.com>
Subject: DEP-8 test

This is just a test
EOF

echo "Verifying that the email was correctly delivered"
if [ -z "$(doveadm search -u dep8 header message-id dep8-test-1@debian.org)" ]; then
        echo "Message not found"
        exit 1
fi

echo "Done"
