#! /bin/bash


monthlist () {
grep "$(date +"%Y"-"%m")" /var/log/exim_mainlog | egrep "A=dovecot_login|A=dovecot_plain" | sed -e "s#H=.* [##' -e 's#]:[0-9]*##" | awk '{print $5,$6,$7,$8}' | sed 's/:.*//'| sort | uniq -c | sort -k1n
}

listhourlist () {
grep "$(date +"%Y"-"%m"-"%d %H")" /var/log/exim_mainlog | egrep "A=dovecot_login|A=dovecot_plain" | sed -e "s#H=.* [##' -e 's#]:[0-9]*##" | awk '{print $5,$6,$7,$8}' | sed 's/:.*//'| sort | uniq -c | sort -k1n
}

todaylist () {
grep "$(date +"%Y"-"%m"-"%d")" /var/log/exim_mainlog | egrep "A=dovecot_login|A=dovecot_plain" | sed -e "s#H=.* [##' -e 's#]:[0-9]*##" | awk '{print $5,$6,$7,$8}' | sed 's/:.*//'|  sort | uniq -c | sort -k1n
}

show_sending () {
grep "$(date +"%Y-%m-%d")" /var/log/exim_mainlog | egrep "A=dovecot_login|A=dovecot_plain"  | grep for  | awk '{print $1,$2,$3,$4,"dovecot_login="$5,$6,$7,$8,$NF}'  | sed  's/^\[.*.\]\ //g' | sort -k3h
}

usage () {
echo "Options are:
                      --lasthour
                      --today
                      --month
                      --details # this shows overview of authenticated sent mail with some transaction details
                      --help"
}

if ! [ "$1" ] ; then
                usage
                exit 1
        elif    [ "$1" == "--help" ] ; then
                usage
fi

if [ "$1" == "--lasthour" ] ; then
                if [ "$2" == "--run" ] ; then
                lasthourrun               
                exit
                else
                listhourlist
                exit
                fi
fi

if [ "$1" == "--today" ] ; then
                if [ "$2" == "--run" ] ; then
                todayrun
                exit
                else
                todaylist                
                exit
                fi
fi

if [ "$1" == "--month" ] ; then
                if [ "$2" == "--run" ] ; then
                monthrun
                exit
                else
                monthlist                
                exit
                fi
fi

if [ "$1" == "--details" ] ; then
                show_sending
                exit
fi
