#!/bin/bash

# Get the current message's notmuch ID from aerc's environment
# We'll use notmuch to find and move the file

maildir="$HOME/.local/share/mail"

# Find files for messages in current thread that aren't already in Trash
notmuch search --output=files thread:{} 2>/dev/null | while read filepath; do
    # Skip if already in Trash
    if [[ "$filepath" == *"/Trash/"* ]]; then
        continue
    fi
    
    if [[ -f "$filepath" ]]; then
        filename=$(basename "$filepath")
        mkdir -p "$maildir/Trash/cur"
        mv "$filepath" "$maildir/Trash/cur/$filename"
    fi
done

# Re-index
notmuch new >/dev/null 2>&1
