From 438c25f4ff0dd682a311e00e17dcdbfd7e6e3481 Mon Sep 17 00:00:00 2001
From: David JULIEN <swytch@mailo.com>
Date: Fri, 4 Nov 2022 14:50:41 +0100
Subject: [PATCH] [mail] feat: add filters for univ

---
 .config/notmuch/default/hooks/post-new |  3 +++
 .config/notmuch/default/hooks/pre-sync | 27 ++++++++++++++++++++++++++
 .local/bin/statusbar/sb-mailbox        |  6 +++---
 3 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100755 .config/notmuch/default/hooks/post-new
 create mode 100755 .config/notmuch/default/hooks/pre-sync

diff --git a/.config/notmuch/default/hooks/post-new b/.config/notmuch/default/hooks/post-new
new file mode 100755
index 0000000..5e1c658
--- /dev/null
+++ b/.config/notmuch/default/hooks/post-new
@@ -0,0 +1,3 @@
+#!/usr/bin/env sh
+
+notmuch tag --input $XDG_CONFIG_HOME/notmuch/tags/univ
diff --git a/.config/notmuch/default/hooks/pre-sync b/.config/notmuch/default/hooks/pre-sync
new file mode 100755
index 0000000..d62bc58
--- /dev/null
+++ b/.config/notmuch/default/hooks/pre-sync
@@ -0,0 +1,27 @@
+#!/usr/bin/env sh
+
+# Archive files that are NOT tagged Inbox
+accounts="univ"
+
+# Update paths & tags
+notmuch new --no-hooks
+
+filter() {
+        notmuch search --output=files --format=text0 $parameters and tag:new| xargs -0 --no-run-if-empty mv -t "$XDG_DATA_HOME/mail/$account/$tag/new/"
+        notmuch search --output=files --format=text0 $parameters and not tag:new| xargs -0 --no-run-if-empty mv -t "$XDG_DATA_HOME/mail/$account/$tag/cur/"
+}
+
+for account in $accounts
+do
+        tags_location="$XDG_CONFIG_HOME/notmuch/tags/$account"
+        tags="$(grep "+" "$tags_location" | sed -e 's/^.//' -e 's/ .*//')"
+        for tag in $tags
+        do
+                [ "univ" = $tag ] && continue
+                parameters="folder:$account/INBOX and tag:$tag"
+                filter
+        done
+done
+
+# Update paths
+notmuch new --no-hooks
diff --git a/.local/bin/statusbar/sb-mailbox b/.local/bin/statusbar/sb-mailbox
index c2d46be..9f0d45b 100755
--- a/.local/bin/statusbar/sb-mailbox
+++ b/.local/bin/statusbar/sb-mailbox
@@ -16,9 +16,9 @@ do
 	new="$(find "${XDG_DATA_HOME:-$HOME/.local/share}"/mail/$acc -path */new/* -type f | grep -iv -e "trash" -e "drafts" | wc -l 2>/dev/null)"
 	if [ $new -gt 0 ]; then
                 box=""
-		[ -n "$(echo "$acc" | sed -n '/@univ/p')" ] && box=""
-		[ -n "$(echo "$acc" | sed -n '/@mailo/p')" ] && box=""
-		[ -n "$(echo "$acc" | sed -n '/@posteo/p')" ] && box=""
+		[ -n "$(echo "$acc" | sed -n '/univ/p')" ] && box=""
+		[ -n "$(echo "$acc" | sed -n '/mailo/p')" ] && box=""
+		[ -n "$(echo "$acc" | sed -n '/posteo/p')" ] && box=""
 		unread="$unread/$box$new"
 	fi
 done