#! /bin/sh
# $Id: scandrafts,v 1.8 1995/04/26 20:02:49 jerry book3 $
### scandrafts - scan MH draft folder; return to original folder if no -stay
### Usage: scandrafts [-stay] [scan arguments]
##
##  THIS SCRIPT IS NICE WHEN YOU'RE WONDERING WHAT'S IN YOUR DRAFT
##  FOLDER, OR YOU NEED TO WORK IN IT.
##  
##  BY DEFAULT, scandrafts SHOWS A LIST OF THE MESSAGES YOU'VE ALREADY
##  SENT (THESE MESSAGES ARE IN FILES WITH A COMMA OR POUND SIGN BEFORE
##  THEIR NAMES).  THEN, IT  scanS YOUR DRAFT FOLDER, AND POPS YOU BACK
##  TO YOUR CURRENT FOLDER.
##  
##  IF YOU USE THE -stay OPTION, scandrafts WILL START A SHELL WITH
##  BOTH THE CURRENT DIRECTORY AND CURRENT FOLDER IN THE DRAFT FOLDER.
##  THAT WAY, YOU CAN RESTORE ONE OF THE ALREADY-SENT MESSAGES AND/OR
##  refile IT TO ANOTHER FOLDER (IN CASE YOU FORGOT TO GIVE YOURSELF A
##  COPY WHEN YOU SENT IT).  OR, YOU CAN DO EXTENSIVE WORK ON THE
##  DRAFTS, MORE DIRECTLY THAN A SCRIPT LIKE recomp WILL LET YOU.
##  
##  HERE'S A DEMONSTRATION:
##  
##  $ scandrafts -stay 
##  Draft message(s) you've already sent:
##  
##  ,1:Subject: Re: SC or GA islands
##  ,5:Subject: Re: our previous message about banners dialups etc
##  ,6:Subject: Re: Can you help?
##  ,7:Subject: Out this morning
##  
##  To get them back, use 'mv'.
##  ===================================================================
##  Draft message(s) you haven't sent:
##  
##     1  03/07*To:alison@mvus.cs  Project status<<Alison, the project
##     2  empty
##     3+ 03/07*To:kx9cq@cornell.  Scientific Visualization Demo<<I re
##  
##      You'll be in a /usr/local/bin/ksh shell in the +drafts folder.
##      To quit, type control-d.
##  scandrafts> rmm 2 
##  scandrafts> mv ,6 6 
##  scandrafts> scan 
##     1  03/07*To:alison@mvus.cs  Project status<<Alison, the project
##     3+ 03/07*To:kx9cq@cornell.  Scientific Visualization Demo<<I re
##     6  03/04*To:warren          Re: Can you help?<<Warren, you aske
##  scandrafts> refile 6 +outbox 
##  scandrafts> ^D 
##  [folder +inbox now current]
##  $
# 
#                             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.


args=   # RESET IN CASE THERE'S AN args ENVIRONMENT VARIABLE
folopts="-fast -nolist -nototal -nopack"
mh=/usr/local/mh
pageprog=${PAGER-/usr/ucb/more} # DISPLAYS SCREEN-BY-SCREEN
stat=1  # DEFAULT EXIT STATUS; RESET TO 0 FOR NORMAL EXITS
temp=/tmp/SCANDRFTS$$
>$temp
chmod 600 $temp
trap 'rm -f $temp; exit $stat' 0 1 2 15

# IF -stay SWITCH IS SET, SET $stay TO y:
for arg
do
    case "$arg" in
    -stay)  stay=y ;;
    *)  args="$args $arg" ;;
    esac
done

# GET DRAFT FOLDER NAME:
draftfold="`$mh/mhparam draft-folder`" || {
    echo "`basename $0`: quitting: can't find your 'Draft-Folder'." 1>&2
    exit
}

$mh/folder $folopts -push +$draftfold >/dev/null || exit
folpath=`$mh/mhpath +$draftfold` || exit
cd $folpath || exit

# IF ANY UN-SENT DRAFTS, SHOW THEM; THEN, scan FOLDER.
# PIPE ALL OF IT TO PAGER SO NONE OF IT SCROLLS OFF SCREEN:
grep "^Subject: " [,#]*[1-9]* >$temp 2>/dev/null
(if test -s $temp
then
    echo "Draft message(s) you've already sent:
    "
    cat $temp
    echo
    case "$stay" in
    y) echo "To get them back, use 'mv'." ;;
    *) echo "To get them back, use 'cd $folpath' and 'mv'." ;;
    esac
    echo "============================================================="
    echo "Draft message(s) you haven't sent:
    "
fi
$mh/scan $args 2>&1) | $pageprog

# USE THEIR $SHELL, IF DEFINED... OTHERWISE, USE sh:
case "$stay" in
y) echo "
    You'll be in a ${SHELL-Bourne} shell in the +$draftfold folder.
    To quit, type control-d."
    PS1="scandrafts> " ${SHELL-sh}
    ;;
esac

# POP FOLDER BACK; PUT NEWLINE BEFORE MESSAGE:
echo "
[folder +`$mh/folder $folopts -pop` now current]"
stat=0
