#!/bin/sh export NOTIFY_SHORT=email export NOTIFY_LONG=email : ${MAILDIR:=$HOME/mail} readonly CACHE="${XDG_CACHE_HOME:=$HOME/.cache}/email-listener" touch "$CACHE" T() { en="$1" sv="$2" shift 2 case "${LC_ALL:-$LANG}" in en_*) printf "$en" "$@" ;; sv_*) printf "$sv" "$@" esac } 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) T 'icon=\ntext=No Emails' 'icon=\ntext=Inga meddelanden' ;; 1) T 'icon=\ntext=1 Email' 'icon=\ntext=1 meddelande' ;; *) T 'icon=\ntext=%d Emails' 'icon=\ntext=%d meddelanden' $sum ;; esac [ $new -gt 0 ] && T ' (%d Unread)' ' (%d ölasta)' $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="$(T 'New Email' 'Nytt meddelande')" desc="$(T '1 new email has been received' '1 nytt meddelande har mottagits')" ;; *) title="$(T 'New Emails' 'Nya meddelanden')" desc="$(T "$new_mails new emails have been received" "$new_mails nya meddelanden har mottagits")" ;; 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