#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  ps2pdf - convert PostScript format to Portable Data Format
#SYNOPSIS
#  ps2pdf 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>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

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

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

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

file: for $file (@ARGV) {
	if ($file =~ /(.*)\.(\w*ps)$/i) {
		$fili = $file;
		$filo = "$1.pdf";
	} else {
		if      (-f ($fili = "$file.ps" )) {$filo = "$file.pdf";
		} elsif (-f ($fili = "$file.eps")) {$filo = "$file.pdf";
		} elsif (-f ($fili = "$file.PS" )) {$filo = "$file.PDF";
		} else {
			print STDERR "Can't find postscript file for $file.\n";
			next file;
		}
	}
	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=pdfwrite -sOutputFile=$outfile $infile -c quit
	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.                                   #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	$ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin';
	push @INC, '.';
	$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 || 'ps2pdf')) =~ 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 '207.172.223.184') {	# 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;
}
