#!/bin/sh
set -e

if test -z "$SWAYSOCK" && pidof gnome-shell > /dev/null
then
    exit 0
fi

die() {
    test "$?" = 0 ||
        notify-send "Failed to sync mail" "try unlocking your gpg key"
}
# trap die EXIT

{%@@ for name in mail | reverse @@%}
    mkdir -p "$HOME/.local/share/mail/{{@@ name @@}}/"
{%@@ endfor @@%}

if test "$1" = "update" && ! pidof mbsync; then
    if ! ping -c3 {{@@ mail.personal.imap  @@}} > /dev/null
    then
        exit 0
    fi
    timeout 10m mbsync -a
fi

cd "$HOME/.local/share/mail/" >/dev/null

find */INBOX*/new -type f |
    wc -l |
    sed 's/^0$//'

# vim:ft=sh
