#! /bin/sh
# $Id: xmhprint,v 1.6 92/07/25 07:12:47 jerry book2 $
### xmhprint - print command for xmh
### Usage (in X resource): xmh*PrintCommand: xmhprint [-cmd_num]
##
##  THE PRINTER SUPPORT WITH xmh ISN'T VERY GOOD.
##  IT HANDS ALL YOUR MESSAGE FILES TO A PRINTER COMMAND AT ONCE.
##  IT DOESN'T FILTER THEM TO REMOVE UNINTERESTING HEADER LINES.
##  YOU CAN'T USE A PIPE AS PART OF THE PRINTER COMMAND BECAUSE xmh 
##  PUTS THE MESSAGE FILENAMES AT THE END OF THE PRINTER COMMAND LINE.
##  
##  THIS PROGRAM, xmhprint, TAKES MESSAGE FILENAMES(S) FROM THE
##  COMMAND LINE, AS WELL AS (MAYBE) ONE OPTION.
##  THE SHELL SCRIPT GETS THE MESSAGE FILENAMES FROM xmh, AND IT CAN
##  PROCESS THEM ANY WAY YOU WANT IT TO.
##  THE OPTIONS LET YOU CHANGE YOUR PRINT SETUP EASILY--INSTEAD OF
##  STORING A COMPLICATED xmh*PrintCommand IN THE RESOURCE MANAGER,
##  JUST CHANGE THE OPTION ON THE xmhprint COMMAND LINE LIKE THIS:
##      xmh*PrintCommand: xmhprint -p2
##  
##  YOU CAN CUSTOMIZE THIS SCRIPT TO FIT YOUR NEEDS.
##  ADD mhl(1) TO CLEAN UP THE MESSAGE HEADERS, USE OTHER PRINTERS,
##  USE A POSTSCRIPT FILTER... GO WILD!
##
##  SET YOUR SYSTEM PRINTER IN THE printcmd VARIABLE.  STORE ANY
##  $printcmd OPTIONS THERE, TOO -- OR SET AN ENVIRONMENT VARIABLE LIKE
##  $PRINTER (IF YOUR SYSTEM HAS THAT) BEFORE STARTING xmh.
##  
##  lpr AND lp PRINT ERROR MESSAGES TO THE STANDARD OUTPUT INSTEAD
##  OF THE STANDARD ERROR ON SOME VERSIONS OF UNIX!  xmh R4 AND R5
##  WILL SHOW YOU stderr MESSAGES IN A DIALOG BOX.  SO, TO MAKE THAT
##  WORK RIGHT WITH $printcmd, THE SCRIPT USES $printcmd 1>&2
##  TO MERGE ALL PRINTER stdout ONTO stderr.
# 
#                             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.


errsubj="xmh*PrintCommand print ERROR" # FOR MAILED ERRORS
printcmd="/usr/ucb/lpr"     # CAN ADD OPTIONS INSIDE QUOTES

# IN xmh RELEASE 3, MUST REDIRECT ALL OUTPUT AWAY FROM xmh.
# IN RELEASE 4 AND 5, STDERR (FD 2) GOES TO ERROR DIALOG BOX.
# UNCOMMENT THIS TO APPEND ALL OUTPUT TO FILE:
# errfile=$HOME/.xmh_printerrs
# exec >> $errfile 2>&1

# GET OPTION (xmh ALWAYS PUTS FILENAMES LAST):
case "$1" in
-p1) # JUST pr; PUTS PATHNAME IN HEADER.  NOTHING FANCY:
    shift
    pr "$@" | $printcmd 1>&2
    ;;
-p2) # SHOW SUBJECT IN pr HEADER OF EACH MESSAGE:
    shift
    for f
    do
        pr -h "`sed -n '/^[sS]ubject: / {
            s///p
            q
        }' $f`" $f
    done | $printcmd 1>&2
    ;;
"") echo "No filenames or command line arguments!?!" |
    mail -s "$errsubj" $USER
    exit 1      # xmh IGNORES THIS :-(
    ;;
*)  # DEFAULT: SEND ALL OPTIONS AND FILENAMES TO lpr:
    $printcmd "$@" 1>&2
    ;;
esac
