#! /bin/sh

# Remove duplicate messages from mainbox

function dedup_mailbox ()
{
    local uids=( $(doveadm -f flow fetch -u $1 "uid" mailbox "$2" all | cut -f 2 -d =) )
    if [ ${#uids[@]} -eq 0 ]; then
        echo "   No messages"
        return
    elif [ ${#uids[@]} -eq 1 ]; then
        echo "   Only one message"
        return
    fi

    for (( i=0; i<${#uids[@]}; i++ )); do
        local md5s_u[$i]=$(echo $(doveadm -f flow fetch -u $1 "date.received body" mailbox "$2" uid ${uids[$i]} | openssl md5)",${uids[$i]}")
        echo -en "   Compute hashes: $i/${#uids[@]}(${md5s_u[$i]})\r"
    done

    echo -en "                                                                      \r"

    local md5s=( $(echo ${md5s_u[@]} | sed 's/ /\n/g' | sort) )

    x=0
    i=0
    while [ $i -lt $((${#md5s[@]} - 1)) ]; do
        A=$(echo ${md5s[$i]} | cut -f 1 -d ,)
        for (( j=$(($i + 1)); j<${#md5s[@]}; j++ )); do
            B=$(echo ${md5s[$j]} | cut -f 1 -d ,)
            if [ $A == $B ]; then
                doveadm expunge -u $1 mailbox "$2" uid $(echo ${md5s[$j]} | cut -f 2 -d ,)
                x=$(($x + 1))
            else
                break
            fi
        done

        echo -en "   Expunged $x message(s) from $(($j + 1))/${#md5s[@]}\r"
        i=$j
    done
    echo ""
}

if [ $2 == "-A" ]; then
    eval boxes=( $(doveadm mailbox list -u $1  | sed 's/.*/"&"/') );
else 
    boxes[0]=$2
fi

for (( k=0; k<${#boxes[@]}; k++ )); do
    echo "${boxes[$k]}:"
    dedup_mailbox $1 "${boxes[$k]}"
done 
