home · contact · privacy
Add mail_sync.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 2 Aug 2023 03:08:41 +0000 (05:08 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 2 Aug 2023 03:08:41 +0000 (05:08 +0200)
bookworm/home_files/user/mail_sync.sh [new file with mode: 0755]

diff --git a/bookworm/home_files/user/mail_sync.sh b/bookworm/home_files/user/mail_sync.sh
new file mode 100755 (executable)
index 0000000..7d96256
--- /dev/null
@@ -0,0 +1,47 @@
+#!/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
+        echo DEBUG0 $f
+         new_name=$(basename "${f}" | sed -e 's/,U=[0-9]*//')
+        echo DEBUG0 $new_name
+         target_path="${target_dir}${new_name}"
+        echo DEBUG0 $target_path
+         if [ ! "${target_path}" = "${f}" ]; then
+             echo "Moving ${f} to ${target_path}."
+             mv "${f}" "${target_path}"
+            # NOTE: if we encounter an error here of ${f} not being findable, run "notmuch reindex tag:${tag}" to fix 
+         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