#!/bin/sh export NOTIFY_SHORT=email export NOTIFY_LONG=email : ${MAILDIR:=$HOME/mail} readonly CACHE="${XDG_CACHE_HOME:=$HOME/.cache}/email-listener" touch "$CACHE" count() { local cur=`find "$MAILDIR"/*/Inbox/cur -type f | wc -l` local new=`find "$MAILDIR"/*/Inbox/new -type f | wc -l` local sum=$((cur + new)) case $sum in 0) printf 'icon=\ntext=No Email' ;; 1) printf 'icon=\ntext=1 Email' ;; *) printf 'icon=\ntext=%d Emails' $sum ;; esac [ $new -gt 0 ] && printf ' (%d Unread)' $new echo echo $new >"$CACHE" } { while : do sleep 1 read new_mails <"$CACHE" if [ $new_mails -gt ${prev:=0} ] then local title local desc case $((new_mails - prev)) in 1) title='New Email' desc='1 new email has been received' ;; *) title='New Emails' desc="$new_mails new emails have been received" ;; esac notify "$title" "$desc" prev=$new_mails fi done } & count | jo inotifywait -qm "$MAILDIR"/*/Inbox/new "$MAILDIR"/*/Inbox/cur \ | while read _ event _ do case "$event" in CREATE|DELETE|MOVED_*) count | jo ;; esac done