#!/bin/csh -f
#
# $Id: lasergnu,v 3.26 92/03/24 22:37:01 woo Exp Locker: woo $
#
#
# Print gnuplot output on an Imagen or Postscript laser printer.

set print_banner = on	# Print a banner page unless told otherwise.
set input_files = ()    # the plot input command files
set lpr_opts = ()	    # options to lpr

# Default printer set by shell variable PRINTER.
if (! $?PRINTER) then 
    if ($?LASER) then
   	   set PRINTER=$LASER
    else
    	   set PRINTER="lw0"
    endif
endif
set printer = (-P$PRINTER)

# File for plot commands, and for plot output
set TMP=/tmp/plot$$
set outfile=$TMP.out	# the output file
onintr cleanup

# default is Imagen mode for Imagen printer; see -p option
set setterm="set terminal imagen"
set LANG="-Limpress"

set usage="usage: lasergnu [-Pprinter] [-b] [-p] [-t title] [-f file] ['plot command']...."

# Loop through the command-line arguments.

top:
	if ($#argv > 0) then

		switch ("$argv[1]")

		case -b*:	# Do not print a banner page.
		case -J*:	# Compatible with imprint.
			set print_banner = off
    	   	  	set lpr_opts=($lpr_opts -h)
			shift argv
			goto top

		case -f?*:	# Specify file containing plot commands
			set input_files = ($input_files `echo $argv[1] | sed 's/^-f//'`)
			shift argv
			goto top

		case -f:	# Specify file containing plot commands
			shift argv
			if ($#argv > 0) then
				set input_files = ($input_files $argv[1])
				shift argv
			else
				echo "Usage: -f file ..."
				echo "Type    lasergnu -help    for help."
				exit (1)
			endif
			goto top

		case -t?*:	# Specify title of plot
			echo set title \""`echo $argv[1] | sed 's/^-t//'`"\" >> $TMP
			shift argv
			goto top

		case -t:	# Specify title of plot
			shift argv
			if ($#argv > 0) then
				echo set title \""$1"\" >> $TMP
				shift argv
			else
				echo "Usage: -t title ..."
				echo "Type    lasergnu -help    for help."
				exit (1)
			endif
			goto top
          case -help:
			echo "$usage"
			exit(1)

		case -P?*:	# Set the printer, exactly as by itroff.
			set printer = $argv[1]
			shift argv
			goto top

		case -P:	# Set the printer, exactly as by itroff.
			shift argv
			if ($#argv > 0) then
				set printer = (-P$argv[1])
				shift argv
			else
				echo "Usage: -P printer ..."
				echo "Type    lasergnu -help    for help."
				exit (1)
			endif
			goto top

                # use impress 
    	   case -I:
	   		 echo Imagen is the default mode now
			 shift argv
	   		 goto top

                # use postscript instead of impress language
    	   case -p:
	   		 set setterm="set term postscript"
	   		 set LANG="-Lpostscript"
			 shift argv
	   		 goto top

		case -?*:
			echo "I do not recognize option $argv[1]."
			echo "$usage"
			exit (1)

		default:
	          echo "$argv[1]"	>> $TMP
			shift argv
			goto top

		endsw
	endif

# try to devine the printer type
if ($printer =~ -Plw*) then
    set setterm="set term postscript"
    set LANG="-Lpostscript"
endif

if ($printer =~ -Pim*) then
    set setterm="set term imagen"
    set LANG="-Limpress"
endif

# Set up input file
echo $setterm > $TMP.plt
echo set output \"$outfile\" >> $TMP.plt
if (-e $TMP) cat $TMP >> $TMP.plt

# If input file is specified AND command line contains plot commands, then
#	do command line args first, then plot commands in input file.
gnuplot $TMP.plt $input_files

if ($status == 0 && -e $outfile && ! -z $outfile) then
    # The printer is whatever printer was last specified,
    # or the default printer if none was specified.
    if ($LANG == -Limpress) then
        /usr/local/bin/ipr $LANG $printer \
    	   -D"jobheader $print_banner" \
    	   -D"pagereversal on" \
    	   -D"program lasergnu" $outfile
    else if ($LANG == -Lpostscript) then
    	   lpr $lpr_opts $printer $outfile
    endif
else
    echo "lasergnu: error in plotting or empty plot; nothing printed."
endif

cleanup:
rm -f $TMP* $outfile
