#!/bin/sh
# procmail-re-test
# 20010801 aurelio <verde@aurelio.net>
#
# details:
#  * LOGFILE=/tmp forces error and log is sent to STDERR
#  * /dev/null because we don't want written files
#  * text input is accepted via $2, $3, ... or STDIN
#  * generic usage: procmail-re-test regex [text]
#  * usages: procmail-re-test regex your sample text
#            cat textfile | procmail-re-test regex
#            procmail-re-test regex < textfile
#  * output: Match, No match, Invalid regexp "..."
#  * examples:
#    $ cat abc
#    homer
#    bart
#    maggie
#    $ cat abc | ./procmail-re-test '^m'
#    Match
#    $ ./procmail-re-test '^m' "`cat abc`"
#    Match
#    $ ./procmail-re-test '^M' My cool line
#    Match
#    $ ./procmail-re-test '^z' My cool line
#    No match
#    $ ./procmail-re-test '^(' My cool line
#    Invalid regexp "^("
#

[ "$1" ] || { echo "usage: $0 regex [text]"; exit 1; }
tmpfile=`mktemp /tmp/procmail-re-test.XXXXXX`
regex="$1"; shift; txt="$*"
[ "$txt" ] || txt="`cat /dev/stdin`" # $* or stdin
{ echo -e "VERBOSE=y\nLOGFILE=/tmp\nDEFAULT=/dev/null\n:0"
  echo "* $regex" ; echo /dev/null ; } > $tmpfile
echo "$txt" | procmail $tmpfile 2>&1 |
sed -n -e '/^procmail: Invalid regexp/{s/^[^ ]* //p;q;}' \
       -e 's/^procmail: \(\(No m\|M\)atch\) .*/\1/p'
rm $tmpfile
