#!/usr/local/bin/perl -w
#
# ps2gif [resolution] file...

# Convert  a postscript file to GIF, using the gs (GhostScript) command.  The
# resolution defaults to  100,  which  is  a  readable  compromise  for  most
# screens.   The files should be postscript files.  You can omit a .ps suffix
# and we'll assume it.
#
# Author: John Chambers <jc@trillian.mit.edu>

$V = $ENV{"V_ps2gif"} || 1;
print "$0: called for " . join(' ',@ARGV) . "\n" if $V>1;

#GS = '/usr/local/bin/gs';
if (-x ($pnmcrop='/usr/bin/pnmcrop')) {
	print "$0: Using '$pnmcrop'\n" if $V>1;
} elsif (-x ($pnmcrop='/usr/local/bin/pnmcrop')) {
	print "$0: Using '$pnmcrop'\n" if $V>1;
} else {
	print STDERR "$0: can't find pnmcrop in search PATH.\n" if $V>0;
	print STDERR "$0: PATH=\"" . $ENV{PATH} . "\"\n" if $V>0;
}

$ENV{LD_LIBRARY_PATH} = '/usr/X11R6/lib/:/usr/eecs/lib:/usr/lib:/usr/lib/aout';

($hres = (shift(@ARGV)) . ' 100') =~ /(\d+)/; $hres = $1;
($vres = (shift(@ARGV)) . ' 100') =~ /(\d+)/; $vres = $1;

file: for $f (@ARGV) {
	if ($f =~ /(.*)\.(\w*ps)$/i) {
		$F = $f;
		$G = "$1.gif";
	} else {
		if      (-f ($F = "$f.ps" )) {$G = "$f.gif";
		} elsif (-f ($F = "$f.eps")) {$G = "$f.gif";
		} elsif (-f ($F = "$f.PS" )) {$G = "$f.GIF";
		} else {
			print STDERR "Can't find postscript file for $f.\n";
			next file;
		}
	}
	system "gs -q -DNOPAUSE -sDEVICE=ppmraw -r${hres}x${vres} -sOutputFile='|$pnmcrop|ppmtogif -interlace >$G' -- $F";
	system "ls -l $F $G" if $V>0;
}
