#!/bin/sh
#---------------------------------------------------------------------------
#
#                       FILE:   rcvsearch
#                       Author: James R. Hamilton
#                       Date:   92.10.12 @ 10:37:51
#
#                       (Small edits: Jerry Peek, jpeek@jpeek.com, 97.09.11)
#
#   USAGE: Rcvsearch search_string maildelivery_program
#
#   WHERE: Search_string is any egrep legal string and maildelivery_program
#          is any arbitrary maildelivery problem.  The key problems with the
#          slocal/.maildelivery combination are the search strings are 1)
#          restricted to header entries only, and 2) restricted to literal
#          matches rather than using fully general regular expressions.  If
#          you were searching for root@jrh, you would mistakenly match 
#          beetroot@jrh as well.  A rcvsearch string of " root@jrh$" avoids
#          this possibility.
#
#   EXAMPLE: The .maildelivery string:
#
#         *,-,|,A,/usr/local/lib/mh/rcvsearch '^[^#]*\.x$' \
#             /usr/local/lib/mh/rcvstore +foundit
#
#         Will store all messages containing a line ending in ".x" without
#         any leading "#" characters.  Note that the .maildelivery string 
#         must not be continued accros multiple lines as I have here.  See 
#         the "slocal" and "mhook" man pages for more info. 
#
#   COMMENTS/FIXES TO: James Hamilton <jrh@jrh.gts.org>
#    (or: Jerry Peek <jpeek@jpeek.com> )
# 
#                             NO WARRANTY
# 
#   BECAUSE THIS PROGRAM IS AVAILABLE FREE OF CHARGE, THERE IS NO WARRANTY
# FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
# OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
# PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
# OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
# TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
# PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
# REPAIR OR CORRECTION.
# 
#   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
# WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
# REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
# INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
# OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
# TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
# YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
# PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGES.
#---------------------------------------------------------------------------

    #{ exec >/tmp/rcvsearch.out 2>&1; set -x; }         # Uncomment to debug
    PROG=`basename $0`
    if [ $# -lt 2 ] ; then
        echo "$PROG: USAGE: \"$PROG search_string maildelivery_program\"."
        exit 10                         # Exit handler not set, return fail.
    fi
    RC=20                               # Return code: Assume failure
    tmp=/tmp/$PROG.$$                   # Tmp file to store note.
    trap 'rm -f $tmp; exit $RC;' 0 1 2 3 14 15  # Clean up and return RC
    cat >$tmp                           # Save mail into tmp file.
    egrep "$1" $tmp >/dev/null || exit  # Search silently; abort on failure
    NewProg="$2"                        # NewProgram Name is Arg 2.
    shift; shift                        # get rid of $1 and $2.
    $NewProg "$@" <$tmp                 # Call the supplied program to process.
    RC=$?                               # Done, return code is last result.
    exit                                # Jump to exit handler.
