# autolatex - astah2png.transdef
# Copyright (C) 2012-13  Stephane Galland <[email protected]>
#
# 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 = .asta .jude .juth

OUTPUT_EXTENSIONS = .png

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-pro.jar',
               'astah-professional.jar',
               'astah-uml.jar',
               'astah-com.jar',
               'astah-community.jar',
       );

       # Detect the Astah installation
       if ( ! $ENV{"ASTAH_HOME"} ) {
               my @tmp = ('astah-pro','astah-uml','astah-com');
               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_pro',
                               '/usr/lib/astah_professional',
                               '/usr/lib/astah_uml',
                               '/usr/lib/astah_com',
                               '/usr/lib/astah_community');
                       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. Please define the ASTAH_HOME environment variable.");
                       }
               }
               $ENV{"ASTAH_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_HOME}", "$library" ) ) {
                       $jarfile = $library;
               }
       }
       if ( ! $jarfile ) {
               printErr("Unable to find the java library of Astah. Please define the ASTAH_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_HOME}","$jarfile"),
               'com.change_vision.jude.cmdline.JudeCommandRunner',
               '-image', 'all',
               '-f', "$in",
               '-t', 'png',
               '-o', "$outputDir");

       # Move the files
       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 =~ /$outext$/s) {
                       my $sourceFile = File::Spec->catfile("$astahOutputDir", "$fn");
                       push @generatedFiles, "$sourceFile";
               }
       }
       closedir(*OUTDIR);

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

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

       1;
}
EOL

FILES_TO_CLEAN = $out_*.png $out