Listing 1: lptek, a frontend to the lp or lpr spooler command

#!/opt/bin/perl
#
# Example 1 (lptek)
#
# This is a frontend to the lp or lpr spooler command.
# Depending on the command line options, various small
# PostScript files are spooled ahead of the actual
# print job to set options on the printer itself. This
# script was written for use with a Tektronix Phaser III
# PXi color printer, but can be modified to work with
# any PostScript printer.
#
# See the info file for documentation. (Available on
# ftp.cps.msu.edu:/pub/prip/lees/sysadmin/printing.)
#
#-----------------------------------------------------
# John Lees, Systems Analyst, Lab Manager
# Pattern Recognition and Image Processing Laboratory
# Department of Computer Science, A714 Wells Hall,
# East Lansing, Michigan 48824-1027  USA
# lees@cps.msu.edu, http://web.cps.msu.edu/prip
#  M i c h i g a n   S t a t e   U n i v e r s i t y
#-----------------------------------------------------
# Copyright 1994 by the Board of Trustees of Michigan
# State University and made available according to the
# provisions of the Free Software Foundation's GNU
# General Public License. The GPL is available by
# anonymous ftp from prep.ai.mit.edu in the file
# /pub/gnu/COPYING-2.0, or from ftp.cps.msu.edu in
# the file /pub/prip/lees/sysadmin/GPL.
#-----------------------------------------------------
chop($date = `date`);
chop($host = `uname -n`);
$user = getlogin;
# ostype is a local script, available on ftp.cps.msu.edu
chop($os = `/bin/ostype`);
# Software supplied with printer.
$P = '/opt/tektronix/PS';

require "getopts.pl";
Getopts('P:bc:ehq:s:');

# We must have access to the PostScript files to prepend
# to set printer options.
if (! -d $P ) {
   die "Cannot access $P. Send mail to manager.\n";
   }

# Help message:
if ($opt_h) {
   print 'Version 22 Sep 1994. Usage: lptek [OPTIONS] [FILES]
Options for Tektronix Phaser III PXi color PostScript printer:
   -P<P> send to printer <P> (to stdout by default).
         Overrides the TEKPS environment variable.
   -c<C> where <C> is one of:
          b   adjust blue
          c   CMYK2CMY adjustment to dark colors
          d   simulate display (RGB) colors (default)
          n   no adjustment to bitmaps
   -q<Q> where <Q> is one of:
          s   standard quality color
          e   enhanced quality color
          p   premium quality color (default)
   -s<S> where <S> is one of:
          50   50 line 45 degree halftone screen
          60   60 line 54 degree halftone screen
          80   80 line 33 degree halftone screen
          f    Finepoint sharpening halftone (default)
Use "info printing m printers" for complete information.
';
   exit;
   }

# Figure out whether we are sending this to the printer or
# to stdout and set up accordingly. If there is no -P option,
# check for TEKPS environment variable.
if (! $opt_P) {
   if ($ENV{'TEKPS'}) {
      # Sending to a printer.
      $opt_P = $ENV{'TEKPS'};
      } }
if ($opt_P) {
   if ($os eq 'SunOS-4') {
       open(LP, "| lpr -P$opt_P") ||
          die "Cannot open pipe to printer \"$opt_P\".\n";
       }
   else {
       open(LP, "| lp -d$opt_P") ||
          die "Cannot open pipe to printer \"$opt_P\".\n";
      } }
else {
   # Make stdout unbuffered.
   $opt_P = 'stdout';
   select((select(STDOUT), $| = 1)[0]);
   open(LP, '>-');
   }

# For the accounting record:
$options = "$opt_P,";
$options .= $opt_b ? ' -b' : '';
$options .= $opt_c ? " -c$opt_c" : '';
$options .= $opt_e ? ' -e' : '';
$options .= $opt_q ? " -q$opt_q" : '';
$options .= $opt_s ? " -s$opt_s" : '';

