#!/bin/sh
# $Header: /aba/home/staff/jon/src/fpick/RCS/fpick,v 1.5 1997/06/17 00:42:50 jo
n Exp jon $
#
# fpick script.  Mh utility
#
# See usage info in "help" variable below
#
# If you name the script something other than "fpick" (with a link, for
# example), you can make a different MH profile entry and set switches
# for that flavor of fpick'ing.  For example, if you make an "errpick"
# link to always search for errors, add an MH profile entry like this:
#   errpick: -subject "Error:" -seq errors
#
# For the folder skip processing to work correctly, a version of egrep
# with a "-v" option is required.
#
# Written by    Jon Eaves       <jon@aba.net.au>
#               Bill Wohler     <wohler@newt.com>
#            &  Jerry Peek      <jpeek@jpeek.com>
#

#
# Initializations (internal variables that need to be set to something).
#
cmd=`basename $0` || exit 1             # name by which command called

# initialise
found=0
folders=
folargs="-noheader -nopack -nototal"    # override user's MH profile
pickargs=

# NOTE : *** This may need to be modified ***
# MH may or may not be installed in /usr/local/bin, modify as appropriate
# for your environment
MHPATH=/usr/local/bin
PATH=$MHPATH/mh:$PATH           # be sure we use stock MH commands

# save current folder
current="+`folder $folargs -fast`"
# make sure that if I'm interrupted, then I'll restore properly
trap "folder $folargs -fast $current > /dev/null ; exit" 1 2 15

#
# Defaults (variables that may be affected by arguments).
#
mkeep=                          # do I keep the current contents ?
skeep=                          # do I keep the current sequence ?
result=+fpick                   # result folder (default +fpick)
sequence=fpick                  # result sequence (default fpick)
print=yes                       # do I display results ? (default yes)
fskip=                          # list of folders to skip
check=yes                       # do I check the pick arguments

#
# Grab MH profile arguments (if any); add to front of command-line args.
#
# The ${1+"$@"} preserves quoting and works around a problem in old shells.
# Set x and shift it away; this makes sure "set" doesn't list all variables:
profargs=`mhparam $cmd`
if [ -n "$profargs" ]
then
        set x `mhparam $cmd` ${1+"$@"}
        shift
fi

#
# parse arguments
#
while [ $# -gt 0 ]
do
    case "$1" in
        -help)  do_help=yes; break ;;
        -mk*)   mkeep=yes ;;
        -nomk*) mkeep= ;;
        -sk*)   skeep=yes ;;
        -nosk*) skeep= ;;
        -ch*)   check=yes ;;
        -noch*) check= ;;
        -fsk*)  shift; fskip="$1";;
        -o)     case "$2" in
                +?*) shift; result="$1";;
                *)   echo "$cmd: missing '+' in -o folder name" 1>&2
                     do_help=yes        # will show help and exit (0 status)
                     break
                     ;;
                esac
                ;;
        -seq*)  shift; sequence="$1";;
        -v*)    print=yes ;;
        -nov*)  print= ;;
        [+@]*)  folders="$folders $1";;
        *)      pickargs="$pickargs '$1'";;
    esac
    shift
done

if [ "$do_help" = yes ]
then
        # Show help message (note: includes some variables).  Then exit.
        # (Some shells don't have "<<-"; if so, replace with "<<" and dedent:)
        cat <<- END_HELP
        usage: $cmd [switches] pick-switches [+folder ...]

        switches are:
          -help:           prints usage message
          -[no]ch(eck):    [don't] check for valid pick-switches
          -fsk(ip) "f1|f2" folders *not* to search; separate names with "|"
          -[no]mk(eep):    [don't] preserve existing messages in $result folder
          -[no]sk(eep):    [don't] preserve existing "$sequence" sequence
          -o +outfolder:   link messages into +outfolder instead of $result
          -seq(uence) seq: save messages in sequence "seq" instead of "$sequenc
e"
          -[no]v(erbose):  after picking, [don't] scan $result folder

        pick-switches are the switches you want to search with.  For example:
          -before "Oct 19" -and -after "Oct 10"

        Searches entire folder tree unless you give +folder starting point(s).

        ${profargs:+profile: $profargs}
        Version: \$Id: fpick,v 1.5 1997/06/17 00:42:50 jon Exp jon $
        END_HELP
        exit 0
fi

# Match start of an option & at least one more argument in single quotes, like:
#       '-search' 'foo'
#

if [ "$check" = "yes" ]
then
    case "$pickargs" in
    *\'-[a-z]*\'*\'*\'*) ;;
    *) echo "$cmd: missing 'pick' arguments.  For help, use '$cmd -help'." 1>&2
       exit 1
       ;;
    esac
fi

#
# If $result folder exists, clear it out (if no -mkeep switch); else create it:
#
if [ -d "`mhpath $result`" ]
then
    test "$mkeep" != yes && rmm all "$result" >/dev/null 2>&1
else
    folder "$result" || exit 1
fi

#
# get a list of folders, not including the '$result' folder
# or anything specified in the -fskip option
#
if [ -n "$fskip" ]
then
    prune="^(+($fskip)|$result)\$"
else
    prune="$result"
fi

#
# set a fence-post so that the 'for' loop below will actually
# work.  Cludgy, but saves having to worry about repeating code
#
if [ "$folders" = "" ]
then
    folders="X-END"
fi

for rootFolder in $folders
do
    if [ "$rootFolder" = "X-END" ]
    then
        rootFolder=""
    fi

    for f in `folders -fast -recurse $rootFolder | egrep -v -e "$prune"`
    do
        #
        # remove all from the fpick sequence in the folder unless -skeep set
        #
        test "$skeep" != yes && mark +$f -seq "$sequence" -delete all 2>/dev/nu
ll
        #
        # find all the matches
        #
        frob="`eval pick $pickargs +$f -sequence $sequence -nolist -nozero 2>/d
ev/null`"
        if [ -n "$frob" -a "$frob" != "0" ]
        then
            found="1"
            #
            # show the number of matches in the folder
            # and add to the 'fpick' folder
            #
            echo "$f:$frob"
            refile -src "$f" -link "$sequence" "$result"
        fi
    done
done

#
# if there were any matches, sort them; optionally, show them
#
if [ "$found" = "1" ]
then
    sortm "$result" > /dev/null 2>&1
    test "$print" = yes && scan "$result"
fi

#
# restore current folder
#
folder $folargs -fast "$current" > /dev/null

