#! /bin/bash
# 27Aug2022 initial, Howell [copy, edit] from :
#		"$d_SysMaint"'email programs/bogotrainer/bogotrainer-1.0-0/bogonotspam.sh'

echo
echo "# **********************************************"
echo "# register Unsure/Spam messages left in the inbox as 'not spam'"
echo "# put Spam in the inbox to re-registered it as 'not spam'"
echo "#"
echo "# this script was written for the user Maildir folder"
echo "# using debian exim4, dovecot, maildrop and bogofilter"
echo "# filtering Spam and more in a user .mailfilter file"
echo "# **********************************************"
echo

startSec="$(date +%s)"
startDate="$(date)"

#	inbox="/home/gary/Maildir/cur/*"
	inbox="$d_evolution""mail/local/cur/*"
hamcount=0
unsurecount=0
spamcount=0
messagecount=0

echo "**** Checking Inbox for Spam and Unsure messages. ****"
echo "**** If found, register them as NotSpam. ****"
echo

for i in $inbox
do

    ((messagecount++))

    if grep -rqiw "X-Bogosity: Ham" "$i"
    then
        ((hamcount++))

    elif grep -rqiw "X-Bogosity: Unsure" "$i"
    then
        echo "bogofilter registering Inbox/Unsure as notspam ..."
        bogofilter -v -n -I "$i"
        echo
        ((unsurecount++))

    elif grep -rqiw "X-Bogosity: Spam" "$i"
    then
        echo "bogofilter registering Inbox/Spam as notspam ..."
        bogofilter -v -n -I "$i"
        echo
        ((spamcount++))
    fi

done

echo
echo "Inbox/Message count: $messagecount"
echo "Inbox/Ham messages count: $hamcount"
echo "Inbox/Unsure messages registerd as NotSpam: $unsurecount"
echo "Inbox/Spam messages registerd as NotSpam: $spamcount"
echo

# calculate the end stats
endSec="$(date +%s)"
endDate="$(date)"
totalMin="$[(endSec-startSec)/60]"
totalSec="$[(endSec-startSec)%60]"

echo "********** bogonotspam is complete **********"
echo
echo "Start: $startDate"
echo "End:   $endDate"
echo "Total time (min:sec): $totalMin:$totalSec"
echo

exit 0
