#!/bin/sh
# $Id: pickthread 0.1 1997/06/06 02:43:34 jpeek Exp $
### pickthread - find thread of messages related to this one
### Usage: pickthread [message] [folder]

# Could use a -range option: to choose the range of messages to search.

# by Jerry Peek.  ALPHA VERSION.  USE AT YOUR OWN RISK!
# 
#                             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`    # basename of this script ($0 with no pathname)

# Set default pick options.  Start with MH profile options (if any);
# options from this script will override because they're last:
pickopts="`mhparam $myname` -zero -list"

# Set default scan options:
scanopts="-noheader -noreverse -width 2000"

usage="Usage: $myname [message] [folder]"

# Check command line.  We should get just one message pathname (assume
# no spaces in it).  If mhpath returns nothing, or output has a newline
# in it, complain (make two-line error messages; second line indented):
case "`mhpath ${*-cur}`" in
"") echo "$usage
    (no source message to find a thread for)"
    exit 2
    ;;
*"
"*) echo "$usage
    (I can only find the thread for one message at a time.)"
    exit 2
    ;;
esac

# This also changes current folder if there was a [folder] argument.
# To be just like an MH utility, we shouldn't change current folder
# until we've found a thread.  But let's be lazy (and run faster):
msgids=`scan $scanopts -format '%{references} %{message-id}' ${*-cur}` || {
    echo "Usage: $usage
    (problem getting header fields from the source message)"
    exit 2
}

# Build a pick command line like this:
#   --message-id "AAAAA" -or --references "AAAAA" -or \
#   --message-id "BBBBB" -or --references "BBBBB" ...
# Hope that no message-ids have a doublequote (") in them;
# if they do, we could quote them but it'd be a pain.
for msgid in $msgids
do
    string="--message-id \"$msgid\" -or --references \"$msgid\""
    case "$lookfor" in
    "") lookfor="$string" ;;
    *)  lookfor="$lookfor -or $string" ;;
    esac
done

case "$lookfor" in
"") echo "$myname quitting: couldn't find a message-id to search for?!" 1>&2
    exit 2
    ;;
esac

# Run pick (with eval, to pick up quoting); check exit status and output:
msgs=`eval pick $pickopts $lookfor`
case "$?$msgs" in
0?*)    # pick had zero status and some output.  List messages and quit:
    echo $msgs
    exit 0
    ;;
0|*)    # pick had zero status and no output, or had non-zero status:
    echo "$myname: couldn't find a thread?!" 1>&2
    exit 1
    ;;
esac
