X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=bullseye%2Fhome_files%2Fuser%2Fmail_sync.sh;fp=bullseye%2Fhome_files%2Fuser%2Fmail_sync.sh;h=6962800090262de68539f10101f4fc36f6d62298;hb=15171dcfb9e48aa0311fb77948478dbca48245fa;hp=0000000000000000000000000000000000000000;hpb=e6322abd2e985e59e8320e92a5b9b3082f21375a;p=config diff --git a/bullseye/home_files/user/mail_sync.sh b/bullseye/home_files/user/mail_sync.sh new file mode 100755 index 0000000..6962800 --- /dev/null +++ b/bullseye/home_files/user/mail_sync.sh @@ -0,0 +1,43 @@ +#!/bin/sh +set -e + +basedir="/home/plom/mail/maildir/" +# Ensure directories exist for all "dir:*" tags. +for tag in $(notmuch search --output=tags '*'); do + if [ ! $(echo "${tag}" | cut -c-4) = "dir:" ]; then + continue + fi + target_dir="${basedir}"$(echo "${tag}" | cut -c5-)"/cur/" + if [ ! -d "${target_dir}" ]; then + echo "Directory ${target_dir} does not exist." + exit 1 + fi +done + +# Ensure all "dir:*"-tagged mails are in proper directories, +# remove all "dir:*" tags. +for tag in $(notmuch search --output=tags '*'); do + if [ ! $(echo "${tag}" | cut -c-4) = "dir:" ]; then + continue + fi + target_dir="${basedir}"$(echo "${tag}" | cut -c5-)"/cur/" + for f in $(notmuch search --output=files tag:"${tag}"); do + new_name=$(basename "${f}" | sed -e 's/,U=[0-9]*//') + target_path="${target_dir}${new_name}" + if [ ! "${target_path}" = "${f}" ]; then + echo "Moving ${f} to ${target_path}." + mv "${f}" "${target_path}" + fi + done + notmuch tag -"${tag}" tag:"${tag}" +done + +# Remove all "deleted"-tagged files from maildirs. +notmuch search --output=files tag:deleted | while read f; do + echo "Deleting ${f}" + rm "${f}" +done + +# Sync changes back to server and update notmuch index. +mbsync -a +notmuch new