#!/bin/sh
#
# mailboxes_sync - count mail with mutt

/usr/local/bin/mw -Y

# Needed for herbe notifications
export HOME="${HOME%/}"
# export DISPLAY=:0
# export XAUTHORITY="${HOME%/}/.Xauthority"

prefix="${HOME%/}/.local/share/mail"

# loop through every account in ~/.mail
for account in "${prefix}"*; do
    # find all unread mail whose file is newer that the last time this script was run and count them
    newcount=$(find "${account}"/INBOX/new -type f -newer ~/.config/neomutt/.mailsynclast 2> /dev/null | wc -l)
    # are there any new unread mail?
    if [ "${newcount}" -gt "0" ]; then
        # send a notification
        /usr/local/bin/terminal-notifier -title "New Mail" -message "${newcount} new mail in mailbox ${account#"${prefix}"}" &
        nohup /usr/local/mybin/mn "${newcount} new mail" >/dev/null &
    fi
done

# Update access time of a marker file
touch ~/.config/neomutt/.mailsynclast

# notmuch
#notmuch --config ~/.config/notmuch-config new
