#!/usr/bin/perl

#could think about extending FilterWorks to do such stuff... or derive sth. from it
#the pipe code should be an option, then...

my $version = '0.1';
my $info = "simple wrapper over epstool to ease the conversion of many eps files to png

just do
	$0 *.eps

when you provide multiple options for color format, the chosen one is the first in this list:

mono ega gray vga alpha

There is no option for true color (24bit) because this is the default.

I hacked this one quickly to not have to remember epstool's (non-intuitive) syntax... epstool can do more than converting to png - but I was just interested in this format and you can easily convert png to any bitmap format you like anyway. The postscript rendering is the nasty part.
";

use lib $ENV{HOME}.'/lib';
use lib $ENV{HOME}.'/lib/perl';

use File::Temp qw(tempdir);
use File::Basename;
use File::Path qw(rmtree);
use File::Spec::Functions;
use Cwd;

my @pars = 
(
	'resolution',100,'r','resolution in dpi',
	'epstool','epstool','e','(path to) epstool',
	'old',0,'o','use old epstool (version 2.3 included with gsview) syntax',
	'convcom','convert -fx \'255\'','C','conversion command (additional arguments are infile and outfile if no pipe)',
	'convpipe',0,'p','conversion through a pipe',
	'keeptemp',0,'k','keep temporary directories',
	'conv',0,'c','apply converison',
	'alpha',0,'A','transparent background and anti-aliasing (via pngalpha device)',
	'vga',0,'V','use 265 colors',
	'gray',0,'G','grayscale',
	'ega',0,'E','16 colors',
	'calcbox',0,'b','calculate bounding box',
	'mono',0,'M','monochrome (b/w)',
	'antialias',0,'a','force anti aliasing via gs-args',
	'gs-args','','','additional ghostscript arguments'
);

my $param;


if(eval { require Config::Param })
{
	$param = Config::Param::get({'info',$info,'version',$version},\@pars);
}
else
{
	print STDERR "!! WARNING: Param library not found; -p/--parameter processing not available !!\n$infostring\n\n";
	for(my $i = 0; $i <= $#pars; $i += 4){ $param->{$pars[$i]} = $pars[$i+1]; }
}

my $startdir = cwd();
my $dev = 'png16m';
$dev = 'pngalpha' if $param->{alpha};
$dev = 'png256' if $param->{vga};
$dev = 'pnggray' if $param->{gray};
$dev = 'png16' if $param->{ega};
$dev = 'pngmono' if $param->{mono};
my $gsargs = $p->{'gs-args'};
if($param->{antialias})
{
	$gsargs .= " -dTextAlphaBits=4 -dGraphicsAlphaBits=4";
}
foreach my $from (@ARGV)
{
	next unless -f $from;
	my $to = $from;
	$to =~ s/\.[eE][pP][sS]$//;
	$to .= '.png';
	print "$to\n";
	my $epscom = $param->{old} ?  "$param->{epstool} ".($param->{calcbox} ? '-b' : '')." -r$param->{resolution} -z$dev -k '-o$to' '$from'" : "$param->{epstool} --bitmap ".($param->{calcbox} ? '-b' : '')." --dpi $param->{resolution} --device $dev".($gsargs ne "" ? " --gs-args '$gsargs'" : "")." --output '$to' '$from'";
	system($epscom)  and print "Error runnig epstool ($!)!\ncommand line: $epscom";
	if($param->{conv})
	{
		my $name = basename($to);
		my $dir = dirname($to);
		chdir($dir);
		my $tempdir = tempdir("eps2png-$name-XXXX");
		my $tempfile = catfile($tempdir,$name);
		rename($name,$tempfile) or die "Error moving file around($!)!\n";
		my $line;
		if($param->{convpipe})
		{
			$line = "cat '$tempfile' | $param->{convcom} > '$name'"; 
		}
		else
		{
			$line = "$param->{convcom} '$tempfile' '$name'"; 
		}
		print "postproduction: $line\n";
		system($line) and print "Error in postproduction!\n";
		rmtree($tempdir,1) unless $param->{keeptemp};
		chdir($startdir) or die "Cannot chdir to startdir ($!)!\n";
	}

}
