#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  ps2png - convert PostScript format to Portable Network Graphics format
#SYNOPSIS
#  ps2png hres vres file...
#
#DESCRIPTION
#  Convert a postscript file to PNG, using the gs (GhostScript) command.  The
#  resolution  defaults  to  100x100, 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
#  Author: John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

# Look around for the programs we need:
if      (-f ($x = '/usr/bin/gs'      )) { $gs_prog = $x;
} elsif (-f ($x = '/usr/local/bin/gs')) { $gs_prog = $x;
} elsif (-f ($x = "$HOME/bin/gs")) { $gs_prog = $x;
} else {print STDERR "$0: Can't find gs program.\n"; exit 1}
if      (-f ($x = '/usr/bin/pnmcrop'      )) { $pnmcrop = $x;
} elsif (-f ($x = '/usr/local/bin/pnmcrop')) { $pnmcrop = $x;
} else {print STDERR "$0: Can't find pnmcrop program.\n"; exit 1}

$| = 1;
$exitstat = 0;
require &LocalSetup(1);	# Figure out localization stuff
$version = '20051129';	# Which version we claim to be

$ENV{PATH} = '.:bin:$HOME/bin:/bin:/usr/bin';
$ENV{LD_LIBRARY_PATH} = '/usr/X11R6/lib/:/usr/eecs/lib:/usr/lib:/usr/lib/aout';

$hres = shift; ($hres . ' 200') =~ /(\d+)/; $hres = $1;	# was 100
$vres = shift; ($vres . ' 200') =~ /(\d+)/; $vres = $1;	# was 100

file: for $file (@ARGV) {
	unless ($fili) {
		$fili = $file;
		next file;
	}
	if ($filo) {
		print STDERR "$P: Extra arg \"$file\" ignored.\n" if $V>0;
	} else {
		$filo = $file;
		print STDERR "$P: filo=\"$filo\"\n" if $V>1;
	}
}
unless ($fili) {
	print STDERR "$P: No input file found in command.\n" if $V>0;
	exit 1;
}
unless ($filo) {
	print STDERR "$P: No input file found in command.\n" if $V>0;
	if ($fili =~ m/(.*)\.e*ps$/i) {	# Is it some sort of PS file?
		$filo = "$1.png";
	} else {		# If not, just add a ".png" suffix.
		$filo = $fili . '.png';
	}
}
print "gs_prog=\"$gs_prog\"\n" if $V>1;
unless (defined($gs_prog)) {	# Did our cgilocal.pm say which gs to call?
	$gs_prog = `which gs`;	# If not, search for it
	print "gs_prog=\"$gs_prog\" [which]\n" if $V>1;
	$gs_prog =~ s/[\r\s]+$//;
}
print "gs_prog=\"$gs_prog\"\n" if $V>1;
#gs_cmd = "$gs_prog -q -DNOPAUSE -sDEVICE=ppmraw -r${hres}x${vres} -sOutputFile='|$pnmcrop|wpng >$filo' -- $fili";
$gs_cmd = "convert -trim $fili $filo";
print "gs_cmd=\"$gs_cmd\"\n" if $V>1;
system $gs_cmd;
if ($?) {
	print STDERR "Conversion of \"$fili\" failed with exit status $?.\n";
	exit $?;
}

exit $exitstat;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

sub LocalSetup { $Vtest = shift;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Figure out where we're running, and try to require a *cgilocal.pm file  for #
# local  settings.   The  end  result of this is to initialize a long list of #
# global variables.  The return value is the name of the cgilocal file, which #
# we usually feed to the 'require' command.                                   #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	$HOME = $ENV{'HOME'};
	$ENV{PATH} = '$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin';
	push @INC, '.', "$HOME/bin", "$HOME/sh";
	$testing = '1';	# Set to 1 while testing
	umask 0002;		# Output files must be group writable
	$| = 1;			# Unbuffered STDOUT
	$" = ',';		# Used in verbose messages
	$pid = sprintf("%06d",$$);
	($P = ($0 || 'ps2png')) =~ s".*/"";	# This program's name, minus directories
	(($ENV{"V_$P"} || $ENV{"D_$P"} || $testing) . ' 2') =~ /(\d+)/;	# Verbose level
	$V = int($1);
	($ENV{REMOTE_ADDR} || '0.0.0.0') =~ m/^\s*([\d.]+)\s*$/;
	$RA = $1;
	if (defined($x = $data{V}) && ($x =~ /^\s*(\d+)\s*$/)) {
		$V = int($1);
		&lsend("V=$V (from data).\n") if $V>3;
	} elsif ($RA eq '66.92.73.254') {	# My home machine
		$V = $Vtest if $V<$Vtest;
	} elsif ($RA =~ /^192\.168\./) {	# My home network
		$V = $Vtest if $V<$Vtest;
	}
	local($ss,$mm,$hh,$DD,$MM,$YY) = gmtime(time);		# Current date/time
	$ymd = sprintf("%d-%02d-%02d",1900+$YY,1+$MM,$DD);	# CCYY-MM-DD
	$hms = sprintf("%02d:%02d:%02d",$hh,$mm,$ss);		# HH:MM:SS
	$hostname = `/bin/hostname`;		# What does this machine call itself?
	$hostname =~ s/^\s*([-.\w]*)([\r\s]*)$/$1/;		# Strip off domain info
	($host = $1) =~ s/\..*//;			# Extract first field of name
	$cwd = `/bin/pwd`;
	$hstloc = $host . "-cgilocal.pm";
	$cgiloc = (-f $hstloc) ? $hstloc : 'cgilocal.pm';
	return $cgiloc;
}
