#!/usr/bin/env sh

echo
echo Unit Test:
echo
bats --tap test_mw.bats

echo
echo "Integration tests need working email $mwaddr"
echo "CTRL-C to skip"

#modify
if [ -z "$MAILDIR" ] || [ -z "$mwaddr" ]; then
    echo "First do:"
    [ -z "$MAILDIR" ] && echo "export MAILDIR=..."
    [ -z "$mwaddr" ] && echo "export mwaddr=your.email@gmail.com"
    exit
fi

cd .. && sudo make install && OK="OK"

if [ "$OK" = "OK" ]; then

    echo "Warning: Out with CTRL-C if your mail (config) files are important here."

    export mwmaildir=$MAILDIR
    export mwname='r n'
    export mwlogin=$mwaddr
    export mwpass="${mwpass:-mutt-wizard-$mwaddr}"
    mwaccmaildir="$mwmaildir/$mwaddr"

    rm -rf $mwaccmaildir

    [ -f "${PASSWORD_STORE_DIR:-~/.password-store}/$mwpass.gpg" ] || pass insert $mwpass

    echo "= Test 1 ="
    mwtype=online mw add
    echo "=> In another prompt, start mutt and verify that mails are shown"
    echo "=> Then answer with N"
    mw purge
    rm -rf $mwaccmaildir

    echo "= Test 2 ="
    mwtype=offline mw add
    mw

    echo "=> In another prompt, start mutt and verify that mails are shown"
    echo "=> Press i1, wait, press i2. Verify that mutt is fast again after i2."
    echo "=> Press gm and gM, to start mw sync."
    echo "=> Answer with Y and check that mbsyncrc, msmtp/config and mutt/accounts are gone."
    mw purge

fi

cd $CD
