#!/bin/bash
#
# $1 = message id
#

DIR_JUNK="INBOX.Junk"
DIR_INBOX="INBOX"
LOG="/home/$USER/.config/astroid/hooks/hook.log"

ID=$1
FILE=$(notmuch search --exclude=false --output=files "id:$ID")    # get actual file path of MID

if [[ $(notmuch search "id:$ID" and tag:spam) ]]; then  # only do something if mail is spam
  notmuch tag -spam -- "id:$ID"                         # unspam mail
  echo "spam removed from $ID" >> $LOG
else
  echo "no spam $ID" >> $LOG
fi

if $(echo $FILE | grep -q $DIR_JUNK); then              # if mail is in spam dir
  FILE_NEW=$(echo $FILE | sed "s|$DIR_JUNK/|$DIR_INBOX/|")
  mv "$FILE" "$FILE_NEW"                                # move from spamdir to inbox
  echo "moved $FILE to $FILE_NEW" >> $LOG
else
  echo "not in junk folder $FILE" >> $LOG
fi
