diff --git a/astroid/.gitignore b/astroid/.gitignore index ac95138..990e415 100644 --- a/astroid/.gitignore +++ b/astroid/.gitignore @@ -1,4 +1,5 @@ *.log +scripts/pending.save scripts/tag-folder-rules.sh scripts/tag-list.csv searches diff --git a/astroid/poll.sh b/astroid/poll.sh index bf24f38..73f90e9 100755 --- a/astroid/poll.sh +++ b/astroid/poll.sh @@ -53,6 +53,10 @@ echo "[DEBUG] Tag mails based on their folder (tag-folder.sh)" bash "$CURDIR"/scripts/tag-folder.sh # generates commands from tag-folder.csv bash "$CURDIR"/scripts/tag-folder-rules.sh # executes generated commands +# Handle pending days (tag:/pend*/) +echo "[DEBUG] Handle pending tags (pending.sh)" +bash "$CURDIR"/scripts/pending.sh + # Mark mails in certain folders read echo "[DEBUG] Mark mails in trash folders read" notmuch tag -unread -- path:Trash/** diff --git a/astroid/scripts/pending.sh b/astroid/scripts/pending.sh new file mode 100755 index 0000000..ebd5201 --- /dev/null +++ b/astroid/scripts/pending.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +CURDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +SAVEFILE="$CURDIR"/pending.save # Location of logfile +BASETAG="pend" +UNPENDTAG="unpend" + +function pdate { + DATE=$(date +%y-%m-%d_%H:%M:%S) + echo "[$DATE]" +} + +function debugmsg { + if ${DEBUG}; then + echo "$(pdate) $*" + fi +} + +function datediff { + d1=$(date -d "$1" +%s) + d2=$(date -d "$2" +%s) + echo $(( (d1 - d2) / 86400 )) +} + + +# Load the values from the last run +touch "$SAVEFILE" +# shellcheck source=/dev/null +source "$SAVEFILE" +# Get the current day +CURDATE=$(date +%Y-%m-%d) +# Populate $LASTDATE if empty +if [[ -z "$LASTDATE" ]]; then + LASTDATE="$CURDATE" + echo "LASTDATE=$LASTDATE" > "$SAVEFILE" +fi + +# Get pending tags +pending_tags=$(notmuch search --output=tags '*' | grep "^$BASETAG") + +# Reduce pending dates if current date not last saved date +if [[ "$CURDATE" != "$LASTDATE" ]]; then + ddiff=$(datediff "$CURDATE" "$LASTDATE") + for tag in $pending_tags; do + # Get day element + day=$(grep -Eo '[0-9]*$' <<< "$tag") + + # Calculate new day for pending tag + day=$(( day - ddiff )) + + # if new pending days more than 1 + if [[ $day -ge 1 ]]; then + notmuch tag -"$tag" +"${BASETAG}${day}" -- tag:"$tag" + # otherwise, delete pending tag and set unpending tag + else + notmuch tag -"$tag" +"$UNPENDTAG" -- tag:"$tag" + fi + done + + # Write the current date in the file + sed -i "s/LASTDATE\=.*/LASTDATE=$CURDATE/" "$SAVEFILE" +fi