package org.flatland.decaf;
import java.lang.reflect.Method;
import java.io.*;

public class Main {
  public static void main(String[] args) throws Exception {
    String class_name = args[0];
    String fifo_dir   = args[1];

    reopenStreams(fifo_dir);
    Method main = Class.forName(class_name).getMethod("main", String[].class);

    String decaf_init = System.getenv("DECAF_INIT");
    if (decaf_init != null) mainInvoke(main, decaf_init);

    mainInvoke(main, readline());
    System.exit(0);
  }

  public static void mainInvoke(Method main, String argstring) throws Exception {
    String [] args = argstring.trim().split("\t");
    if (args.length == 1 && args[0].equals("")) {
      args = null;
    }
    main.invoke(null, (Object) args);
  }

  public static void reopenStreams(String fifo_dir) throws FileNotFoundException, IOException {
    System.in.close();
    System.out.close();
    System.err.close();
    System.setIn(new BufferedInputStream(new DelayedFileInputStream(fifo_dir + "/in")));
    System.setOut(new PrintStream(new DelayedFileOutputStream(fifo_dir + "/out")));
    System.setErr(new PrintStream(new DelayedFileOutputStream(fifo_dir + "/err")));
  }

  public static String readline() throws IOException {
    StringBuffer s = new StringBuffer();

    while (true) {
      char c = (char) System.in.read();
      if (c == '\n' || c == -1) break;
      s.append(c);
    }
    return s.toString();
  }
}

@@ -271,70 +271,104 @@
 n = 
-Class.forName(class_name).getMethod(%22main%22, String%5B%5D.class
+mainMethod(class_name);%0A%0A    Method init       = mainMethod(System.getenv(%22DECAF_INIT_CLASS%22)
 );%0A
-%0A
     
@@ -374,26 +374,26 @@
  String 
-decaf_init
+init_args 
  = Syste
@@ -424,26 +424,25 @@
     if (
-decaf_init
+init_args
  != null
@@ -458,24 +458,45 @@
 oke(
-main, decaf_init
+init == null ? main : init, init_args
 );%0A%0A
@@ -546,32 +546,292 @@
 m.exit(0);%0A  %7D%0A%0A
+  private static Method mainMethod(String class_name)%0A    throws ClassNotFoundException, NoSuchMethodException %7B%0A    if (class_name != null) %7B%0A      return Class.forName(class_name).getMethod(%22main%22, String%5B%5D.class);%0A    %7D else %7B%0A      return null;%0A    %7D%0A  %7D%0A%0A
   public static 

use DECAF_INIT_CLASS to specify the main class for init call