#! /bin/sh

UNREAD="AND tag:unread"
READ="AND NOT tag:unread"
#NOTMUCH=$HOME/local/bin/notmuch
NOTMUCH=/usr/bin/notmuch

usage() {
    echo "usage: " $(basename $0) MAILDIR QUERY
    exit 1
}

if [ $# -lt 2 ]; then
    usage
fi

MAILDIR=$1
shift
QUERY=$*

mkdir -p $MAILDIR 2>/dev/null || true
cd $MAILDIR
mkdir cur new tmp 2>/dev/null || true
rm cur/* new/* tmp/* 2>/dev/null

for msg in $($NOTMUCH search --output=files $QUERY $UNREAD); do
    ln -s $msg new
done

for msg in $($NOTMUCH search --output=files $QUERY $READ); do
    ln -s $msg cur
done