#!/bin/bash

if [ -d ~/.balsa ]; then
    printf >&2 '~/.balsa already exists, will not run test.\n'
    exit 1
fi

if [ -n "$AUTOPKGTEST_ARTIFACTS" ]; then
    export workdir=$AUTOPKGTEST_ARTIFACTS
else
    export workdir=$(mktemp -d)
fi

printf 'working directory: %s\n' "$workdir"

mkdir ~/.balsa
mkdir -p "$workdir/mail/"{mailbox/,outbox/,sent/,drafts/,trash/}{cur,new,tmp}
cat > ~/.balsa/config <<EOF
[Globals]
MailDir=$workdir/mail

[Notifications]
GtkUIManager=true

[mailbox-Inbox]
Path=$workdir/mail/mailbox
Type=LibBalsaMailboxMaildir
Name=Inbox

[mailbox-Outbox]
Path=$workdir/mail/outbox
Type=LibBalsaMailboxMaildir
Name=Outbox

[mailbox-Sentbox]
Path=$workdir/mail/sent
Type=LibBalsaMailboxMaildir
Name=Sent

[mailbox-Draftbox]
Path=$workdir/mail/drafts
Type=LibBalsaMailboxMaildir
Name=Drafts

[mailbox-Trash]
Path=$workdir/mail/trash
Type=LibBalsaMailboxMaildir
Name=Trash

[identity]
CurrentIdentity=user

[identity-user]
FullName=Example User
Address=user@example.com

[smtp-server-Default]
Server=localhost:25
Anonymous=true
EOF
cat > ~/.balsa/config-private <<EOF
EOF

xvfb-run bash -c 'balsa --checkmail & sleep 2 ; xwd -name Balsa | gm convert - $workdir/current.png; killall balsa; wait %1'

gm compare -metric mse "$workdir/current.png" debian/tests/orig-screenshot.png
