#!/bin/sh
set -e

test -z "$XDG_RUNTIME_DIR" &&
    export XDG_RUNTIME_DIR=/run/user/$(id -u)

getnew(){
    find ~/.local/share/mail/INBOX{,.{Contas,Faculdade,Git,Trabalhos}}/new -type f | wc -l
}

OLD=$(getnew)

[ "$1" = "update" ] && mbsync -a

NEW=$(getnew)

if [ "$NEW" -gt "$OLD" ];then
    notify-send " New E-Mails!"
fi

echo $NEW | sed 's/^0$//'

if [ "$(ps -o comm= $PPID)" != "waybar" ] && pidof waybar>/dev/null;then
    pkill -SIGRTMIN+4 waybar
fi

# vim:ft=sh
