package net.swisstech.swissarmyknife.sys.linux;

import static net.swisstech.swissarmyknife.sys.linux.Signal.TERM;

import java.io.File;
import java.util.Collection;
import java.util.List;

/**
 * simple util to launch a process, wait for a number of open ports and kill the process.
 * @since 1.1.5
 */
public final class BackgroundProcess {

	private final ProcessWrapper process;

	private BackgroundProcess(ProcessWrapper process) {
		this.process = process;
	}

	public static BackgroundProcess launch(List<CharSequence> cmd, File workingDir) {
		ProcessWrapper process = ProcessWrapper.launch(cmd, workingDir);
		return new BackgroundProcess(process);
	}

	public BackgroundProcess waitForOpenPorts(Collection<Integer> ports, long timeout) {
		PortSnoop.waitForOpenPorts(process, ports, timeout);
		return this;
	}

	int shutdown() {
		return process.killWait(TERM);
	}
}

@@ -49,23 +49,16 @@
 %0Aimport 
-static 
 net.swis
@@ -82,29 +82,25 @@
 ife.
-sys.linux.Signal.TERM
+lang.InputStreams
 ;%0A%0Ai
@@ -119,16 +119,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -294,16 +294,19 @@
 rocess.%0A
+ *%0A
  * @sinc
@@ -836,16 +836,403 @@
 s;%0A%09%7D%0A%0A%09
+public BackgroundProcess waitForStringInStdout(String needle, long timeout) throws IOException %7B%0A%09%09InputStreams.waitForOutput(needle, process.getInputStream(), timeout);%0A%09%09return this;%0A%09%7D%0A%0A%09public BackgroundProcess waitForStringInStderr(String needle, long timeout) throws IOException %7B%0A%09%09InputStreams.waitForOutput(needle, process.getErrorStream(), timeout);%0A%09%09return this;%0A%09%7D%0A%0A%09public 
 int shut
@@ -1266,16 +1266,23 @@
 illWait(
+Signal.
 TERM);%0A%09

add waitForStringInStd{our|err} which will wait for a string to appear and then return (uses new InputStreams util)