/*
 * Copyright (c) 2014 Fredy Wijaya
 * 
 * All rights reserved. This document and accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.fredy.openhere.core;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class is responsible for launching a process.
 * 
 * @author fredy
 */
public class ProcessLauncher {
    private ProcessLauncher() {
    }
    
    private static String[] buildCommand(String command, String path) {
        String newCommand = substitute(command, path);
        List<String> matchList = new ArrayList<>();
        Pattern regex = Pattern.compile("[^\\s\"']+|(\"[^\"]*\")|('[^']*')");
        Matcher matcher = regex.matcher(newCommand);
        while (matcher.find()) {
            if (matcher.group(1) != null) {
                matchList.add(matcher.group(1));
            } else if (matcher.group(2) != null) {
                matchList.add(matcher.group(2));
            } else {
                matchList.add(matcher.group());
            }
        }
        return matchList.toArray(new String[0]);
    }
    
    private static String substitute(String str, String replacement) {
        return str.replaceAll("\\$\\{path\\}", Matcher.quoteReplacement(replacement));
    }
    
    /**
     * Launches a process.
     * 
     * @param command the process
     */
    public static void launch(String command, String path) {
        String[] cmd = buildCommand(command, path);
        ProcessBuilder pb = new ProcessBuilder(cmd);
        try {
            Process p = pb.start();
            // TODO: fix this
            // if (p.waitFor() != 0) {
            //     throw new ProcessLauncherException(
            //         join(cmd) + " returned " + p.exitValue() + " exit code");
            // }
        } catch (Exception e) {
            throw new ProcessLauncherException(e);
        }
    }
    
    private static String join(String[] strings) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (String str : strings) {
            if (i == 0) {
                sb.append(str);
            } else {
                sb.append(" ").append(str);
            }
            i++;
        }
        return sb.toString();
    }
} 

@@ -1728,20 +1728,8 @@
     
- Process p =
  pb.
@@ -1753,25 +1753,47 @@
     
-// TODO: fix this
+%0A            // Process p = pb.start();
 %0A   
@@ -2080,32 +2080,35 @@
 %7D%0A    %7D%0A    %0A   
+ //
  private static 
@@ -2139,28 +2139,31 @@
 ings) %7B%0A    
+//
     
+ 
 StringBuilde
@@ -2189,24 +2189,27 @@
 ilder();%0A   
+ //
      int i =
@@ -2211,24 +2211,27 @@
 t i = 0;%0A   
+ //
      for (St
@@ -2251,24 +2251,27 @@
 rings) %7B%0A   
+ //
          if 
@@ -2276,32 +2276,35 @@
 f (i == 0) %7B%0A   
+ //
              sb.
@@ -2316,32 +2316,34 @@
 d(str);%0A    
+//
         
 %7D else %7B%0A   
@@ -2322,32 +2322,33 @@
 ;%0A    //        
+ 
 %7D else %7B%0A       
@@ -2335,32 +2335,35 @@
     %7D else %7B%0A   
+ //
              sb.
@@ -2386,24 +2386,27 @@
 nd(str);%0A   
+ //
          %7D%0A 
@@ -2403,24 +2403,27 @@
        %7D%0A   
+ //
          i++
@@ -2423,24 +2423,27 @@
     i++;%0A   
+ //
      %7D%0A     
@@ -2432,32 +2432,35 @@
     //     %7D%0A   
+ //
      return sb.t
@@ -2473,14 +2473,17 @@
 g();%0A   
+ //
  %7D%0A%7D %0A

Comment out unused code