/*
 * Created by IntelliJ IDEA.
 * User: yole
 * Date: 28.09.2006
 * Time: 19:06:36
 */

import java.util.*;
import java.io.*;

public class P4Info {
    private static final String OS_NAME = System.getProperty("os.name").toLowerCase();
    public static final String OS_VERSION = System.getProperty("os.version").toLowerCase();
    public static final String JAVA_VERSION = System.getProperty("java.version");
    public static final String JAVA_RUNTIME_VERSION = System.getProperty("java.runtime.version");

    public static final boolean isWindows = OS_NAME.startsWith("windows");
    public static final boolean isWindowsNT = OS_NAME.startsWith("windows nt");
    public static final boolean isWindows2000 = OS_NAME.startsWith("windows 2000");
    public static final boolean isWindows2003 = OS_NAME.startsWith("windows 2003");
    public static final boolean isWindowsXP = OS_NAME.startsWith("windows xp");
    public static final boolean isWindows9x = OS_NAME.startsWith("windows 9") || OS_NAME.startsWith("windows me");
    public static final boolean isOS2 = OS_NAME.startsWith("os/2") || OS_NAME.startsWith("os2");
    public static final boolean isMac = OS_NAME.startsWith("mac");
    public static final boolean isFreeBSD = OS_NAME.startsWith("freebsd");
    public static final boolean isLinux = OS_NAME.startsWith("linux");
    public static final boolean isUnix = !isWindows && !isOS2;

    public static void main(String[] args) {
        String cmd = "p4 info";
        final Runtime rt = Runtime.getRuntime();
        try {
            final Process process = rt.exec(cmd, getEnvironment(), new File(args[0]));
            final InputStream inputStream = process.getInputStream();
            while(true) {
                int i = inputStream.read();
                if (i == -1) break;
                System.out.print((char) i);
            }

            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String[] getEnvironment() {
      Map enviroment = getEnviromentProperties();
      String[] envp = new String[enviroment.size()];
      int i = 0;
      for (Iterator iterator = enviroment.keySet().iterator(); iterator.hasNext();) {
        String name = (String)iterator.next();
        String value = (String)enviroment.get(name);
        envp[i++] = name + "=" + value;
      }
      return envp;
    }

    public static Map<String, String> getEnviromentProperties() {
        List vars = getProcEnvironment();
        HashMap<String, String> ourEnviromentProperties = new HashMap<String, String>();
        if (vars != null) {
          for (Iterator i = vars.iterator(); i.hasNext();) {
            String entry = (String)i.next();
            int pos = entry.indexOf('=');
            if (pos != -1) {
              String prop = entry.substring(0, pos);
              String val = entry.substring(pos + 1);
              //if (SystemInfo.isWindows) prop = prop.toLowerCase();
              ourEnviromentProperties.put(prop, val);
            }
          }
        }
        return ourEnviromentProperties;
    }

    private static synchronized List getProcEnvironment() {
      List procEnvironment = new ArrayList();

      try {
        String[] procEnvCommand = getProcEnvCommand();
        Process process = Runtime.getRuntime().exec(procEnvCommand);
        if (process == null) return null;
        InputStream processIn = process.getInputStream();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while (true) {
          int b = processIn.read();
          if (b < 0) break;
          out.write(b);
        }
        int exitCode = process.waitFor();
        if (exitCode < 0) return null;
//      process.destroy();

        BufferedReader in = new BufferedReader(new StringReader(out.toString()));

        String var = null;
        String line;
        String lineSep = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
          if (line.indexOf('=') == -1) {
            if (var == null) {
              var = lineSep + line;
            }
            else {
              var += lineSep + line;
            }
          }
          else {
            if (var != null) {
              procEnvironment.add(var);
            }
            var = line;
          }
        }
        if (var != null) {
          procEnvironment.add(var);
        }
      }
      catch (Throwable exc) {
          exc.printStackTrace();
      }

      return procEnvironment;
    }

    private static String[] getProcEnvCommand() {
      if (isOS2) {
        String[] cmd = {"cmd", "/c", "set"};
        return cmd;
      }
      else if (isWindows) {
        if (!isWindows9x) {
          String[] cmd = {"cmd", "/c", "set"};
          return cmd;
        }
        else {
          String[] cmd = {"command.com", "/c", "set"};
          return cmd;
        }
      }
      else if (isUnix) {
        String[] cmd = {"/usr/bin/env"};
        return cmd;
      }

      return null;
    }
}