# autolatex - plot2pdf+tex.transdef
# Copyright (C) 1998-2013  Stephane Galland <galland@arakhne.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#--------------------------------------------
# See template.txt for details on the fields
#--------------------------------------------

INPUT_EXTENSIONS = .plott .plot_tex .plottex .plot+tex .tex.plot +tex.plot .gnut .gnu_tex .gnutex .gnu+tex .tex.gnu +tex.gnu

OUTPUT_EXTENSIONS for pdf = .pdf .pdftex_t
OUTPUT_EXTENSIONS for eps = .eps .pstex_t

TRANSLATOR_PERL_DEPENDENCIES = Cwd AutoLaTeX::TeX::TeXParser

TRANSLATOR_FUNCTION =<<EOL {
	my $curdir = getcwd();
	my $plotdir = dirname("$in");

	# Filenames
	my $plotFile = File::Spec->catfile("$plotdir", basename("$in", @inexts).'__xxxx_autolatex.plot');
	my $texFile = File::Spec->catfile(dirname("$out"), basename("$out", @outexts).'.tex');
	my $psFile = File::Spec->catfile(dirname("$out"), basename("$out", @outexts).'.ps');
	my $ptextFile = File::Spec->catfile(dirname("$out"), basename("$out", @outexts).
				($ispdfmode ? '.pdftex_t' : '.pstex_t'));

	# Generate the PLOT file that is able to generate the (PS/PDF)TEX
	local *IN;
	local *OUT;
	open(*IN, "<$in") or printErr("$in: $!");
	open(*OUT, ">$plotFile") or printErr("$plotFile: $!");
	print OUT "set terminal pstex color auxfile;\n";
	print OUT "set output \"".addSlashes($texFile)."\";\n\n";
	while (my $line = <IN>) {
		$line =~ s/set\s+term(?:inal)?.*?[\n;]//gs;
		print OUT $line;
	}
	print OUT "unset output;\n";
	print OUT "quit;\n";
	close(*OUT);
	close(*IN);

	# Invoke Gnuplot
	chdir($plotdir) or printErr("$plotdir: $!");
	runCommandOrFail('gnuplot', "$plotFile");
	chdir($curdir) or printErr("$curdir: $!");
	unlink($plotFile);

	if ($ispdfmode) {
		# Clean the PDFTEX_T file by removing the special macros, and
		# adding the graphic inclusion at the right place
		my $content = readFileLines("$texFile");
		my $listener = {
			'content' => '',
			'expandMacro' => sub {
				my $listener = shift;
				my $parser = shift;
				my $macro = shift;
				if ($macro eq '\\special' ) {
					if ($_[0] && $_[0]->{'text'} && $_[0]->{'text'} =~ /\Qpsfile=\E/s) {
						return "\\put(0,0){\\includegraphics{$out}}";
					}
		# The first character after \special is consumed, reinject it if it is a brace.
					if ($_[0] && $_[0]->{'text'} && $_[0]->{'text'} eq '}') {
						return "$macro}";
					}
					return '';
				}
				return undef;
			},
			'outputString' => sub {
				my $listener = shift;
				my $parser = shift;
				my $str = shift;
				$listener->{'content'} .= $str;			
			},
		};

		my $parser = AutoLaTeX::TeX::TeXParser->new("$texFile", $listener);
		$parser->addTextModeMacro('special','!{}');
		$parser->addMathModeMacro('special','!{}');
		$parser->parse( $content );
		open(*OUT, ">$ptextFile") or printErr("$ptextFile: $!");
		print OUT $listener->{'content'};
		close(*OUT);
		unlink("$texFile");
	}
	else {
		move("$texFile", "$ptextFile") or printErr("$texFile -> $ptextFile: $!");
	}

	# Generate the proper figure
	if ($isepsmode) {
		move("$psFile", "$out") or printErr("$psFile -> $out: $!");
	}
	else {
		my $transresult = runTranslator('eps2pdf', "$psFile", "$out");
		unlink($psFile);
		$transresult;
	}
}
EOL

FILES_TO_CLEAN = $out.pdftex_t $out.pstex_t *__xxxx_autolatex.plot $out/