#! /bin/sh
# $Id: mysend,v 1.3 1994/09/06 12:14:16 jerry book3 $
### mysend -- sendproc that adds signature (or anything) to mail
### Usage (in MH profile):     sendproc: /xxx/yyy/mysend
##
##  INSTALL THIS SCRIPT AS YOUR sendproc, whatnow WILL RUN
##  IT AFTER YOU TYPE send OR push AT THE What now? PROMPT.
##  THE SCRIPT PARSES ALL THE ARGUMENTS YOU TYPE AT THE
##  What now? PROMPT (IF ANY) AND LOOKS FOR -signature OR
##  -nosignature (OR THE ABBREVIATIONS -sig AND -nosig).
##  ALSO, -face ADDS A X-Face: COMPONENT FROM THE ~/.face FILE;
##  -noface STOPS THE FACE IF IT'S BEEN SET AS THE DEFAULT.
##
##  IF THE SCRIPT FINDS THOSE FLAGS, IT SETS A FLAG VARIABLE;
##  THE REST OF THE ARGUMENTS, INCLUDING THE DRAFT PATHNAME
##  (ADDED BY whatnow) ARE PASSED TO THE *REAL* MH send COMMAND.
##
##  NOTE: whatnow RUNS THIS SCRIPT; IT DOES NOT PASS THIS SCRIPT
##  ANY send: ARGUMENTS FROM YOUR MH PROFILE.  (YOU CAN GET mysend:
##  ARGUMENTS FROM THE MH PROFILE BY USING THE mhparam SCRIPT.)
##
##  BEFORE IT ADDS YOUR SIGNATURE TO THE DRAFT, mysend CHECKS
##  CHECKS THE $mhdist ENVIRONMENT VARIABLE TO BE SURE YOU AREN'T
##  USING dist (YOU CAN'T ADD ANY TEXT TO THE BODY OF A dist'ED
##  MESSAGE).  THEN IT CHECKS TO SEE WHETHER YOU WANT A SIGNATURE
##  (YOU CAN HARD-CODE THE DEFAULT ON THE dosig= LINE OF THE SCRIPT,
##  BY THE WAY).  IF YOU DO, IT APPENDS YOUR $HOME/.signature FILE
##  TO THE DRAFT WITH A LINE OF TWO DASHES AND A SPACE (-- ) BEFORE.
##
##  FINALLY, THE SCRIPT execS THE REAL MH send COMMAND.
##
##  NOTE: THERE SEEMS TO BE A BUG IN MH 6.7.2 (AT LEAST) THAT WON'T
##  PASS THE -dist SWITCH TO THE post COMMAND IF AN EXTERNAL
##  sendproc (LIKE THIS!) IS USED.  THE RESULT IS ERRORS LIKE
##  THESE IF YOU USE THIS sendproc DURING THE dist COMMAND:
##      What now? send
##      post: illegal header line -- Resent-To:
##      ...
##  THE mydist SHELL SCRIPT WORKS AROUND THIS PROBLEM.
##  IT'S A FRONT-END dist SHELL SCRIPT THAT MAKES A
##  TEMPORARY COPY OF YOUR MH PROFILE WITHOUT ITS
##  sendproc: ENTRY.  UGLY... BUT WORTH IT IF YOU USE dist.
##
##  NOTE: IF YOU RENAME THIS, DON'T CALL send; whatnow WON'T USE IT.
# 
#                             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.


mh=/usr/local/mh            # WHERE MH COMMANDS LIKE send LIVE
doface=n                    # DEFAULT: NO FACE
dosig=n                     # DEFAULT: NO SIGNATURE
face=$HOME/.face            # FACE FILE, INCLUDING X-Face:
signature=$HOME/.signature  # SIGNATURE FILE TO READ

# PARSE COMMAND LINE, GRAB OUR OPTIONS.
# FIRST ARGS ARE OPTIONS; LAST IS DRAFT FILENAME:
while :
do
    case "$1" in
    "")         break ;;            # ALL DONE
    -sig*)      dosig=y ;;
    -nosig*)    dosig=n ;;
    -face)      doface=y ;;
    -noface)    doface=n ;;
    *)          args="$args $1" ;;  # FOR send
    esac
    draft="$1"  # EVENTUALLY THIS GETS DRAFT PATHNAME
    shift
done

# NO EDITS IF $mhdist ENVARIABLE IS 1 (DRAFT IS A dist HEADER):
case "$mhdist" in
1)  ;;
*)  # BEFORE DOING EDITS, EXIT IF DRAFT ISN'T READY:
    if [ ! -w "$draft" -o ! -r "$draft" ]; then
        echo "$0 quitting: unwritable or unreadable '$draft'" 1>&2
        exit 1
    fi
    # SET UP FOR ANY sed EDITS BY BUILDING $sedcmd EDITING COMMAND:
    case "$doface" in
    y)  if [ -r "$face" ]; then
            # WE CAN'T KNOW IF ANY HEADER COMPONENTS ARE MULTI-LINE
            # OR IF MESSAGE HAS To:, cc: ETC.  SO ADD FACE TO TOP:
            sedcmd="${sedcmd} -e '1r $face'"
        else
            echo "$0 quitting: unreadable '$face'" 1>&2
            exit 1
        fi
        ;;
    esac
    # DO ANY sed EDITS.  (USE sed BECAUSE ed BOMBS ON HUGE FILES.)
    # CAN TEST ALL sed-EDITING VARIABLES AT ONCE (case "$var1$var2")
    case "$doface" in
    *y*)    temp=/tmp/MYSEND$$
            # sed COMMAND MAY HAVE QUOTES IN IT, SO USE eval:
            eval /bin/sed $sedcmd $draft > $temp || exit 1
            /bin/cp $temp $draft || exit 1
            /bin/rm -f $temp
            ;;
    esac
    # ADD SIGNATURE FILE:
    case "$dosig" in
    y)  if [ -r "$signature" ]; then
            # SOME echoS CHOKE ON DASHES, SO DO IT THIS WAY:
            /bin/cat - "$signature" << \ENDPRESIG >> $draft || exit 1
-- 
ENDPRESIG
        else
            echo "$0 quitting: unreadable '$signature'" 1>&2
            exit 1
        fi
        ;;
    esac
    ;;
esac
if $mh/send $args
then exit 0
else
    # send returned non-zero.  If "push" command was used,
    # try to ring bell (by making a control-G) and print warning
    # (there's no second chance; "whatnow" always exits after "push").
    case "$args" in
    *-push*)
        /bin/cat << END 1>&2
*******************************************************
`echo x | /bin/tr x '\07'`$0 WARNING:
    $mh/send $args
returned a non-zero status.  Check for an unsent draft.
*******************************************************
END
        ;;
    esac
    # If "push" not used, this will make "whatnow" prompt again:
    exit 1
fi
