package de.hsadmin.core.qserv;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CommandShell {
	
	private static final Logger logger = Logger.getLogger("de.hsadmin.core.qserv");

	
	public static String execute(String command) throws ShellException {
		return execute(command, null);
	}

	public static String execute(String command, String stdInput) throws ShellException {
		Process backend = null;
		String callOutput = null;
		int exitCode = 0;
		try {
			String logCommand = command;
			if (stdInput != null)  {
				logCommand += "<<EOF\n" + stdInput + "EOF";
			}
			logCommand += "\n";
			logger.log(Level.INFO, logCommand);
			String[] cmdArray = { "/bin/bash", "-c", command };
			backend = Runtime.getRuntime().exec(cmdArray);
			if (stdInput != null) {
				OutputStream stdInputStream = backend.getOutputStream();
				PrintWriter stdInputWriter = new PrintWriter(stdInputStream);
				stdInputWriter.print(stdInput);
				stdInputWriter.close();
				stdInputStream.close();
			}
			callOutput = readProcessStream(backend.getInputStream());
			exitCode = backend.waitFor();
			logger.log(Level.INFO, "Return-Code: " + exitCode);
			if (exitCode != 0) {
				String aErr = readProcessStream(backend.getErrorStream());
				logger.log(Level.SEVERE, aErr);
				throw new ShellException(aErr);
			}
		} catch (IOException e) {
			throw new ShellException(e);
		} catch (InterruptedException e) {
			throw new ShellException(e);
		}
		if (callOutput != null) {
			return callOutput.trim();
		}
		return null;
	}

	private static String readProcessStream(InputStream stream) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
		StringBuffer textBuff = new StringBuffer();
		String textLine = reader.readLine();
		while (textLine != null) {
			textBuff.append(textLine);
			textBuff.append('\n');
			textLine = reader.readLine();
		}
		reader.close();
		return textBuff.toString();
	}
}
