!636 // auto-import
!standard functions
!multi-line strings
!quicknew

public class main {
  static String prelude = [[
    public class main {
      public static void main(String[] args) throws Exception {
        System.out.println(calc());
      }

      public static Object calc() throws Exception {
  ]];
  
  static String postlude = [[
      }
    }
  ]];

  public static void main(String[] args) throws IOException {
    String code = loadTextFile("input/main.java", null);
    if (code == null)
      throw new RuntimeException("Nothing to do (no input/main.java)");
    code = prelude + code + postlude;
    code = moveImportsUp(code);
    saveTextFile("output/main.java", code);
  }
  
  static String moveImportsUp(String s) {
    List<String> l = toLines(s);
    new List<String> x;
    Pattern p = Pattern.compile("^\\s*import\\s");
    for (ListIterator<String> i = l.listIterator(); i.hasNext(); ) {
      String line = i.next();
      if (p.matcher(line).find()) {
        x.add(line);
        i.remove();
      }
    }
    x.addAll(l);
    return fromLines(x);
  }
}