#! /bin/sh
# $Id: distprompter 1.10 1997/06/06 02:32:16 jpeek book4 $
### distprompter - replaces "prompter" for MH "dist" command
### Usage (in .mh_profile):    dist: -editor distprompter
##
##  BY DEFAULT, THE MH dist COMMAND USES prompter TO EDIT THE DRAFT
##  MESSAGE.  FOR dist, THAT'S NOT A GREAT CHOICE BECAUSE:
##      - IF YOU ACCIDENTALLY TYPE A BODY, THE MESSAGE CAN'T BE SENT
##      - YOU ALWAYS HAVE TO PRESS CONTROL-D TO SKIP THE BODY
##
##  distprompter IS AN EDITOR DESIGNED FOR dist.  IT READS THE
##  EMPTY HEADER THAT dist GIVES IT, LINE BY LINE.  IF A COMPONENT
##  IS EMPTY, IT PROMPTS YOU.  IF A COMPONENT IS FINISHED, IT DOESN'T
##  PROMPT.  IF A COMPONENT IS ILLEGAL (NOT Resent-xxx:), IT COMPLAINS.
##  WHEN IT'S READ THE HEADER, IT EXITS; YOU DON'T NEED CONTROL-D.
##
##  NOTE TO HACKERS: TABSTOPS ARE SET AT 4 IN THIS CODE
# 
#                             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`"
err=/tmp/DISTPRe$$ header=/tmp/DISTPRd$$
> $header
chmod 600 $header

# UNCOMMENT THE RIGHT LINE FOR YOUR UNIX:
echo="echo -n" nnl=         # BSD
# echo=echo      nnl="\c"   # SYSV
# echo="echo -n" nnl=   PATH=/usr/bin:$PATH; export PATH    # SunOS

stat=1  # DEFAULT EXIT STATUS; RESET TO 0 FOR NORMAL EXITS
trap 'rm -f $header $err; exit $stat' 0
trap 'echo "$myname: Interrupt!  Cleaning up..." 1>&2; exit' 1 2 15

if [ ! -w "$1" -o -z "$1" ]
then
    echo 1>&2 "$myname: quitting: missing or unwritable draft
    '$1'"
    exit
fi

# READ DRAFT (A COPY OF distcomps FILE) LINE-BY-LINE.
# ACT LIKE prompter, BUT EXIT AFTER WE'VE READ DRAFT FILE
# (WHEN YOU USE dist, THE DRAFT FILE IS ONLY A HEADER).
# read AT TOP OF LOOP GETS STDIN (FD 0), SO SAVE FD 0 NOW:
exec 4<&0   # SAVE ORIGINAL STDIN (USUALLY TTY) AS FD 4
while read label line
do
    case "$label" in
    [Rr]esent-?*:)
        case "$line" in
        ?*) # SHOW LINE ON SCREEN AND PUT INTO HEADER FILE:
            echo "$label $line"
            echo "$label $line" 1>&3
            ;;
        *)  # FILL IT IN OURSELVES:
            $echo "$label $nnl"
            exec 5<&0   # SAVE DRAFT FILE FD; DO NOT CLOSE!
            exec 0<&4   # RESTORE ORIGINAL STDIN
            read ans
            exec 0<&5   # RECONNECT DRAFT FILE TO STDIN
            case "$ans" in
            "") ;;  # EMPTY; DO NOTHING
            *)  echo "$label $ans" 1>&3 ;;
            esac
            ;;
        esac
        ;;
    ""|---*) # END OF HEADER
        echo "-------" 1>&3
        break   # PROBABLY NOT NEEDED...
        ;;
    *)  echo "$myname: illegal header component
        '$label $line'" 1>&2
        break
        ;;
    esac
done <$1 2>$err 3>$header

# IF THE ERROR FILE HAS SOMETHING IN IT, SHOW IT AND QUIT:
if [ -s $err ]
then
    /bin/cat $err 1>&2
    echo "$myname: quitting." 1>&2
else
    if /bin/cp $header $1
    then stat=0
    else echo "$myname: can't replace draft '$1'?" 1>&2
    fi
fi
exit
