#!/bin/csh -f

# modification added to support pshalf, Mike Jacknis

set initdir=/usr/athena/lib/init # so add will work
attach -n -q consult ; attach -n -q postscript #; add andrew

# I took out add andrew because ez2ps and ezprint are on the sys packs..?

# known bug: since full paths aren't used, aliases can be a problem.
# for example, if the user aliases lpr, then it will use aliased version
# is that true?  What to do?  (This depends on the platform and teh shell)

#      ^
# No it's not, adding -f prevents the loading o aliases in csh
# <Jerrad Pierce, belg4mit@mit.edu) 4/14/99

########################################################################
# lpr2: prints file(s) in duplex mode or tumble mode
#  See usage at end of file for more details.
#  only handles  ascii, ez, and ps files
########################################################################

#####
# July 12: Added noburst variable because -J overrides -h.  Blah.
# July 29: Fixed bugs with -h and multiple files.
#####

# Full path of PostScript header files to include
set PS_DIR = /mit/consult/lib/ps
set DUPLEX_PS = $PS_DIR/duplex.ps
set TUMBLE_PS = $PS_DIR/tumble.ps
set TRAY1_PS = $PS_DIR/bottomtray.ps

unset noclobber
set lpropts options file newfiles noburst
if (! $?LPR2OPT) then
	set LPR2OPT
endif

# Check to make sure there are at least SOME arguments.
if ($#argv == 0) then
    goto usage
endif

# Separate options from filenames
foreach arg ($argv $LPR2OPT)
    if ($?getnext) then
	set lpropts = ($lpropts $arg)
	unset getnext
    else if ($?getnext2) then
	set options = ($options $arg)
	unset getnext2
    else
      switch ($arg)
	case -e:
		set ezfiles
		breaksw
	case -d:
		set duplex
		breaksw
	case -m:
		set tumble
		breaksw
	case -b:
		set bottom
		breaksw
	case -4:
		set pshalf
		set options = ($options '-2r')
		breaksw
	case -2r:
		set pshalf
		set options = ($options '-2r')
		breaksw
	case -[aCJ]:
		set lpropts = ($lpropts $arg)
		set getnext
		breaksw
	case -[nNTe]:
		set options = ($options $arg)
		set getnext2
		breaksw
	case -[P#szuk]*:
		set lpropts = ($lpropts $arg)
		breaksw
	case -h:
		if ($noburst != 1) then
		  set noburst = 1
		else
		  set noburst = 0
		endif
		set lpropts = ($lpropts $arg)
		breaksw
	case -*:
		set options = ($options $arg)
		breaksw
	default:
		set file = ($file $arg)
      endsw
    endif
end

if ($?LPROPT) then
  foreach arg ($LPROPT)
    if ("$arg" == "-h") then
      if ($noburst != 1) then
	set noburst = 1
      else
	set noburst = 0
      endif
    endif
  end
endif

if ("$options" == "" && ! $?ezfiles) then
    set options = (-B -fCourier11 -q)
endif

# The meat: to activate any of these modes, we need to issue a PostScript
# command; to issue a PostScript command, we need a PostScript file.  We
# also have to retain the same print job, so we can't just send a file
# with the command in it, then print other stuff...it wouldn't work.
#
# we put the desired header files into temporary files then cat the postscript
# onto that.  We use ez2ps to create the postscript for ez files and enscript
# to create the postscript for ascii files.


if ("$file" == "") then
    set newfiles = /tmp/.lpr2.${USER}.stdin.ps
    if ($?tumble) then
	/bin/cp $TUMBLE_PS $newfiles
    else if ($?duplex) then
	/bin/cp $DUPLEX_PS $newfiles
    else
	/bin/rm -f $newfiles >& /dev/null
    endif
    if ($?bottom) then
	/bin/cat $TRAY1_PS >> $newfiles
    endif

    set file = stdin				# for the burst page
    if ($?ezfiles) then
	echo "Running ez2ps on stdin ..."
	if ($?pshalf) then
		echo "Using multips to shrink output..."
		ez2ps $options | /mit/save/bin/multips >> $newfiles
		else
		ez2ps $options >> $newfiles
	endif
    else 
	set oneof = /tmp/.lpr2.${USER}.stdin
	cat > $oneof
	if ("`head -1 $oneof`" !~ %\!*) then	# Was %!PS-Adobe-*
	    echo "Running enscript on stdin ..."
	    enscript $options -p - $oneof >> $newfiles
	else
	    set postscript
	    echo "Processing stdin as normal PostScript ..."
	    /bin/cat $oneof >> $newfiles
        endif
    endif
    if ($?pshalf && $?postscript) then
	unset postscript
	echo "Using pshalf to shrink output..."
        /mit/save/bin/pshalf < $newfiles > ${newfiles}.2
        mv -f ${newfiles}.2 $newfiles
    endif
    /bin/rm -f $oneof >& /dev/null
else
    foreach oneof ($file)
      if (-e $oneof) then
	set t1 = ${oneof:t}
	set thisfile = /tmp/.lpr2.${USER}.${t1}.ps
	if ($?tumble) then
	    /bin/cp $TUMBLE_PS $thisfile
	else if ($?duplex) then
	    /bin/cp $DUPLEX_PS $thisfile
	else
	    /bin/rm -f $thisfile >& /dev/null
	endif
	if ($?bottom) then
	    /bin/cat $TRAY1_PS >> $thisfile
	endif

	if ($?ezfiles) then
	    echo "Running ez2ps on $oneof ..."
		if ($?pshalf) then
			echo "Using multips to shrink output..."
			echo "  This may or may not work properly..."
			ez2ps $options $oneof | /mit/save/bin/multips >> $thisfile
			else
	    		ez2ps $options $oneof >> $thisfile
		endif
	else if ("`head -1 $oneof`" !~ %\!*) then	# Was %!PS-Adobe-*
	    echo "Running enscript on $oneof ..."
	    enscript $options -p - $oneof >> $thisfile
	else
	    set postscript
	    echo "Processing $oneof as normal PostScript ..."
	    /bin/cat $oneof >> $thisfile
	endif
    if ($?pshalf && $?postscript) then
	unset postscript
	echo "Using pshalf to shrink output..."
	echo "  This may or may not work properly..."
        /mit/save/bin/pshalf < $thisfile > ${thisfile}.2
        /bin/mv -f ${thisfile}.2 $thisfile
    endif

	set newfiles = ($newfiles $thisfile)
    else
      echo "$oneof does not exist."
    endif
  end
endif

# Finally, issue the lpr command with the command-line options, and the
# new modified PostScript files in /tmp.  Use the first real file for title.

echo "Sending file(s) to printer ..."
if ($noburst == 1) then
  lpr $lpropts $newfiles
else
  lpr -J$file[1] $lpropts $newfiles
endif
# clean up

/bin/rm $newfiles
exit

usage:
  echo "lpr2 [-d] [-m] [-b] [-e] [ezprint or enscript options] [lpr options] [file(s)]"
  echo "  -d: duplex"
  echo "  -m: tumble mode"
  echo "  -b: bottom tray"
  echo "  -e: print ez files"
  echo "  -4: print two pages per side."
