#!/bin/bash

# pre-new --- Notmuch rules that run before notmuch-new(1)
#
# Copyright (c) 2021-2025  Protesilaos Stavrou <info@protesilaos.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
### Commentary:
#
# Move unwanted email to the appropriate local directory and tag it
# accordingly.  The order of those commands matters.
#
# Part of my dotfiles: <https://github.com/protesilaos/dotfiles>.

### Code:

if [ "$HOSTNAME" = "kronos" ]
then
    mail_root_path=$(notmuch config get database.path)
    for tag in del spam
    do
        target_directory="${mail_root_path}/${tag}local/"

        if [ ! -d "$target_directory" ]
        then
            mkdir -p "$target_directory"
        fi

        while IFS= read -r -d $'\0' file
        do
            if [ -f "$file" ]
            then
                mv "$file" "$target_directory$(basename -- "${file//,U=[0-9]*:/:}")"
            fi
        done < <(notmuch search --output=files --format=text0 tag:"$tag")

        notmuch tag -inbox -"${tag}" +"${tag}"local -- tag:"$tag"
    done
fi

### PROT 2025-03-01: To archive mail manually, I did the following.
### This is useful once every few years, so there is no need to
### automate it. Note that this assumes that mbsync can remove and
### expunge mail on the far/server side.
#
# Confirm that we have messages matching these search terms:
#
#     notmuch count tag:"sent" from:*@protesilaos.com date:..2024-01-01
#
# Now add a tag to all of them to make it clear they are archived:
#
#     notmuch tag +archivesent -sent -- tag:"sent" from:*@protesilaos.com date:..2024-01-01
#
# Confirm the count is the same for the new tag:
#
#     notmuch count tag:"archivesent"
#
# Same principle for the inbox:
#
#     notmuch count tag:"inbox" date:..2024-01-01
#     notmuch tag +archiveinbox -inbox -- tag:"inbox" date:..2024-01-01
#     notmuch count tag:"archiveinbox"
#
# Now move the messages to their respective directories, which I
# created beforehand (note that I use `echo` to make sure it works
# before doing anything---remove the `echo` to actually move the
# files).
#
#     while IFS= read -r -d $'\0' file; do echo mv "$file" "$HOME"/.mail/inboxlocal/$(basename -- "${file//,U=[0-9]*:/:}") ; done < <(notmuch search --output=files --format=text0 tag:"archiveinbox" and not tag:"archivesent")
#
#     while IFS= read -r -d $'\0' file; do echo mv "$file" "$HOME"/.mail/sentlocal/$(basename -- "${file//,U=[0-9]*:/:}") ; done < <(notmuch search --output=files --format=text0 tag:"archivesent")
#
# Then do an `mbsync` followed by `notmuch new` to synchronise everything.
#
# In case the aforementioned tags are confusing:
#
#     notmuch tag -archiveinbox +iarchive -- tag:archiveinbox
#     notmuch tag -archivesent +isent -- tag:archivesent
