package dgcj;

import java.io.*;

public class LocalTesting {
	public static final File logFile = new File("log~.txt");

	public static void run(String problemName, int nodes) {
		if ("TemplateDgcjProblem".equals(problemName)) {
			throw new AssertionError("Change PROBLEM field in Main!");
		}
		logFile.delete();
		String exec = "./dgcj_tool~/dcj.sh"
				+ " test"
				+ " --source Main.java"
				+ " --library " + problemName + ".java"
				+ " --nodes " + nodes;
		System.out.println("Running: " + exec);
		try {
			System.out.println(execute(exec));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		try {
			if (logFile.exists()) {
				System.out.println(readToEnd(new FileReader(logFile)));
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static String readToEnd(InputStream is) throws IOException {
		return readToEnd(new BufferedReader(new InputStreamReader(is)));
	}

	public static String readToEnd(Reader reader) throws IOException {
		BufferedReader br = new BufferedReader(reader);
		StringBuilder sb = new StringBuilder();
		String s;
		while ((s = br.readLine()) != null) {
			sb.append(s).append('\n');
		}
		br.close();
		s = sb.toString();
		if (!s.isEmpty()) {
			s = s.substring(0, s.length() - 1);
		}
		return s;
	}

	public static String execute(String exec) throws IOException {
		Process p = Runtime.getRuntime().exec(exec);
		try {
			p.waitFor();
		} catch (InterruptedException e) {
			throw new IOException(e);
		}
		return readToEnd(p.getInputStream());
	}
	
	public static void log(String message) {
		try {
			PrintWriter pw = new PrintWriter(new FileOutputStream(logFile, true));
			pw.println(message);
			pw.close();
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -613,16 +613,45 @@
 e);%0A%09%09%7D%0A
+%09%09new File(%22Main%22).delete();%0A
 %09%09try %7B%0A

Remove temp sh file after invokation