#!/bin/sh
# For emails returned by mairix, add a header with the directory the email
# came from to each email.  
#
# This takes time, since mairix simply makes links to the emails for speed.  
#
# This script:
# 1. removes the links created by mairix in the results directory
# 2. copies the real maildir emails to the results directory instead
# 3. adds a header to the copy of each email in the results directory
#

# The directory where mairix puts results (this is the "mfolder" line in
# .mairixrc)
RESULTS_DIR=$HOME/.mairix/results

# Your maildir directory
MAILDIR=$HOME/Maildir/

# The name of the header to add.  If you're using mutt, you'll want to
# unignore this header.
HEADER_NAME=X-source-folder

# If there are more results than this, don't add the header since it
# will take too long.
MAX_COPY_FILES=200

# ---------------------------------------------------------------
# End user configuration
# ---------------------------------------------------------------

function die() {
    echo "$*"
    exit 1
}

[ -d $RESULTS_DIR ] || die "Results directory $RESULTS_DIR not found"
[ ! -z $HOME ] || die "\$HOME environment variable is not set"
# sed pattern to strip from the beginning of filenames to
# get the maildir folder (replace '/' with '\/'):
MAILDIR_SED=$(echo -n "$MAILDIR" | sed -e 's/\//\\\//g')

echo "Processing results, press Ctrl-C to exit"

# - Copy mails to $RESULTS_DIR instead of linking so we can modify headers
# - Add header to each mail show which folder it's in
function process_folder() {
    FILES=$(find . -printf '%p@%l\n' | egrep -v '^\.@$')
    NUM_FILES=$(echo "$FILES" | wc -l)
    # Skip out on this folder if there are too many files to copy and modify
    if [ $NUM_FILES -gt $MAX_COPY_FILES ]; then
        messages=1
        return
    fi
    for f in $FILES; do
        echo -n "."
        messages=$[ $messages + 1 ]
        local=$(echo -n $f | cut --delimiter='@' --fields=1)
        remote=$(echo -n $f | cut --delimiter='@' --fields=2)
        folder=$(dirname $(dirname $remote))
        folder=$(echo -n $folder | sed -e "s/$MAILDIR_SED//")
        if [ ! -f $remote ]; then
            echo "warning: $local was in list but not found (the mairix index is out of date)"
            continue
        fi
        rm -f $local || die "error during rm -f $local"
        cp $remote temp.mail || die "error during cp $remote $local"
        cat temp.mail | formail -A "$HEADER_NAME: $folder" > $local \
            || die "error adding header to $local"
        rm -f temp.mail || die "error during rm -f temp.mail"
    done
}

# Remove old search results (rm * doesn't work if there are too many)
find $RESULTS_DIR/cur -mindepth 1 -maxdepth 1 -exec rm -f \{\} \;
find $RESULTS_DIR/new -mindepth 1 -maxdepth 1 -exec rm -f \{\} \;
find $RESULTS_DIR/tmp -mindepth 1 -maxdepth 1 -exec rm -f \{\} \;

# Re-index
#mairix

# Search
mairix --threads $*

# Process results
messages=0
cd $RESULTS_DIR/cur 
process_folder
cd $RESULTS_DIR/new 
process_folder
cd $RESULTS_DIR/tmp 
process_folder

# If no messages, say so then exit
if [ $messages -eq 0 ]; then
    echo "No messages, press enter to return"
    read crap
    exit 0
fi

# View search results in a new mutt
mutt -f $RESULTS_DIR
