home · contact · privacy
7d96256fe9bb8a26075511b5a5b154f7bc15b92b
[config] / bookworm / home_files / user / mail_sync.sh
1 #!/bin/sh
2 set -e
3
4 basedir="/home/plom/mail/maildir/"
5 # Ensure directories exist for all "dir:*" tags.
6 for tag in $(notmuch search --output=tags '*'); do
7     if [ ! $(echo "${tag}" | cut -c-4) = "dir:" ]; then
8         continue
9     fi
10     target_dir="${basedir}"$(echo "${tag}" | cut -c5-)"/cur/"
11     if [ ! -d "${target_dir}" ]; then
12         echo "Directory ${target_dir} does not exist."
13         exit 1
14     fi
15 done
16
17 # Ensure all "dir:*"-tagged mails are in proper directories,
18 # remove all "dir:*" tags.
19 for tag in $(notmuch search --output=tags '*'); do
20     if [ ! $(echo "${tag}" | cut -c-4) = "dir:" ]; then
21         continue
22     fi
23     target_dir="${basedir}"$(echo "${tag}" | cut -c5-)"/cur/"
24     for f in $(notmuch search --output=files tag:"${tag}"); do
25          echo DEBUG0 $f
26          new_name=$(basename "${f}" | sed -e 's/,U=[0-9]*//')
27          echo DEBUG0 $new_name
28          target_path="${target_dir}${new_name}"
29          echo DEBUG0 $target_path
30          if [ ! "${target_path}" = "${f}" ]; then
31              echo "Moving ${f} to ${target_path}."
32              mv "${f}" "${target_path}"
33              # NOTE: if we encounter an error here of ${f} not being findable, run "notmuch reindex tag:${tag}" to fix 
34          fi
35     done
36     notmuch tag -"${tag}" tag:"${tag}"
37 done
38
39 # Remove all "deleted"-tagged files from maildirs.
40 notmuch search --output=files tag:deleted | while read f; do
41     echo "Deleting ${f}"
42     rm "${f}"
43 done
44
45 # Sync changes back to server and update notmuch index.
46 mbsync -a
47 notmuch new