--- /dev/null
+#!/bin/bash
+
+##################################################################
+# START_IP_HEADER #
+# #
+# Written by Francois Fleuret #
+# Contact <francois.fleuret@idiap.ch> for comments & bug reports #
+# #
+# END_IP_HEADER #
+##################################################################
+
+set -e
+
+RESULT_MBOX="/tmp/gma"
+
+if [[ -z "${MAIL_ARCHIVE_DIR}" ]]; then
+ echo "Undefined \$MAIL_ARCHIVE_DIR" >&2
+ exit 1
+fi
+
+if [[ $1 ]]; then
+
+ PATTERN=$1
+
+ if [[ -z "${PATTERN}" ]]; then
+ echo "No pattern." >&2
+ exit 1
+ fi
+
+ if [[ $2 ]]; then
+ FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${2}/*/*gz"
+ SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/${2} | cut -f 1 -d" ")
+ else
+ FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*gz"
+ SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/ | cut -f 1 -d" ")
+ fi
+
+ echo "Looking for ${PATTERN}"
+ echo " in ${FILE_PATTERN}"
+ echo " writing result in ${RESULT_MBOX}"
+
+ cat ${FILE_PATTERN} \
+ | pv -s ${SIZE} -p -t -e - \
+ | zcat \
+ | mboxgrep -i "$1" \
+ | mboxgrep -v '^Subject:.*SPAM' > ${RESULT_MBOX}
+ echo "Found "$(grep ^"From " ${RESULT_MBOX} | wc -l)" messages."
+
+else
+
+ rm -i ${RESULT_MBOX}
+
+fi