home · contact · privacy
0837390302a4b9d05e55f5bfae163b74969a56be
[config] / buster / home_files / user / mail_sync.sh
1 #!/bin/sh
2 set -e
3
4 # Ensure all "dir:*"-tagged mails are in proper directories,
5 # remove all "dir:*" tags.
6 basedir="/home/plom/mail/maildir/"
7 for tag in $(notmuch search --output=tags '*'); do
8     if [ ! $(echo "${tag}" | cut -c-4) = "dir:" ]; then
9         continue
10     fi
11     target_dir="${basedir}"$(echo "${tag}" | cut -c5-)"/cur/"
12     for f in $(notmuch search --output=files tag:"${tag}"); do
13          new_name=$(basename "${f}" | sed -e 's/,U=[0-9]*//')
14          target_path="${target_dir}${new_name}"
15          if [ ! "${target_path}" = "${f}" ]; then
16              echo "Moving ${f} to ${target_path}."
17              mv "${f}" "${target_path}"
18          fi
19     done
20     notmuch tag -"${tag}" tag:"${tag}"
21 done
22
23 # Remove all "deleted"-tagged files from maildirs.
24 notmuch search --output=files tag:deleted | while read f; do
25     echo "Deleting ${f}"
26     rm "${f}"
27 done
28
29 # Sync changes back to server and update notmuch index.
30 mbsync -a
31 notmuch new