/* 
 * Copyright (c) 2015 Sebastian Brudzinski
 * 
 * See the file LICENSE for copying permission.
 */
package latexstudio.editor.runtime;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import latexstudio.editor.util.ApplicationUtils;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.IOUtils;

/**
 * The class responsible for execution of commands in runtime and handling the outputs
 * of external processes.
 * 
 * @author Sebastian
 */
public final class CommandLineExecutor {
    
    private static final DefaultExecutor EXECUTOR;
    
    static {
        EXECUTOR = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(8000);

        // generating pdf returns code 1, so we add it to the accepted values
        EXECUTOR.setExitValues(new int[] {0,1});
        EXECUTOR.setWatchdog(watchdog);
    }
      
    public static void executeGeneratePDF(CommandLineBuilder cmd) {
        String outputDirectory = "--output-directory=" + cmd.getOutputDirectory();
        String outputFormat = "--output-format=pdf";
        
        String job = cmd.getJobname() == null ? "" : "--jobname=" + cmd.getJobname().replaceAll(" ", "_");
        String includeDir = cmd.getWorkingFile() == null ? "" : "--include-directory=" + cmd.getWorkingFile().getParentFile().getAbsolutePath();

        ByteArrayOutputStream outputStream = null;
        
        try {           
            String[] command =  new String[] {
                outputDirectory, outputFormat, job, includeDir, cmd.getPathToSource()
            };
         
            CommandLine cmdLine = new CommandLine(ApplicationUtils.getPathToTEX(cmd.getLatexPath()));
            //For windows, we set handling quoting to true
            cmdLine.addArguments(command, ApplicationUtils.isWindows());
            
            
            outputStream = new ByteArrayOutputStream();
            PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
            EXECUTOR.setStreamHandler(streamHandler);
            
            EXECUTOR.execute(cmdLine);      

            if (cmd.getLogger() != null) {
                cmd.getLogger().log(cmdLine.toString());
                cmd.getLogger().log(outputStream.toString());
            }
                        
        } catch (IOException e) {
            if (cmd.getLogger() != null) {
                cmd.getLogger().log("The path to the pdflatex tool is incorrect or has not been set.");
            }
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    }
}

@@ -1069,16 +1069,29 @@
  public 
+synchronized 
 static v
@@ -1136,24 +1136,24 @@
 der cmd) %7B%0D%0A
-
         Stri
@@ -1278,26 +1278,16 @@
 =pdf%22;%0D%0A
-        %0D%0A
         
@@ -1390,156 +1390,8 @@
 );%0D%0A
-        String includeDir = cmd.getWorkingFile() == null ? %22%22 : %22--include-directory=%22 + cmd.getWorkingFile().getParentFile().getAbsolutePath();%0D%0A%0D%0A
     
@@ -1577,20 +1577,8 @@
 job,
- includeDir,
  cmd
@@ -1875,30 +1875,16 @@
       %0D%0A
-            %0D%0A
         
@@ -2008,32 +2008,32 @@
 outputStream);%0D%0A
-
             EXEC
@@ -2077,32 +2077,213 @@
 %0D%0A            %0D%0A
+            if (cmd.getWorkingFile() != null) %7B%0D%0A                EXECUTOR.setWorkingDirectory(cmd.getWorkingFile().getParentFile().getAbsoluteFile());%0D%0A            %7D%0D%0A            %0D%0A
             EXEC

Fix working with other files on Linux