package org.codehaus.mojo.latex;

/*
 * Copyright 2010 INRIA / CITI Laboratory / Amazones Research Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import static org.apache.commons.exec.CommandLine.parse;
import static org.apache.commons.io.FileUtils.copyDirectory;
import static org.apache.commons.io.FileUtils.copyFile;

/**
 * LaTeX documents building goal.
 *
 * @author Julien Ponge
 * @goal latex
 * @phase compile
 */
public class LaTeXMojo
    extends AbstractMojo
{

    /**
     * The documents root.
     *
     * @parameter expression="${latex.docsRoot}" default-value="src/main/latex"
     * @required
     */
    private File docsRoot;

    /**
     * Common files directory inside the documents root (the only directory to be skipped).
     *
     * @parameter expression="${latex.commonsDirName}" default-value="common"
     * @required
     */
    private String commonsDirName;

    /**
     * The Maven build directory.
     *
     * @parameter expression="${project.build.directory}"
     * @required
     * @readonly
     */
    private File buildDir;

    /**
     * The LaTeX builds directory.
     *
     * @parameter expression="${project.latex.build.directory}" default-value="${project.build.directory}/latex"
     * @required
     */
    private File latexBuildDir;

    /**
     * Path to the LaTeX binaries installation.
     *
     * @parameter expression="${latex.binariesPath}" default-value=""
     */
    private String binariesPath;

    @Override
    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        try
        {
            final File[] docDirs = getDocDirs();
            final File[] buildDirs = prepareLaTeXBuildDirectories( docDirs );
            buildDocuments( buildDirs );
        }
        catch ( IOException e )
        {
            getLog().error( e );
            throw new MojoFailureException( e.getMessage() );
        }
    }

    private void buildDocuments( File[] buildDirs )
        throws IOException, MojoFailureException
    {
        for ( File dir : buildDirs )
        {
            final File texFile = new File( dir, dir.getName() + ".tex" );
            final File pdfFile = new File( dir, dir.getName() + ".pdf" );
            final File bibFile = new File( dir, dir.getName() + ".bib" );

            final CommandLine pdfLaTeX =
                parse( executablePath( "pdflatex" ) )
                    .addArgument( "-shell-escape" )
                    .addArgument( "--halt-on-error" )
                    .addArgument( texFile.getAbsolutePath() );

            final CommandLine bibTeX = parse( executablePath( "bibtex" ) ).addArgument( dir.getName() );

            execute( pdfLaTeX, dir );
            if ( bibFile.exists() )
            {
                execute( bibTeX, dir );
                execute( pdfLaTeX, dir );
            }
            execute( pdfLaTeX, dir );

            copyFile( pdfFile, new File( buildDir, pdfFile.getName() ) );
        }
    }

    private String executablePath( String executable )
    {
        if ( binariesPath == null )
        {
            return executable;
        }
        else
        {
            return new StringBuilder().append( binariesPath ).append( File.separator ).append( executable ).toString();
        }
    }

    private void execute( CommandLine commandLine, File dir )
        throws IOException, MojoFailureException
    {
        final DefaultExecutor executor = new DefaultExecutor();
        executor.setWorkingDirectory( dir );
        if ( executor.execute( commandLine ) != 0 )
        {
            throw new MojoFailureException( "Error code returned for: " + commandLine.toString() );
        }
    }

    private File[] prepareLaTeXBuildDirectories( File[] docDirs )
        throws IOException
    {
        final File[] buildDirs = new File[docDirs.length];
        final File commonsDir = new File( docsRoot, commonsDirName );

        for ( int i = 0; i < docDirs.length; i++ )
        {
            final File dir = docDirs[i];
            final File target = new File( latexBuildDir, docDirs[i].getName() );
            buildDirs[i] = target;

            copyDirectory( dir, target );
            if ( commonsDir.exists() )
            {
                copyDirectory( commonsDir, target );
            }
        }

        return buildDirs;
    }

    private File[] getDocDirs()
    {
        return docsRoot.listFiles( new FileFilter()
        {
            @Override
            public boolean accept( File pathname )
            {
                return pathname.isDirectory() && !( pathname.getName().equals( commonsDirName ) ) &&
                    !( pathname.isHidden() );
            }
        } );
    }

}

@@ -3405,24 +3405,95 @@
 utePath() );
+%0A            getLog().debug( %22pdflatex: %22 + pdfLaTeX.getExecutable() );
 %0A%0A          
@@ -3582,24 +3582,91 @@
 getName() );
+%0A            getLog().debug( %22bibtex: %22 + bibTeX.getExecutable() );
 %0A%0A          

Fix for MLATEX-6: added debug log messages to know which pdflatex/bibtex executables are being called.