#!/bin/sh
set -e

opts="--no-config --set .mailbox.mailbox-type=mbox"
mbox=testsuite/spool/mbox1

# Reading: pipe an ex-style command to a readonly session.
echo 'print 1' | MAILRC=/dev/null \
    mail.mailutils $opts -N -E 'set readonly' -f "$mbox" | grep -q Jabberwocky

# Sending: point sendmail at a stub that just captures the message,
# so nothing ever touches the network.
stub="$AUTOPKGTEST_TMP/stub-sendmail"
out="$AUTOPKGTEST_TMP/mail.out"

cat > "$stub" <<EOF
#!/bin/sh
cat > "$out"
EOF
chmod +x "$stub"

echo 'Test message.' | MAILRC=/dev/null \
    mail.mailutils $opts -N -E "set sendmail=$stub" \
        -s 'Test Subject' recipient@example.net

grep -q '^Subject: Test Subject' "$out"
grep -q '^To: .*recipient@example.net' "$out"
grep -q 'Test message.' "$out"
