#!/bin/csh -f
#
# texprint - Print a TeX dvi file on any CS department printer.
# $Id: texprint.sh,v 1.9 1995/08/17 18:36:48 jpe Exp $
#
# Functions much like the old iptex; in fact, iptex should be linked
# to this for a while. Then, it should be a script that prints a
# message and calls texprint. Then just print a message. Then disappear.
#
# Understands laserwriter printers.

# For laserwriters, it calls
#          dvips (dvi->ps) and lpr (to spool the postscript)
#
# David Kotz August 1990
# Removed imagen support 8-19-91

# Find out our command name
set CMD_name = $0
set CMD = $CMD_name:t

# Handle interrupts and cleanup
set cleanfiles=()
onintr quit

set MAXCOPIES = 10	    # Do not allow more than $MAXCOPIES copies.

# The arguments for the Postscript print program
set psflags=()
set lprflags=()
	   
# We choose a printer based on the $PRINTER environment variable,
# or a -P flag in the arguments. For backward compatibility, we 
# set PRINTER from LASER if PRINTER does not exist and LASER does.

if (! $?PRINTER) then
	   if ($?LASER) then
	   		 setenv PRINTER $LASER
	   # else we don't know. Will give error later if no -P supplied.
	   endif
endif

# Define the usage error message
set usage = \
"Usage: $CMD [-Pprinter] [-p pages to print] \
         [-l] [-force] [-hold] [-help] [-q] [-s] \
         [-c copies] [-b] filename"

# Loop through the command-line arguments.

while ($#argv > 0)
	   switch ($argv[1])
	   case -p:       # next argument contains pages to use
	   		 shift
                if ($#argv == 0) then
                    echo $usage:q > /dev/tty
                    exit (1)
                endif
                set select=$argv[1]
                shift
	   		 breaksw

	   case -c:
	   		 shift argv
	   		 if ($#argv == 0) then
	   		 	    echo "Usage: -c <integer number of copies> ..." \
	   		 	    		  > /dev/tty
	   		 	    exit (1)
	   		 endif
	   		 # FALL THROUGH HERE
	   		 # (This actually allows "-c -c2", but what the heck...)

	   case -c?*:	    # number of copies
	   		 # Allow only up to $MAXCOPIES copies.
	   		 # Set ncopies to the number of copies.
    		
	   		 set ncopies = `echo $argv[1] | sed "s/^-c//"`
	   		 shift argv

	   		 set junk=`echo $ncopies | sed 's/[0-9]*//'`
	   		 if ("$ncopies" == "" || "$junk" != "") then
	   		 	    echo "Usage: -c <integer number of copies> ..." \
	   		 	    		  > /dev/tty
	   		 	    exit (1)
	   		 endif
	   		 if ($ncopies > $MAXCOPIES) then
	   		 	    echo -n "Sorry, I will not make more than" > /dev/tty
	   		 	    echo " $MAXCOPIES copies.  Try again." > /dev/tty
	   		 	    exit (1)
	   		 endif

	   		 echo -n > /dev/tty \
"Using the xerox machine to make copies is\
cheaper than using the laser printer to make copies.\
Do you REALLY want to use the laser printer to make $ncopies copies?\
(yes/no) => "
	   		 set answer = ($<)
	   		 if ( $answer =~ [Yy]* ) then
	   		 	    set psflags = ($psflags -c$ncopies)
	   		 else
	   		 	    echo Only one copy will be printed.
	   		 endif
	   		 breaksw
	
	   case -b*:	# Do not print a banner page.
	   case -J*:	# consistent with lpr
	   		 set lprflags=($lprflags -h)
	   		 shift argv
	   		 breaksw

	   case -P:	# Set the printer
	   		 shift argv
	   		 if ($#argv > 0) then
	   		 	    setenv PRINTER $argv[1]
	   		 	    shift argv
	   		 else
	   		 	    echo "Usage: -P <printer name> ..." > /dev/tty
	   		 	    exit (1)
	   		 endif
	   		 breaksw

	   case -P?*:	# Set the printer
	   		 setenv PRINTER `echo $argv[1] | sed 's/^-P//'`
	   		 shift argv
	   		 breaksw

	   case -help:	# Print the usage statement only.
	   		 echo $usage:q > /dev/tty
	   		 echo For more information, type \'man $CMD\'
	   		 exit (1)

	   case -hold:	# Hold output; feed the paper manually.
	   		 set psflags=($psflags -m)
	   		 shift argv
	   		 breaksw

	   case -l:	# Print in landscape mode
	   		 set psflags = ($psflags -t landscape)
	   		 shift argv
	   		 breaksw

	   case -s:	    # Use symlink for spooling large files.
	   		 set lprflags = ($lprflags:q -s)
	   		 shift argv
	   		 breaksw

	   case -force:	# Force the output to go
	   		 shift argv
	   		 breaksw

	   case -q:	# Run quietly 
	   		 set psflags = ($psflags -q)
	   		 shift argv
	   		 breaksw

	   # OBSOLETE options
	   case -#*:
	   		 echo Use -c instead of -#.
	   case -R:
	   case -d:
	   case -m:
	   case -r:	    # this would be nice for a no-reverse option later
	   case -si:
	   		 echo The $argv[1] option is obsolete, and is now ignored.
	   		 shift argv
	   		 breaksw

	   default:	# The file name is the dvi file.
	   		 if ("$argv[1]" =~ -*) then
	   		 	    echo Argument $argv[1] not a recognized option.
	   		 	    echo Taking $argv[1] to be the file name.
	   		 endif
	   		 if (! $?dviname) then
	   		 	    set dviname = $argv[1]
	   		 	    set dvi = $argv[1]
	   		 else
	   		 	    echo "Too many filenames." > /dev/tty
	   		 	    echo "The file name is the .dvi file from TeX."\
	   		 	    		  > /dev/tty
	   		 	    exit (1)
	   		 endif
	   		 shift argv
	   		 breaksw
    	endsw
end

# If dvi file was not set,  print an error.

if (! $?dvi) then
	   echo $usage:q > /dev/tty
	   echo "${CMD}: No file was specified, so nothing will be output." \
	   		 > /dev/tty
	   exit(1)
endif

# Add ".dvi" to file name, if necessary
if ( "$dvi" !~ ?*.dvi ) then
	   set dvi = "${dvi}.dvi"
	   set dviname = "${dviname}.dvi"
endif

# Be sure file is readable
if ( ! -r "$dvi" ) then
	   echo "${CMD}: cannot read file $dvi"  > /dev/tty
	   exit(1)
endif

# Select only some pages?
if ($?select) then
	   set dvitmp=/tmp/dviselect$$.dvi
	   set cleanfiles=($cleanfiles $dvitmp)
	   dviselect $select $dvi $dvitmp
	   set dvi = $dvitmp
endif


# Do we know the printer name?
if (! $?PRINTER) then
	   echo "${CMD}: Please specify a printer either"
	   echo "     1) in PRINTER environment variable,"
	   echo "  or 2) with -Pprinter argument."
	   exit 1
endif
	   		 
###################################################################
# Print the file
#
echo Sending $dvi to printer $PRINTER

dvips -P$PRINTER $psflags:q \
	 -o\!lpr" -P$PRINTER -J$dviname $lprflags:q" $dvi

if ($#cleanfiles > 0) then
	/bin/rm -f $cleanfiles
endif

exit(0)

quit:
    if ($#cleanfiles > 0) then
	/bin/rm -f $cleanfiles
    endif
    exit(1)
