#!/bin/sh
# $Id: mimecat 1.0 1994/12/10 12:47:21 jerry book3 $
### mimecat - send content of an MH MIME message to standard output
### Usage: mimecat [-part nn]
##
##  THIS SCRIPT RUNS mhn -store TO OUTPUT MESSAGE CONTENT TO THE
##  STANDARD OUTPUT.  FOR MULTIPART MESSAGES, GIVE THE -part SWITCH.
##  IF THE SCRIPT STORES PARTS THAT AREN'T LISTED IN ITS $profile
##  VARIABLE (SEE BELOW), IT WARNS YOU.
##
##  BECAUSE mhn -store UN-ENCODES THE MESSAGE, THIS IS AN EASY WAY
##  TO UN-DO quoted-printable AND base64 ENCODING.
# 
#                             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.


mhnout=/tmp/MIMECATm$$
profile=/tmp/MIMECATp$$
stat=1  # DEFAULT EXIT STATUS; RESET TO 0 FOR NORMAL EXITS
trap 'rm -f $mhnout $profile; exit $stat' 0
trap 'echo "$0: interrupted.  Quitting early..." 1>&2; exit' 1 2 15

# THE MH 6.8.3 VERSION OF mhn -store SEEMS TO HAVE A BUG:
# A mhn-store- PROFILE ENTRY OF "-" (DASH) IS SUPPOSED TO WRITE A
# CONTENT TO STANDARD OUTPUT.  BUT IT DOESN'T; IT MAKES A FILE
# NAMED "-".  WORK AROUND THE PROBLEM BY MAKING A PROFILE STRING
# THAT RUNS "| cat".  IF YOUR VERSION OF mhn WORKS RIGHT, REPLACE
# THE LINE BELOW WITH output='-' AND CHANGE THE grep -v COMMAND.
output='| cat'

# CONFIGURE FOR TYPES YOU WANT TO BE ABLE TO OUTPUT.
# TYPES NOT LISTED WILL BE STORED USING RULES IN YOUR OTHER PROFILE(S).
# NOTE: SHELL EXPANDS SOME METACHARACTERS (LIKE ` AND $) IN HERE:
cat > $profile << ENDPROFILE
mhn-store-text/plain: $output
mhn-store-text/enriched: $output
mhn-store-text/richtext: $output
mhn-store-application/octet-stream: $output
mhn-store-application/postscript: $output
ENDPROFILE

# STORE PARTS IN $profile -- AND MAYBE OTHER PLACES TOO.  CAPTURE mhn STDERR:
MHN=$profile mhn -store -noverbose -noauto ${1+"$@"} 2>$mhnout

# CHECK mhn OUTPUT FOR PROBLEMS.  TYPICAL OUTPUT:
#   1. storing msg 105 part 1.1 using command (cd /tmp;  cat)
#   2. storing message 105 part 2 as file /tmp/105.2.basic
#
# IF ANY LINES DON'T MATCH LINE 1 ABOVE, SHOW THEM.
# ELSE SET A ZERO (SUCCESS) EXIT STATUS:
grep -v "^storing m.*using command (cd.*cat)" $mhnout 1>&2 || stat=0
