#!/usr/bin/env bash

## an initial tagging script for notmuch mail 
# https://afew.readthedocs.io/en/latest/

#FIXME: NOT TESTED YET needs notmuch-mutt installed first
#FIXME: add afew tagging: https://readthedocs.org/projects/afew/downloads/pdf/latest/

apt install -y afew # provides /usr/bin/afew

DEST=${1:-/etc/skel}

# Put a list of filters into ~/.config/afew/config :
mkdir -p ${DEST}/.config/afew
cat > ${DEST}/.config/afew/config <<'EOF'
# This is the default filter chain
[SpamFilter]
[KillThreadsFilter]
[ListMailsFilter]
[ArchiveSentMailsFilter]
[InboxFilter]
EOF

MAILDIR_PATH=
# https://notmuchmail.org/initial_tagging/

# The [new] config section allows you to control which tags new messages receive. By default, notmuch config will use the tags inbox and unread.
# An ad-hoc approach to initial message tagging, which sets all new messages to get the new tag:
#sed -i 's,^\(tags=\).*,\1'new\;',' ~/.notmuch-config
sed -i '/\[new\]/{n;s/.*/tags=new\;/}' .notmuch-config

# After running notmuch new, all new messages will be marked new. You can then do various tag post-processing by just acting on messages with that tag.

MAILDIR="${DEST}/.local/share/mail"  # set by mutt-wizard setup

# And create a post-new hook for notmuch.
mkdir -p ${MAILDIR}/.notmuch/hooks

cat >  ${MAILDIR}/.notmuch/hooks/post-new.notmuch.example <<EOF
# immediately archive all messages from "me"
notmuch tag -new -- tag:new and from:me@example.com

# delete all messages from a spammer:
notmuch tag +deleted -- tag:new and from:spam@spam.com

# tag all message from notmuch mailing list
notmuch tag +notmuch -- tag:new and to:notmuch@notmuchmail.org

# finally, retag all "new" messages "inbox" and "unread"
notmuch tag +inbox +unread -new -- tag:new
EOF

# Note: the queries do not generally include tag:new because this is implied when afew is run with the –new flag.
mkdir -p  ${MAILDIR_PATH}/.notmuch/hooks
cat >  ${MAILDIR_PATH}/.notmuch/hooks/post-new.afew.example <<EOF
[ArchiveSentMailsFilter]

[Filter.1]
message = Delete all messages from spammer
query = from:spam@spam.com
tags = +deleted;-new

[Filter.2]
message = Tag all messages from the notmuch mailing list
query = to:notmuch@notmuchmail.org
tags = +notmuch

[InboxFilter]

EOF

mkdir -p ${MAILDIR_PATH}/.notmuch/hooks
cat > ${MAILDIR_PATH}/.notmuch/hooks/post-new <<'EOF'
!/bin/sh
/usr/bin/afew --tag --new
EOF


#---------------------------------------------------------------------
cat > ${DEST}/.config/afew/afew.config-example <<'EOF'
# global configuration
[global]

#[MailMover]
#folders = INBOX Junk
#max_age = 15

##rules
#INBOX = 'tag:spam':Junk 'NOT tag:inbox':Archive
#Junk = 'NOT tag:spam and tag:inbox':INBOX 'NOT tag:spam':Archive

# This is the default filter chain
#[SpamFilter]
#[KillThreadsFilter]
#[ListMailsFilter]
#[ArchiveSentMailsFilter]
#[InboxFilter]

# Let's say you like the SpamFilter, but it is way too polite

# 1. create an filter object and customize it
#[SpamFilter.0] # note the index
#message = meh

# 2. create a new type and...
#[ShitFilter(SpamFilter)]
#message = I hatez teh spam!

#    create an object or two...
#[ShitFilter.0]
#[ShitFilter.1]
#message = Me hatez it too.

# 3. drop a custom filter type in ~/.config/afew/
#[MyCustomFilter]


# To create a custom generic filter, define it inline with
# your above filter chain. E.g.:

# ...
# [ListMailsFilter]
#
# [Filter.1]
# query = from:boss@office.com
# tags = +office
#
# [ArchiveSentMailsFilter]
# ...
EOF

