#!/usr/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>

$pid = sprintf("%06d",$$);
($P = $0) =~ s".*/"";
($ENV{"V_$P"} || $ENV{"D_$P"} || '1') =~ /(\d+)/;	# Verbose level
$V = int($1);

$ENV{PATH} = '/hda/home/jc/bin:/hda/home/jc/sh:/usr/bin:/usr/sbin:/usr/local/bin';
$ENV{LD_LIBRARY_PATH} = '/lib:/usr/lib:/var/lib:/usr/local/lib:/usr/X11R6/lib:/hda/root/lib:/hda/root/usr/lib:/hda/root/var/lib';

$hres = shift;
$vres = shift;

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;
		}
	}
	&lsend("PATH='" . $ENV{PATH} . "'") if $V>0;
	&showcmd('gs');
	&showcmd('pnmcrop');
	&showcmd('ppmtogif');
	system "gs -q -DNOPAUSE -sDEVICE=ppmraw -r${hres}x${vres} -sOutputFile='|pnmcrop|ppmtogif -interlace >$G' -- $F";
}
exit $?;

sub lsend {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Send a line to the logfile.  
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#	if ($Lopen) {
#		print L @_;
#	} else {
#		print STDERR "$0($$): No logfile.\n";
		print STDERR @_,"\n";
#	}
}

sub showcmd { my $F='showcmd';
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($cmd) = @_;
	local($pth);
	$pth = `which $cmd`;
	$pth =~ s/^[\r\n]+//;
	&lsend("$P/$F: '$cmd' is $pth\n");
#	return $pth;
}
