#! /bin/sh
# $Id: showpr,v 2.5 1995/04/26 20:02:49 jerry book3 $
### showpr - show MH message(s) with pr(1), custom heading, maybe mhl(1)
### Usage: showpr [fdr] [msgs] [-mhl] [-format 'mh-format-str'] [-pr 'pr-opts']
##
##  THE EASY WAY TO PRINT MESSAGES IN MH ISN'T ESPECIALLY GREAT.  YOU CAN
##  PRINT YOUR MAIL MESSAGES LIKE THIS (ASSUMING YOUR PRINTER PROGRAM IS
##  NAMED lpr), BUT ALL THE MESSAGES WILL BE RUN TOGETHER:
##      % show 23 24 29 | lpr
##  OR, YOU CAN USE pr(1) TO MAKE SIMPLE HEADERS (WITH THE FOLDER AND
##  MESSAGE NUMBER), PAGE NUMBERS, AND START EACH MESSAGE ON A NEW PAGE:
##      % show -showproc pr 23 24 29 | lpr
##  
##  THE showpr PROGRAM LETS YOU DO MORE.  IT CAN USE mhl, IF YOU WANT, TO
##  CLEAN UP THE MESSAGE BEFORE PRINTING.  YOU CAN CUSTOMIZE THE PAGE HEADING
##  -- INCLUDING THE MESSAGE SUBJECT, FOR INSTANCE.  AND YOU CAN PASS OPTIONS TO
##  pr, TO TELL IT HOW TO FORMAT YOUR MESSAGE.  FINALLY, YOU CAN STORE DEFAULT
##  showpr OPTIONS IN YOUR MH PROFILE, BECAUSE showpr CALLS THE mhprofile
##  SCRIPT TO GET THEM.
##  
##  THE COMMAND LINE USES mh-format STRINGS LIKE THE ONES YOU'D GIVE TO
##  THE scan COMMAND.  HERE ARE SOME EXAMPLES.  SOME OF THESE MIGHT LOOK
##  COMPLICATED, BUT REMEMBER THAT YOU CAN STORE ANY SET OF OPTIONS AS
##  DEFAULTS IN YOUR MH PROFILE:
##  
##  TO PRINT THE CURRENT MESSAGE, WITH THE MESSAGE SUBJECT IN EACH PAGE
##  HEADING (IF YOU HAVEN'T PUT ANY showpr OPTIONS IN YOUR MH PROFILE):
##      % showpr | lpr
##  TO PRINT THE LAST 3 MESSAGES, WITH THE MESSAGE SUBJECT IN EACH PAGE
##  HEADING; FORMAT THE MESSAGES WITH mhl; AND TELL pr TO USE
##  ITS OPTIONS -f (SEPARATE PAGES WITH FORMFEEDS) AND -l50
##  (MAKE PAGE LENGTH 50 LINES):
##      % showpr -mhl -pr '-f -l50' last:3
##  (YOU CAN SHORTEN THOSE OPTIONS TO -m AND -p, IF YOU WANT.)
##  
##  HERE'S MORE.  TO PRINT THE LAST 3 MESSAGES WITH mhl FORMATTING, WITH
##  THE "From" ADDRESS AND MESSAGE DATE IN THE HEADING, LIKE THIS:
##      Mar 11 08:49 1990  From: al@phlabs.ph.com  Date: 11/23/89  Page 2 
##  USE A COMMAND LINE LIKE THIS (SPLIT ONTO TWO LINES FOR READABILITY):
##      showpr -m -f 'from: %(friendly{from})  \
##          date: %(mon{date})/%(mday{date})/%(year{date})' last:3 | lpr
##  TO MAKE THAT EASIER, YOU COULD PUT THE FOLLOWING LINE IN
##  YOUR MH PROFILE (NOTE: ALTHOUGH IT'S SPLIT ONTO TWO LINES
##  HERE, YOU SHOULD PUT IT ALL ON *ONE* LINE IN YOUR MH PROFILE):
##      showpr: -m -f 'from: %(friendly{from})  \
##             date: %(mon{date})/%(mday{date})/%(year{date})'
##  THEN GET THAT FORMATTING AND PRINT YOUR MESSAGES WITH:
##      % showpr last:3 | lpr
##  
##  FINALLY, ABOUT mhl.  YOU CAN MAKE AN mhl FORM FILE NAMED mhl.showpr
##  AND PUT IT IN YOUR MH DIRECTORY (LIKE /xxx/yyy/Mail/mhl.showpr).
##  IF YOU DO, AND IF YOU USE THE -mhl OPTION, THEN showpr WILL
##  FORMAT YOUR MESSAGES WITH mhl -form mhl.showpr.
##  OTHERWISE, THE -mhl OPTION USES THE STANDARD mhl.format FILE.
# 
#                             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.


