#!/usr/bin/perl -w
#
# ps2png [resolution] file...
#
# Convert a postscript file to PNG, using the gs (GhostScript) command.   The
# resolution defaults to 85, 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>

$gs = '/usr/local/bin/gs';
$pnmcrop = '/usr/bin/pnmcrop';
$wpng = '/hda/home/jc/bin/wpng';
$ENV{PATH} = '$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin';
$ENV{LD_LIBRARY_PATH} = '/usr/X11R6/lib/:/usr/eecs/lib:/usr/lib:/usr/lib/aout';

$hres = shift;
$vres = shift;

file: for $file (@ARGV) {
	if ($file =~ /(.*)\.(\w*ps)$/i) {
		$fili = $file;
		$filo = "$1.png";
	} else {
		if      (-f ($fili = "$file.ps" )) {$filo = "$file.png";
		} elsif (-f ($fili = "$file.eps")) {$filo = "$file.png";
		} elsif (-f ($fili = "$file.PS" )) {$filo = "$file.PNG";
		} else {
			print STDERR "Can't find postscript file for $file.\n";
			next file;
		}
	}
	$cmd = "gs -q -DNOPAUSE -sDEVICE=ppmraw -r${hres}x${vres} -sOutputFile='|pnmcrop|wpng >$filo' -- $fili";
#	$cmd = "gs -q -DNOPAUSE -sDEVICE=ppmraw -r${hres}x${vres} -sOutputFile='|pnmcrop|pnmtopng >$filo' -- $fili";
	print STDERR "cmd: $cmd\n";
	system $cmd;
	if ($?) {
		print STDERR "Conversion of \"$fili\" failed with exit status $?.\n";
		exit $?;
	}
}