#--- Color adjustment.
# b   adjust blue
# c   CMYK2CMY adjustment to dark colors
# d   simulate display colors
# n   no adjustment to bitmaps
if ($opt_c) {
   if (index("bcdn", $opt_c) < 0) {
      print STDERR "\"-c$opt_c\" is invalid, ignoring.\n";
      }
   else {
      $C = substr($opt_c,0,1);
      } }

$F = "$P/display.ps";
COLOR: {
   ($C eq "b") && ($F = "$P/blueadj.ps", last COLOR);
   ($C eq "c") && ($F = "$P/cmyk2cmy.ps", last COLOR);
   ($C eq "d") && ($F = "$P/display.ps", last COLOR);
   ($C eq "n") && ($F = "$P/noimage.ps", last COLOR);
   }
open(F, "$F") ||
   die "Could not open $F\n";
while (<F>) {
   print LP;
   }

#--- Disable black substitution.
$F = "$P/blacksub.ps";
if ($opt_b) {
   $F = "$P/noblksub.ps";
   }
open(F, "$F") ||
   die "Could not open $F\n";
while (<F>) {
   print LP;
   }

#--- Install extended error handler.
if ($opt_e) {
   $F = "$P/tekehand.ps";
   open(F, "$F") ||
      die "Could not open $F\n";
   while (<F>) {
      print LP;
      } }

#--- Quality.
# d   draft quality black & white
# s   standard quality color (default)
# e   enhanced quality color
# p   premium quality color
if ($opt_q) {
   if (index("dsep", $opt_q) < 0) {
      print STDERR "\"-q$opt_c\" is invalid, ignoring.\n";
      }
   else {
      $Q = substr($opt_q,0,1);
      } }

$F = "$P/premium.ps";
QUALITY: {
   ($Q eq "d") && ($F = "$P/draft.ps", last QUALITY);
   ($Q eq "s") && ($F = "$P/standard.ps", last QUALITY);
   ($Q eq "e") && ($F = "$P/enhanced.ps", last QUALITY);
   ($Q eq "p") && ($F = "$P/premium.ps", last QUALITY);
   }
open(F, "$F") ||
   die "Could not open $F\n";
while (<F>) {
   print LP;
   }

#--- Halftone screen.
#  50   50 line 45 degree halftone screen
#  60   60 line 54 degree halftone screen
#  80   80 line 33 degree halftone screen
#  f    Finepoint sharpening halftone
if ($opt_s) {
   if (index("506080f", $opt_s) < 0) {
      print STDERR "\"-s$opt_s\" is invalid, ignoring.\n";
      }
   else {
      $S = substr($opt_s,0,2);
      } }

$F = "$P/finepnt.ps";
SCREEN: {
   ($S eq "50") && ($F = "$P/adobescr.ps", last SCREEN);
   ($S eq "60") && ($F = "$P/screen60.ps", last SCREEN);
   ($S eq "80") && ($F = "$P/screen80.ps", last SCREEN);
   ($S eq "f") && ($F = "$P/finepnt.ps", last SCREEN);
   }
open(F, "$F") ||
   die "Could not open $F\n";
while (<F>) {
   print LP;
   }

#----------
# At this point we are done prepending PostScript code
# and are ready to tack on the file (or stdin).
#----------
$first = 1;
$pages = 1;

# Files or stdin?
if (@ARGV > 0) { # file(s)
   -f $ARGV[0]
      || die "Cannot open input file \"$ARGV[0]\".\n";
   $files = join(' ', @ARGV);
   open(INPUT, "cat $files |");
   }
else { # stdin
   open(INPUT, '-');
   }

# Now read the print job.
while (<INPUT>) {
   if ($first) {
      /^%!/
         || die "Not PostScript!\n";
      $first = 0;
      }
   /^%%Pages:\s([0-9]*)$/ && do {
      $pages = $1;
      };
   print LP;
   }
close INPUT;
close LP;

#----------
# Try to do accounting.
#----------
open(ACC,
   ">>/home/pixel/u40/lees/Cron/Pac/Acct/pripTekPS")
      && do {
      printf(ACC "%7.2f %s:%s - %s  %s\n",
         $pages, $host, $user, $date, $options);
      };
close ACC;

#----------
# Done.
exit 0;
