#!/bin/zsh
#dep:perl notify-send
DEFAULT=raw
ICON=/usr/share/icons/Adwaita/48x48/actions/mail-message-new.png
opt() {
	if [ -n "$1" ]; then
		return 0
	else
		return 1
	fi
}
contains() {
	arr=$1
	value=$2
	[[ ${${(P)arr}[(ie)$value]} -lt ${#${(P)arr}} ]]
}
zparseopts -D -E h=help -help=help \
	r=raw -raw=raw \
	n=number -number=number \
	p=pretty -pretty=pretty
if [ -n "$help" ]; then
	cat </\>/'
}
from() { formail -zcb -xFrom | tail -n 1 | decode }
subject() { formail -zcb -xSubject | decode }
if opt $number; then
	echo -n $newmails | wc -l
	exit 0
fi
if opt $pretty; then
	touch $cache
	seen=$(<$cache)
	truncate --size 0 $cache
	notify-send --icon=$ICON "$(
		echo -n $newmails | while read mail; do
			if ! contains seen $mail; then
				echo -n "$(from < $mail): $(subject < $mail)
"
			fi
			echo $mail >> $cache
		done
	)"
	exit 0
fi
#otherwise
echo -n $newmails