#!/usr/bin/perl
#
#	psprint: print a postscript file
#
#
use Getopt::Std;
getopts("lcp",\%options) || errexit();
if($options{l}){
	$landscape='landscap.ps';
	}
if($options{c}){
	$colourps=1;
	open(COLOUR,"/usr/local/site//etc/printcolour") || die;
	while(<COLOUR>){
		next if /^#/;
		chomp;
		$colourcommand = $_;
		}
	if($colourcommand !~ /^\s*gs/){
		print stderr ("Need gs command in /usr/local/site/etc/printcolour");
		errexit();
		}
	}
$file = shift;
$readfromstdin=1 unless $file;
&errexit unless ( -f $file || $readfromstdin);
$extension = $1 if $file =~ /\.(\w+$)/;
if($options{p}){  # force ps
	$extension='ps';
	}

if($extension eq 'ps' && $colourps){
	$extension='cps';  # force colour ps
	}
if($readfromstdin){
	open(GHOST, "|gs -q -dNOPAUSE -dBATCH -sPAPERSIZE=a4  -sDEVICE=ljet4 -r600 -sOutputFile=- $landscape - |
                lpr  -b");
	while(<>){
		print GHOST;
		}
	}
		
elsif($extension eq 'dvi'){
	system( "dvips  $file -f |
		 gs -q  -dNOPAUSE -dBATCH -sPAPERSIZE=a4  -sDEVICE=ljet4 -r600 -sOutputFile=- $landscape - |
		lpr -b");
	}
elsif ($extension eq 'cps' || $colourps ){
	if($reafromstdin){
		open(GHOST,"| $colourcommand $landscape - |
			lpr -b -P dot ");
		while(<>){
			print GHOST;
			}
		} else {
		system("cat $file |
		$colourcommand $landscape - |
			lpr -b -P dot");
		} 
	}
elsif ($extension eq 'ps' || $extension eq 'ps_tmp'){
	system("cat $file |
	 gs -q  -sPAPERSIZE=a4  -dNOPAUSE -dBATCH -sDEVICE=ljet4 -r600 -sOutputFile=- $landscape - | 
                lpr -l ");
	}

elsif ($extension eq 'tex'){
	$file =~ s/\.\w+$//;
	system ("latex $file;
		dvips  $file -o- |
                 gs -q t  -sPAPERSIZE=a4  -dNOPAUSE -dBATCH -sDEVICE=ljet4 -r600 -sOutputFile=- $landscape - | 
                lpr -b");	
		}
else {
	$groffcommand = `grog $file`;
	if($groffcommand =~ /groff/){
		system("$groffcommand |
		dvips  $file -f |
                 gs -q  -sPAPERSIZE=a4  -dNOPAUSE -dBATCH -sDEVICE=ljet4 -r600 -sOutputFile=- $landscape - |
                lpr ");
		}
	}	

#

#
sub errexit{
print "usage psprint file\noptions -l (landscape) -c (force colour)\n";
exit;
}
