#!/bin/sh
#-----------------------------------------------------------------------------
# pri: Print ASCII or PostScript files
#-----------------------------------------------------------------------------
#
#   Copyright (C) 1992-1994,1996,1999
#   Associated Universities, Inc. Washington DC, USA.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: casa-feedback@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#
#-----------------------------------------------------------------------------
# Usage: pri [-m mode] [-p paper] [-P printer] [-t type] [-w]
#            [file1 [file2 ...]]
#-----------------------------------------------------------------------------
# pri prints an ASCII or PostScript file in a variety of ways.
#
# Options:
#   -m:  Print mode for ASCII text files:
#           p   Portrait mode.
#           l   Landscape mode.
#           72  Two-column portrait mode for files with lines not
#               exceeding 72 characters in length.
#           80  Two-column landscape mode for files with lines not
#               exceeding 80 characters in length.
#
#   -p:  Paper type:
#           3 or A3       Metric A3
#           4 or A4       Metric A4
#           l or letter   Letter
#
#   -P:  Printer to use.
#
#   -t:  Input file type:
#           txt  ASCII text file.
#           ps   PostScript file.
#
#   -w:  Wait for the print queue to empty before submitting each job.
#
#   -*:  All other options are passed to lwf_a.
#
# Original: 1992/03/04 by Mark Calabretta, ATNF
# $Id$
#=============================================================================
# Parse arguments.
  MODE="p"
  PAPER=""
  TYPE=""
  QUAL=""
  FILES=""
  WAIT=""

  while [ "$#" -gt 0 ]
  do
     case $1 in
     -m*)
        case $1 in
        -m)
           shift
           MODE="$1"
           ;;
        *)
           MODE=`echo $1 | sed -e 's/^-m//'`
           ;;
        esac
        ;;
     -p*)
        case $1 in
        -p)
           shift
           PAPER="$1"
           ;;
        *)
           PAPER=`echo $1 | sed -e 's/^-p//'`
           ;;
        esac
        ;;
     -P*)
        case $1 in
        -P)
           shift
           PRINTER="$1"
           ;;
        *)
           PRINTER=`echo $1 | sed -e 's/^-P//'`
           ;;
        esac
        ;;
     -t*)
        case $1 in
        -t)
           shift
           TYPE="$1"
           ;;
        *)
           TYPE=`echo $1 | sed -e 's/^-t//'`
           ;;
        esac
        ;;
     -w)
        WAIT=1
        ;;
     -*)
        QUAL="$QUAL $1"
        ;;
     *)
        FILES="$FILES $1"
        ;;
     esac

     [ "$2" = "" ] && break

     shift
  done

# Set default printer if necessary.
  [ "$PRINTER" = "" ] && PRINTER="`prd`"

# Input from stdin?
  if [ "$FILES" = "" ]
  then
     FILES="/tmp/pri.1.$$"
     trap "rm -f $FILES ; exit" 0 1 2 3 15
     cat - > $FILES
     QUAL="$QUAL -n<stdin>"
  fi

# Query the aipsrc databases for the paper type.
  [ "$PAPER" = "" ] && PAPER=`getrc printer.$PRINTER.paper 2> /dev/null`
  case $PAPER in
  A4 | a4 | 4)
     PAPER=A4
     GVPAPER=-a4
     ;;
  A3 | a3 | 3)
     PAPER=A3
     GVPAPER=-a3
     ;;
  letter | l | quarto | q)
     PAPER=letter
     GVPAPER=-letter
     ;;
  *)
     echo "Unknown paper type: $PAPER" 1>&2
     exit 1
     ;;
  esac


# Print each file.
  for FILE in $FILES
  do
#    Try to deduce the file type if it was not specified.
     if [ "$TYPE" = "" ]
     then
        TYPEi=txt

        PSID=`sed -n 1,2p $FILE`
        set - $PSID dummy
 
        CTRLD=""
        case $1 in
        )
           CTRLD=1
           shift
           ;;
        %!*)
           CTRLD=1
           ;;
        esac
 
        case $1 in
        %!* | %!*)
           TYPEi=ps
           [ "$CTRLD" = 1 ] && echo "Warning: initial ^D ignored." 1>&2
           ;;
        esac
     else
        TYPEi="$TYPE"
     fi

     if [ "$WAIT" != "" ]
     then
        COUNT=0
        while [ "$COUNT" -lt 60 ]
        do
           prq | grep "Job" 2>&1 > /dev/null
           [ "$?" = 1 ] && break
           sleep 60
           COUNT=`expr $COUNT + 1`
        done

        echo "pri: Printing $FILE"
     fi

     case $TYPEi in
     ps)
        case $PRINTER in
        ghostview)
           ghostview $GVPAPER $FILE
           ;;
        *)
           lpr -P$PRINTER $FILE
           ;;
        esac
        ;;
     txt)
        case $MODE in
        p)
           LWFOPT="-i2.5 -p$PAPER $QUAL"
           ;;
        l)
           LWFOPT="-i1 -p$PAPER -l $QUAL"
           ;;
        72)
           case $PAPER in
           A4)
              LWFOPT="-i1 $QUAL -c2 -p$PAPER -s6"
              ;;
           A3)
              LWFOPT="-i1 $QUAL -c4 -p$PAPER -s6 -l"
              ;;
           letter)
              LWFOPT="-i1 $QUAL -c2 -p$PAPER -s6"
              ;;
           esac
           ;;
        80)
           case $PAPER in
           A4)
              LWFOPT="-i2 $QUAL -c2 -p$PAPER -s8 -l"
              ;;
           A3)
              LWFOPT="-i2 $QUAL -c2 -p$PAPER -s8"
              ;;
           letter)
              LWFOPT="-i2 $QUAL -c2 -p$PAPER -s7 -l"
              ;;
           esac
           ;;
        *)
           echo "Unknown mode: $MODE" 1>&2
           exit 1
           ;;
        esac

        case $PRINTER in
        ghostview)
           (trap "rm -f /tmp/pri.2.$$" 0 1 2 3 15 ; \
            lwf_a $LWFOPT $FILE > /tmp/pri.2.$$ ; \
            ghostview /tmp/pri.2.$$ ; \
            exit) &
           ;;
        *)
           lwf_a $LWFOPT $FILE | lpr -P$PRINTER
           ;;
        esac
        ;;
     *)
        echo "Unknown file type: $TYPEi" 1>&2
        exit 1
        ;;
     esac
  done


  exit 0
