#!/bin/sh
# Nico Schottelius, 2024-05-08
# Can also be run in a loop with:
# for uid in $(find . | grep -o 'U=.*:' | sort | uniq -d | sed -e 's/U=//' -e 's/:$//') ; do fix-maildir-mbsync-duplicate-uid $uid; done

if [ $# -ne 1 ]; then
    echo $0 UID
    echo "Remove the UID from the newer message"
fi

MAIL_UID=$1

i=0

for mail in $(ls *U=${MAIL_UID}:*); do
    i=$((i+1))
    # Do not rename the first one
    if [ $i = 1 ]; then
        continue
    fi

    # Rename all others
    new_name=$(echo $mail | sed 's/,U=.*//')

    echo "Renaming $mail to $new_name"
    mv $mail $new_name
done
