#!/bin/sh

#
# This is the kedpr script.  It simplifies printing of ked
# files by cat'ing all of the files to the printer.
# In addition to this, the left, right, top, and bottom margins
# can be set using the -lm, -rm, -tm, and -bm command line parameters.
# The vertical spacing between the lines can be set using the -vs
# command line parameter.
# The font size can be set using the -fs command line parameter.
# Another command line parameter that can be set is the printer.
#
# An example might be:
#    kedpr -Pcrl_apple1 -tm 760
#
#    kedpr -fs 9 -tm 730 -vs 3 myfile
#
# The first example will set the top margin to 760 and send the files
# to the crl_apple1 printer.  If the -P<printer> command line
# parameter is not given, kedpr looks for the PRINTER environment
# variable for the name of the printer.
#
# The second example would set the font size to 9 points, set the top
# margin to 730, set the vertical spacing to 3 and print "myfile"
# instead of the default file.
#
# The vertical spacing value is added to the font size to get the
# actual vertical spacing used when printing out.
#
# The default values for the margins, vertical spacing, font size, the
# default print file, and the printer can be set below.
#
# Other parameters you might need to change are:
# HEADER  - the directory and filename of the "Header" file.
# FONT    - the directory and filename of the "Munjo" file.
# FILE    - the default file that kedpr prints.
#
# This script will print the default file stored in the FILE
# parameter if it does not find a file name on the command line.
#
# mleisher@nmsu.edu (Mark Leisher)
# Wed Jun 19 06:00:40 1991
#

TMPFILE="/usr/tmp/kedjob.$$"
trap 'rm -f $TMPFILE' 1 2 3 4 5 6 7 8 10 12 13 14 15 16

# This is the name of the script used later for messages.

PROGNAME=`basename $0`

# These are the directories in which the ked Header and Munjo files
# are located.  It is assumed that the PrintOut file is in the
# current directory.

HEADER="/usr/local/lib/ked/Header"
FONT="/usr/local/lib/ked/Munjo"
FILE="./PrintOut"

# This sets the printer.  Change the printer name to be one
# of your printers.
if [ -z "$PRINTER" ]; then
   PRINTER=apple
   export PRINTER
fi

# These are the default margin settings for ked print outs.

# Left margin
LM=20

# Right margin
RM=550

# Top margin
TM=760

# Bottom margin
BM=50

# Default font size for ked print outs.
FS=10

# Default vertical spacing for ked print outs.
VS=2

while [ $# != 0 ]; do
      case "$1" in
           -P*) PRINTER=`echo $1 | sed -e 's/-P//'`
                echo "Printer is set to $PRINTER"
                export PRINTER;;
           -lm|-LM) shift; LM=$1;;
           -rm|-RM) shift; RM=$1;;
           -tm|-TM) shift; TM=$1;;
           -bm|-BM) shift; BM=$1;;
           -fs|-FS) shift; FS=$1;;
           -vs|-VS) shift; VS=$1;;
           *) FILE=$1

              # This part checks to see if the file to be printed
              # really exists.
              if [ \! -s $FILE ]; then
                 echo $PROGNAME: \"$FILE\" not found
                 exit 1
              fi

              # This part checks to see if the file to be printed
              # is a file generated by ked.
              KEDFILE=`egrep "^%! Ked" $FILE`
              if [ "$KEDFILE" = "" ]; then
                 echo $PROGNAME: \"$FILE\" doesn\'t seem to be a ked file
                 exit 1
              fi;;
      esac
      shift
done

# Replace the margin, font size, and vertical spacing values in the
# file to be printed.

sed -e 's/^\/LM.*/\/LM '$LM' def/' -e 's/^\/RM.*/\/RM '$RM' def/' \
    -e 's/^\/TM.*/\/TM '$TM' def/' -e 's/^\/BM.*/\/BM '$BM' def/' \
    -e 's/^\/FS.*/\/FS '$FS' def/' -e 's/^\/VS.*/\/VS { FS '$VS' add } def/' \
    $FILE > $TMPFILE

cat $HEADER $FONT $TMPFILE | lpr -h -P$PRINTER
rm -f $TMPFILE
exit 0
