package main.test.java.com.magtech.nlmdtd2richxml.text;

import java.io.IOException;
import java.io.PrintWriter;

public class CommandExecutor extends Thread {

    private final static boolean bDebug = true;
    public static final String WINDOWS_95 = "WINDOWS 95";
    public static final String WINDOWS_98 = "WINDOWS 98";
    public static final String WINDOWS_NT = "WINDOWS NT";
    public static final String WINDOWS_2K = "WINDOWS 2000";
    public static final String WINDOWS_2003 = "WINDOWS 2003";
    public static final String WINDOWS_VISTA = "WINDOWS VISTA";

    public static final String WINDOWS_7 = "WINDOWS 7";

    public static final String WINDOWS_XP = "WINDOWS XP";
    public static final String LINUX = "LINUX";
    public static final String SOLARIS = "SOLARIS";
    public static final String AIX = "AIX";
    public static final String FREEBSD = "FREEBSD";
    public static final String IRIX = "IRIX";
    public static final String HP_UX = "HP-UX";

    public static String exec(String[] sCmd) throws Exception {
        System.out.println("execute commands:");
        for (int i = 0; i < sCmd.length; i++) {
            System.out.println(sCmd[i]);
        }
        String strRet = "";
        String sOS = System.getProperty("os.name").toUpperCase();
        if (sOS.equals(WINDOWS_2K) || sOS.equals(WINDOWS_NT) || sOS.equals(WINDOWS_XP) || sOS.equals(WINDOWS_2003) || sOS.equals(WINDOWS_7)|| sOS.equals(WINDOWS_VISTA))
            strRet = execInWinNT(sCmd);
        else if (sOS.equals(WINDOWS_95) || sOS.equals(WINDOWS_98))
            strRet = execInWin9x(sCmd);
        else if (sOS.equals(LINUX) || sOS.equals(FREEBSD) || sOS.equals(AIX) || sOS.equals(IRIX)
                || sOS.equals(SOLARIS) || sOS.equals(HP_UX))
            strRet = execInUNIX(sCmd);
        else {
            throw new Exception("Unknown OS:" + sOS);
        }
        return strRet;
    }

    public static String exec(String sCmd) throws Exception {
        String[] sCmds = new String[1];
        sCmds[0] = sCmd;
        return exec(sCmds);
    }

    private static String execInWin9x(String sCmd[]) {
        String strRet = "";
        try {
            Runtime aRT = Runtime.getRuntime();
            Process aProc = aRT.exec("command.com");
            strRet = exec(aProc, sCmd);
        } catch (IOException ex) {
            if (bDebug)
                System.out.println("CommandExecutor.execInWin9x:error");
        }

        return strRet;
    }

    private static String execInWinNT(String sCmd[]) {
        String strRet = "";
        try {
            Runtime aRT = Runtime.getRuntime();
            Process aProc = aRT.exec("cmd.exe");
            strRet = exec(aProc, sCmd);
        } catch (IOException ex) {
            if (bDebug)
                System.out.println("CommandExecutor.execInWinNT:error");
        }
        return strRet;
    }

    private static String execInUNIX(String sCmd[]) {
        String strRet = "";
        try {
            Runtime aRT = Runtime.getRuntime();
            Process aProc = aRT.exec("bash");
            strRet = exec(aProc, sCmd);
        } catch (IOException ex) {
            if (bDebug)
                System.out.println("CommandExecutor.execInUNIX:error" + ex.getMessage() + "<br>");
        }

        return strRet;
    }

    private static String exec(Process aProc, String sCmd[]) {
        String strRet = "";
        try {
            strRet = "";
            StreamGobbler errGrobbler = new StreamGobbler(aProc.getErrorStream(), "ERROR");
            errGrobbler.start();
            System.out.println("CommandExecutor.cmd : error " + errGrobbler.getLog());
            StreamGobbler outputGrobbler = new StreamGobbler(aProc.getInputStream(), "OUTPUT");
            outputGrobbler.start();
            PrintWriter shell = new PrintWriter(aProc.getOutputStream());
            for (int i = 0; i < sCmd.length; i++) {
                if (bDebug) {
                    System.out.println("CommandExecutor.cmd : output " + sCmd[i]);
                }
                shell.println(sCmd[i]);
                shell.flush();
            }
            shell.close();

            int exitVal = aProc.waitFor();
            String strErr = errGrobbler.getLog();
            String strOut = outputGrobbler.getLog();
            if ((strErr != null) && !(strErr.equals(""))) {
                strRet += strErr;
            }
            if ((strOut != null) && !(strOut.equals(""))) {
                strRet += strOut;
            }
            if (bDebug) {
                System.out.println("ExitValue : " + exitVal);
            }
        } catch (Exception ioex) {
            if (bDebug) {
                System.out.println("CommandExecutor: io error");
            }
        }
        return strRet;
    }

}
