#! /bin/sh
# $Id: recomp,v 1.9 1993/04/22 08:33:46 jerry book3 $
### recomp - re-compose a draft mesage in MH draft folder
### Usage: recomp [msgnum]
##
##  WHEN YOU TYPE q AT A What now? PROMPT, IT LEAVES THE DRAFT MESSAGE
##  WITHOUT SENDING IT.  IF YOU HAVE A DRAFT FOLDER, THE COMMAND LINE
##  YOU'D TYPE TO RE-COMPOSE THE DRAFT IS LONG, LIKE:
##      comp -use -draftm 3 -draftf +drafts -editor vi.
##  ALSO, IT CAN BE HARD TO REMEMBER THE DRAFT NUMBER--SO YOU HAVE TO
##  scan THE DRAFT FOLDER, THEN REMEMBER TO CHANGE YOUR FOLDER BACK.
##  
##  THIS SCRIPT HELPS WITH THAT.  IF YOU GIVE IT A MESSAGE NUMBER IN THE
##  DRAFT FOLDER, IT STARTS comp -use ON THAT MESSAGE WITH YOUR FAVORITE
##  EDITOR PROGRAM.  WITHOUT A MESSAGE NUMBER, recomp scanS THE DRAFT
##  FOLDER, THEN LETS YOU ENTER THE NUMBER OF THE MESSAGE YOU WANT TO
##  RE-COMPOSE AND STARTS comp -use.
##
##  WHEN YOU EXIT YOUR EDITOR, YOU GET THE USUAL What now? PROMPT.
# 
#                             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.


draftf=+drafts          # NAME OF DRAFT FOLDER
folopts="-fast -norecurse -nolist -nototal -nopack"
mh=/usr/local/mh        # WHERE MH PROGRAMS LIVE

# THIS SCRIPT CHANGES CURRENT FOLDER TO THE $draftf FOLDER.
# SET TEMPORARY CONTEXT FILE SO OTHER MH PROCESSES WON'T NOTICE CHANGE:
tempctx=/tmp/RECOMP$$
MHCONTEXT=$tempctx; export MHCONTEXT
stat=1   # DEFAULT EXIT STATUS; RESET TO 0 FOR NORMAL EXITS
trap '/bin/rm -f $tempctx; exit $stat' 0
trap 'echo "`basename $0`: Interrupt!  Cleaning up..." 1>&2' 1 2 15
$mh/folder $folopts $draftf >/dev/null || {
    echo "`basename $0`: quitting: problem with draft folder '$draftf'." 1>&2
    exit 1
}

case $# in
0)  # THEY DIDN'T GIVE MESSAGE NUMBER; SHOW THEM FOLDER:
    if $mh/scan
    then
        echo -n "Which draft message number do you want to re-edit? "
        read msgnum
    else
        echo "`basename $0`: quitting: no messages in your $draftf folder?" 1>&2
        exit
    fi
    ;;
1)  msgnum="$1" ;;
*)  echo "I don't understand '$*'.
    I need the draft message number, if you know it... otherwise, nothing.
    Usage: `basename $0` [msgnum]" 1>&2
    exit
    ;;
esac

$mh/comp -use -e ${VISUAL-${EDITOR-${EDIT-vi}}} -draftm $msgnum -draftf $draftf
stat=$?   # SAVE comp'S STATUS (IT'S USUALLY 0) FOR OUR exit
