#!/bin/bash

# post-new --- Notmuch rules that run after notmuch-new(1)
#
# Copyright (C) 2021-2026  Protesilaos <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:
#
# Note that the order of those commands matters.
#
# Part of my dotfiles: <https://github.com/protesilaos/dotfiles>.

### Code:

# remove "unread" from "replied"
notmuch tag -unread -new -- tag:replied

# tag all "new" messages "inbox" and "unread"
while IFS= read -r -d $'\0' file
do
    if [[ "$file" != *R* ]]; then
        notmuch tag +unread -- "path:${file}"
    fi
done < <(notmuch search --output=files --format=text0 tag:new)

notmuch tag +inbox -new -- tag:new

# tag my replies as "sent"
# notmuch tag -new -unread +inbox +sent -- '(from:"*@protesilaos.com*" not to:"*@protesilaos.com*" not tag:list)'
notmuch tag -new -unread +inbox +sent -- '(from:"*@protesilaos.com*" not to:"*@protesilaos.com*" not tag:list not tag:archived)'

# This is imperfect, but add the "coach" tag to any message that
# includes those keywords in the subject line.  The starting date is
# the one I made the announcement:
# <https://protesilaos.com/news/2023-01-20-emacs-linux-life-coach/>.
# I may refine this further based on patterns I observe.
notmuch tag +coach -- date:2023-01-20..now subject:/[Tt]utor\|[Cc]oach\|[Ll]esson/

# Another "mostly okay" but not 100% correct way of identifying my
# package-related messages.
notmuch tag +package -- "(to:protesilaos/*noreply.github.com or to:*incoming.gitlab.com) not tag:archived not tag:list"
