#! /bin/sh
# $Id: pickthis 1.11 1997/06/06 04:16:35 jpeek book4 $
### pickthis - search messages (default: all) for one with same subject
### Usage: pickthis [+search-folder|@search-folder] [msgs to search]
##
##  pickthis GRABS THE Subject: HEADER FIELD FROM THE CURRENT MESSAGE,
##  STRIPS OFF ANY Re:, RE:, ETC. FROM THE FRONT OF THE SUBJECT,
##  THEN SEARCHES FOR OTHER MESSAGES WITH THAT SUBJECT.  
##  IT STORES THE LIST OF MESSAGES IN THE SEQUENCE NAMED picked
##  (SO YOU CAN USE THEM LATER); IT scanS THE SEQUENCE BEFORE IT QUITS.
##
##  BY DEFAULT, pickthis SEARCHES ALL MESSAGES IN THE CURRENT FOLDER.
##  WITH A LIST OF MESSAGES (EXAMPLE: pickthis cur:50), IT'LL SEARCH THOSE.
##  IF YOU NAME A FOLDER, IT'LL GET THE SUBJECT OF THE CURRENT MESSAGE
##  IN THE *CURRENT* FOLDER, THEN SEARCH FOR THAT MESSAGE IN THE NAMED
##  FOLDER.  FOR EXAMPLE, IF THE CURRENT MESSAGE SUBJECT IS "bar baz",
##  YOU CAN CHECK FOR MESSAGES WITH THE SAME SUBJECT IN YOUR "old"
##  FOLDER BY TYPING pickthis +old -- IF THERE ARE MATCHES, pickthis
##  WILL CHANGE YOUR CURRENT FOLDER TO +old, ELSE IT STAYS THE SAME.
##
##  BUG: pick WILL FIND A MESSAGE SUBJECT *CONTAINING* THE SUBJECT YOU'RE
##  LOOKING FOR.  FOR EXAMPLE, IF THE CURRENT MESSAGE SUBJECT IS "exmh",
##  pickthis WILL ALSO FIND MESSAGES WITH THE SUBJECT "xmh vs. exmh", ETC.
##  I DON'T KNOW HOW TO CHANGE THE WAY THAT pick WORKS. :-(
#
#   USE AT YOUR OWN RISK.  SEEMS TO WORK, BUT IT'S YOUR RESPONSIBILITY!
#   PLEASE TELL ME ABOUT BUGS AND FIXES: 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.


myname="`basename $0`"  # NAME OF THIS SCRIPT WITHOUT LEADING PATH

msg=`mhpath cur`
# IF mhpath RETURNED ZERO STATUS AND THE START OF A PATHNAME, OK.
# ELSE, COMPLAIN:
case "$?$msg" in
0/?*) ;;
*)  echo "$myname quitting: can't get current message?" 1>&2
    exit 1
    ;;
esac

# GET SUBJECT OF THIS MESSAGE WITHOUT ANY Re: ON FRONT.
# scan SQUEEZES MULTIPLE SPACES TO ONE SPACE, SO WE CAN'T USE IT. :-(
# THIS DOESN'T HANDLE MULTI-LINE SUBJECTS...
# ESCAPE METACHARACTERS LIKE [, ], ETC. FOR pick:
subj=`sed -n -e '
    1,/^$/ {
        /^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]:[    ]*/ {
            s///
            /^[Rr][Ee]:/ {
            :rezap
                    s/^[Rr][Ee]: *//
                    /^[Rr][Ee]:/b rezap
            }
            s/[][*.$^\\]/\\\\&/g
            p
            q
        }
    }' "$msg"`
# IF sed RETURNED ZERO STATUS AND AT LEAST ONE CHARACTER IN $subj, OK.
case "$?$subj" in
0?*) ;;
*)  echo "$myname quitting: can't find subject of current message?" 1>&2
    exit 1
    ;;
esac

# SEARCH FOR MESSAGE AND scan IF WE FOUND ANY.  DEFAULT TO all MSGS IN FOLDER.
# (THE WEIRD ${1+"$@"} GETS ANY ARGS AND WORKS AROUND A PROBLEM IN EARLY sh'S.)
if pick -subject "$subj" ${1+"$@"} -seq picked
then
    # THIS exitS WITH scan'S EXIT STATUS:
    scan picked
    exit
else
    exit 1
fi
