# autolatex - asml2pdf.transdef
# Copyright (C) 2017  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 = .asml

OUTPUT_EXTENSIONS for pdf = .pdf
OUTPUT_EXTENSIONS for eps = .eps

TRANSLATOR_FUNCTION =<<EOL {
	my $INITIAL_HEAP_SIZE = "64m";
	my $MAXIMUM_HEAP_SIZE = "1024m";
	my @JAVA_OPTS = ("-Xms$INITIAL_HEAP_SIZE", "-Xmx$MAXIMUM_HEAP_SIZE");

	my @libraries = (
		'astah-sys.jar',
	);

	# Detect the Astah installation
	if ( ! $ENV{"ASTAH_SYSML_HOME"} ) {
		my @tmp = ('astah-sysml');
		my $tmp = undef;
		for(my $i=0; $i<@tmp && !$tmp; $i++) {
			my $bin = $tmp[$i];
			$bin = which("$bin");
			if ( $bin && -x "$bin" ) {
				$bin = readlink_osindep("$bin");
				$tmp = dirname("$bin");
			}
		}
		if ( ! $tmp ) {
			@tmp = ('/usr/lib/astah_sysml',
				);
			for(my $i=0; $i<@tmp && !$tmp; $i++) {
				my $dir = $tmp[$i];
				if ( -d "$dir" ) {
					$tmp = $dir;
				}
			}
			if ( ! $tmp ) {
				printErr("Unable to find the installation directory of Astah SysML. Please define the ASTAH_SYSML_HOME environment variable.");
			}
		}
		$ENV{"ASTAH_SYSML_HOME"} = $tmp;
	}

	# Detect the Java library of astah
	my $jarfile;
	for(my $i=0; $i<@libraries && !$jarfile; $i++) {
		my $library = $libraries[$i];
		if ( -f File::Spec->catfile("$ENV{ASTAH_SYSML_HOME}", "$library" ) ) {
			$jarfile = $library;
		}
	}
	if ( ! $jarfile ) {
		printErr("Unable to find the java library of Astah SysML. Please define the ASTAH_SYSML_HOME environment variable.");
	}

	# Prepare the generation from Astah
	my $outputDir = File::Spec->rel2abs(dirname("$in"));
	my $shortBasename = basename("$in",@inexts);
	my $astahOutputDir = File::Spec->catfile("$outputDir","$shortBasename");
	if ( -d "$astahOutputDir" ) {
		remove_tree("$astahOutputDir") or printErr("$astahOutputDir: $!");
	}

	# Generation of the SVG from Astah
	runCommandOrFail(
		'java',
		@JAVA_OPTS,
		'-cp',
		File::Spec->catfile("$ENV{ASTAH_SYSML_HOME}","$jarfile"),
		'com.change_vision.jude.cmdline.JudeCommandRunner',
		'-image', 'all',
		'-f', "$in",
		'-t', 'svg',
		'-o', "$outputDir");

	# Generation of the PDF/PS from the SVG
	my @generatedFiles = ();
	local *OUTDIR;
	opendir(*OUTDIR, "$astahOutputDir") or printErr("$astahOutputDir: $!");
	while (my $fn = readdir(*OUTDIR)) {
		if ($fn ne File::Spec->updir() && $fn ne File::Spec->curdir()
				&& $fn =~ /\.svg$/s) {
			my $svgFile = File::Spec->catfile("$astahOutputDir", "$fn");
			push @generatedFiles, "$svgFile";

			# Remove the background
			my $svgcontent = readFileLines("$svgFile");
			$svgcontent =~ s/\Q<rect\E\s+.*?\/?>//s;
			writeFileLines("$svgFile", "$svgcontent");
		}
	}
	closedir(*OUTDIR);

	my $transresult = 0;

	# Move the generated files
	if (@generatedFiles>1) {
		my $template = File::Spec->catfile(
					dirname($out),
					basename($out,$outext)).'_';
		my $transresult = 0;
		foreach my $file (@generatedFiles) {
			my $bn = basename($file,'.svg');
			$bn =~ s/\s+/_/sg;
			$bn = "$template$bn$outext";
			my $result = runTranslator( 'svg2pdf', "$file", "$bn", 0);
			if ($result) {
				$transresult = 1;
			}
		}
		if (!$transresult) {
			printErr("No file generated.");
		} else {
			touch("$out");
		}
	}
	elsif (@generatedFiles) {
		my $file = shift @generatedFiles;
		runTranslator('svg2pdf', "$file", "$out");
	}
	else {
		printErr("No file generated.");
	}

	if ( -d "$astahOutputDir" ) {
		remove_tree("$astahOutputDir") or printErr("$astahOutputDir: $!");
	}

	1;
}
EOL

FILES_TO_CLEAN = $out_*.pdf $out_*.eps $out

