#!/usr/bin/perl

my $info = 'little wrapper to produce small pieces of rendered LaTeX
Main intention: Want a single formula as nice graphic for web pages... of course this is a bad intention since things like MathML are there to do this nicely, but I need sth. now that works.
';

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

my @pars =
(
	'header','\documentclass[a4paper,12pt]{letter}
\pagestyle{empty}
\usepackage[isolatin]{inputenc}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{graphicx}
\usepackage{color}','','document preamble (header); begin and end of document are of course added',
	'eqnarray',1,'e','set \begin{eqnarray*} and \end{eqnarray*} around your code',
	'res',0,'r','create png files with this resolution (if > 0, otherwise produce eps file)',
	'dvitops','dvips','d','dvi to ps converter (dvips syntax)',
	'latex','latex','l','latex command (obviously latex syntax;-)',
	'keeptemp',0,'k','keep temporary directories',
	'boldmath',1,'b','make all math bold...',
	'sansserif',1,'s','make text sans serif',
	'join',0,'j','first file is tex source, rest are files to be linked into temp dir',
	'eps',1,'p','boil it all down to an eps file, (otherwise make a normal postscript file)'
);

my $startdir = cwd();

my $param;

if(eval { require Config::Param })
{
	 $param = Config::Param::get({'info',$info,'hidenonshort',1},\@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]; }
}

for(my $i = 0; $i <= $#ARGV; ++$i)
{
	my $a = $ARGV[$i];
	my @addf = ();
	if($param->{join})
	{
		for($i = 1; $i <= $#ARGV; ++$i)
		{
			push(@addf, $ARGV[$i]);
		}
	}
	if(open(DAT, $a))
	{
		my $name = basename($a);
		my $dir = dirname($a);
		$name =~ s/\.(tex|TEX)$//;   #what endings are usual?	
	
		my $tempdir = tempdir("minitex-$name-XXXX");
		foreach my $f (@addf)
		{
			#to be enhanced greatly
			print catfile($startdir,$f),"\n";
			symlink(catfile($startdir,$f), catfile($tempdir,$f)) or die "cannot link ($!)!\n";
		}
		print "work in $tempdir\n";
		my $tempfile = catfile($tempdir,$name);
		open(ODAT, ">$tempfile.tex") or die "Fatal: cannot create $tempfile.tex ($!)!\n";
		print ODAT $param->{header}."\n\\begin\{document\}\n";
		print ODAT "\\sf\n" if $param->{sansserif};
		print ODAT "\\boldmath\n" if $param->{boldmath};
		print ODAT "\\begin\{eqnarray*\}\n" if $param->{eqnarray};
		while(<DAT>){ print ODAT; }
		print ODAT "\\end\{eqnarray*\}\n" if $param->{eqnarray};
		print ODAT "\\end\{document\}\n";
		close(ODAT);
		close(DAT);
		chdir($tempdir) or die "Cannot chdir to tempdir ($!)!\n";
		print "Running latex...\n";
		system($param->{latex}." -interaction=nonstopmode '$name.tex'") and print "Error runnig latex ($!)!\n";
		print "Running dvips...\n";
		if($param->{eps})
		{
			system($param->{dvitops}." -E -Ppdf -o '$name.eps' '$name.dvi'") and print "Error running $param->{dvitops} ($!)!\n";
		}
		else
		{
			system($param->{dvitops}." -Ppdf -o '$name.ps' '$name.dvi'") and print "Error running $param->{dvitops} ($!)!\n";
		}
		chdir($startdir) or die "Cannot chdir to startdir ($!)!\n";
		rename($tempfile.($param->{eps} ? '.eps' : '.ps'),catfile($dir,$name.($param->{eps} ? '.eps' : '.ps'))) or  die "Error moving $tempfile.(e)ps to $name.(e)ps ($!)!\n";
		rmtree($tempdir,1) unless $param->{keeptemp};
	}
	else{ print "Could'n read $a ($!)!\n"; }
}
