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

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

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

    String [] args = readline().substring(1).split("\t");

    if (args.length == 1 && args[0].equals("")) {
      args = null;
    }

    main.invoke(null, (Object) args);
    System.exit(0);
  }

  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();
  }
}

@@ -128,21 +128,16 @@
 tring%5B%5D 
-init_
 args) th
@@ -177,21 +177,16 @@
 _name = 
-init_
 args%5B0%5D;
@@ -210,15 +210,12 @@
 dir 
+  
 = 
-init_
 args
@@ -344,40 +344,286 @@
 ing 
-%5B%5D args = readline().substring(1
+decaf_init = System.getenv(%22DECAF_INIT%22);%0A    if (decaf_init != null) mainInvoke(main, decaf_init);%0A%0A    mainInvoke(main, readline());%0A    System.exit(0);%0A  %7D%0A%0A  public static void mainInvoke(Method main, String argstring) throws Exception %7B%0A    String %5B%5D args = argstring.trim(
 ).sp
@@ -633,17 +633,16 @@
 (%22%5Ct%22);%0A
-%0A
     if (
@@ -708,17 +708,16 @@
 ;%0A    %7D%0A
-%0A
     main
@@ -746,36 +746,16 @@
  args);%0A
-    System.exit(0);%0A
   %7D%0A%0A  p

add support for DECAF_INIT env variable