folopts="-nolist -nototal -nopack"
mh=/usr/local/mh            # WHERE MOST MH PROGRAMS LIVE
mhl=/usr/local/lib/mh/mhl   # WHERE mhl LIVES
myname="`basename $0`"
pr=/bin/pr
prwidth=55  # MAX WIDTH OF -h FIELD IN $pr + 5 FOR MSG NUM
scanopts="-noclear -noheader -noreverse"
stat=1  # DEFAULT EXIT STATUS, RESET TO 0 BEFORE NORMAL EXIT
usage="usage: $myname [fdr] [msgs] [-mhl] [-format 'mh-format-str'] [-pr 'pr-opts']"

# RESET "COMMAND LINE" PARAMETERS.  FIRST, AN x, WHICH WE
# shift AWAY, IN CASE THERE ARE NO OTHER PARAMETERS.
# THEN, MH PROFILE OPTIONS (IGNORE mhparam RETURN STATUS).
# LAST, ORIGINAL COMMAND LINE ARGS (WITH SHELL BUG PATCH):
eval set x `$mh/mhparam $myname` '${1+"$@"}'
shift

# PARSE set ARGS.  IF OPTIONS REPEATED, LAST ONES PREVAIL:
while :
do
    case "$1" in
    "") break ;;    # NO MORE ARGUMENTS
    [+@]*)  newfdr="$1" ;;
    -h*) # HELP:
        echo "$usage
        \$Revision: 2.5 $ \$Date: 1995/04/26 20:02:49 $"
        exit
        ;;
    -m*) # SET mhlopts AS FLAG TO USE mhl.
        mhlopts="-nobell -noclear -nofaceproc -nomoreproc"
        # USE mhl.showpr FILE, IF ANY:
        if test -r `$mh/mhpath +`/mhl.showpr
        then mhlopts="$mhlopts -form mhl.showpr"
        fi
        ;;
    -f*)
        case "$2" in
        "") echo "$usage
            (Missing string after '$1')." 1>&2
            exit
            ;;
        *)  format="$2"; shift ;;
        esac
        ;;
    -p*)
        case "$2" in
        "") echo "$usage
            (Missing string after '$1')." 1>&2
            exit
            ;;
        +*|-*)  propts="$2"; shift ;;
        *)  echo "$usage
            (Bad options after '$1')." 1>&2
            exit
            ;;
        esac
        ;;
    *)  msgs="$msgs $1" ;;
    esac

    shift
done

# SET FORMAT OF pr HEADER.  IF NO -format OPTION, AND IF NO
# "showpr:" LINE IN MH PROFILE, DEFAULT TO MESSAGE SUBJECT:
: ${format='%{subject}'}

# CHANGE FOLDER (IF USER GAVE ONE), GET NAME.
folder="`$mh/folder $folopts -fast $newfdr`"
cd `$mh/mhpath +` || exit   # cd TO MH DIRECTORY

# scan ALL MESSAGES; FEED TO LOOP.  IF NONE, DEFAULT TO cur:
scan -width $prwidth $scanopts -format "%(msg) $format" ${msgs-cur} |
while read msgnum heading
do
    # IF MESSAGE UNREADABLE, MH 6.6 scan PRINTS (TO stdout!)
    # LIKE THIS, WITH LEADING BLANKS UNLESS msgnum > 999:
    #   <msgnum>  unreadable
    # IF THERE ARE LEADING BLANKS, SOME sh'S WILL COPY BOTH
    # MESSAGE NUMBER AND unreadable INTO $heading, AND LEAVE
    # $msgnum EMPTY.  TRY TO CATCH BOTH CASES:
    case "$msgnum" in
    "") echo "$myname: skipping, message $msgnum $heading" 1>&2; continue ;;
    esac
    case "$heading" in
    unreadable) echo "$myname: skipping unreadable message '$msgnum'." 1>&2; continue ;;
    esac

    msgpath=$folder/$msgnum
    case "$mhlopts" in
    "")  $pr $propts -h "$heading" $msgpath || break ;;
    *) $mhl $mhlopts $msgpath | $pr $propts -h "$heading" || break ;;
    esac
done

stat=0
exit
