package moduly.sbor;


import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;

public class ProcessSbor implements Runnable {

    private final File sbor_file;

    public ProcessSbor(File sbor_file) {
        this.sbor_file = sbor_file;
    }

//--------------------------------------------------------------

    public void sborMac() {
        try {
            PrintWriter pw = new PrintWriter(sbor_file);
            String s = null;
            RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
            List<String> inputArguments = runtimeMXBean.getInputArguments();

            for (String argument : inputArguments) {
                s += argument + "\n";
            }
            pw.println(s);
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    public void sborWin() {
        try {
            PrintWriter pw = new PrintWriter(sbor_file);
            String line;
            Process p = Runtime.getRuntime().exec
                    (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
            BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = input.readLine()) != null) {
                pw.println(line); //<-- Parse data here.
            }
            input.close();

            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//--------------------------------------------------------------

    @Override
    public void run() {
        try {
            if (new InfoPC().isWindows()) {
                sborWin();
            } else {
                sborMac();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //sbor();
    }

//--------------------------------------------------------------
}
