package net.hyperic.sigar.cmd;

import net.hyperic.sigar.SigarException;

import java.io.File;
import java.io.FilenameFilter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * Display System Information
 */
public class SysInfo extends SigarCommandBase {

    public SysInfo(Shell shell) {
        super(shell);
    }

    public SysInfo() {
        super();
    }

    public String getUsageShort() {
        return "Display system information";
    }

    private static boolean contains(String value, String substr) {
        //matches does not work w/ jre 1.3
        //value.toLowerCase().matches(".*" + substr + ".*");
        return value.toLowerCase().indexOf(substr) != -1;
    }

    public void output(String[] args) throws SigarException {
        String osName = System.getProperty("os.name");

        /**
         * OS info
         */
        this.out.println(osName + " " +
            System.getProperty("os.version") + " " +
            System.getProperty("os.arch"));

        /**
         * craziness to try to determine the linux distro
         */
        if (osName.equals("Linux")) {
            File etc = new File("/etc");
            File[] release = etc.listFiles(new FilenameFilter()
            {
                public boolean accept(File dir, String name)
                {
                    boolean distroFile = contains(name, "elease");

                    if (!distroFile) {
                        distroFile = contains(name, "ersion");
                    }
                    return distroFile;
                }
            });

            if (release.length != 1) {
                this.out.println("Cannot determine Linux distribution!");
                if (release.length > 1) {
                    this.out.println("Possible version files:");
                    for (int i = 0 ; i < release.length ; i++) {
                        this.out.println("  " + release[i]);
                    }
                }
            }
            else {
                try {
                    //this.out.println(release[0].getAbsoluteFile() + ":");
                    BufferedReader rel = new BufferedReader(
                        new FileReader(release[0]));
                    String line;
                    while ((line = rel.readLine()) != null) {
                        this.out.println(line);
                    }
                }
                catch (IOException e) {
                    this.out.println("Cannot determine Linux distribution!");
                }
            }
        }

        /**
         * JVM info
         */
        this.out.println(System.getProperty("java.vm.name") + " - " +
            System.getProperty("java.vm.vendor") + " [" +
            System.getProperty("java.vm.version") + "]");
        this.out.println(System.getProperty("java.specification.vendor") + " " +
            System.getProperty("java.specification.name") + " " +
            System.getProperty("java.specification.version"));
        this.out.println("JAVA_HOME=" + System.getProperty("java.home"));
        this.out.println("");

        /**
         * uptime
         */
        new Uptime(this.shell).output(args);
        this.out.println("");

        /**
         * CPU info
         */
        net.hyperic.sigar.CpuInfo[] infos =
            this.sigar.getCpuInfoList();
        for (int i=0; i<infos.length; i++) {
            net.hyperic.sigar.CpuInfo info = infos[i];
            this.out.println("CPU " + i + ":");
            this.out.println("  Vendor........" + info.getVendor());
            this.out.println("  Model........." + info.getModel());
            this.out.println("  Mhz..........." + info.getMhz());
            this.out.println("  Cache size...." + info.getCacheSize());
        }
        this.out.println("");

        /**
         * memory info
         */
        new Free(this.shell).output(args);
    }

    public static void main(String[] args) throws Exception {
        new SysInfo().processCommand(args);
    }
}

@@ -54,152 +54,55 @@
 gar.
-SigarException;%0A%0Aimport java.io.File;%0Aimport java.io.FilenameFilter;%0Aimport java.io.FileReader;%0Aimport java.io.BufferedReader;%0Aimport java.io.IO
+OperatingSystem;%0Aimport net.hyperic.sigar.Sigar
 Exce
@@ -803,437 +803,155 @@
     
-this.out.println(osName + %22 %22 +%0A            System.getProperty(%22os.version%22) + %22 %22 +%0A            System.getProperty(%22os.arch%22));%0A%0A        /**%0A         * craziness to try to determine the linux distro%0A         */%0A        if (osName.equals(%22Linux%22)) %7B%0A            File etc = new File(%22/etc%22);%0A            File%5B%5D release = etc.listFiles(new FilenameFilter()%0A            %7B%0A                public boolean accept(File dir, String name)
+OperatingSystem os = OperatingSystem.getInstance();%0A%0A        this.out.println(os.getName() + %22 %22 +%0A                         os.getVersion() + %22 %22 +
 %0A   
@@ -960,17 +960,8 @@
     
-       %7B%0A
     
@@ -976,1221 +976,128 @@
     
-    boolean distroFile = contains(name, %22elease%22);%0A%0A                    if (!distroFile) %7B%0A                        distroFile = contains(name, %22ersion%22);%0A                    %7D%0A                    return distroFile;%0A                %7D%0A            %7D);%0A%0A            if (release.length != 1) %7B%0A                this.out.println(%22Cannot determine Linux distribution!%22);%0A                if (release.length %3E 1) %7B%0A                    this.out.println(%22Possible version files:%22);%0A                    for (int i = 0 ; i %3C release.length ; i++) %7B%0A                        this.out.println(%22  %22 + release%5Bi%5D);%0A                    %7D%0A                %7D%0A            %7D%0A            else %7B%0A                try %7B%0A                    //this.out.println(release%5B0%5D.getAbsoluteFile() + %22:%22);%0A                    BufferedReader rel = new BufferedReader(%0A                        new FileReader(release%5B0%5D));%0A                    String line;%0A                    while ((line = rel.readLine()) != null) %7B%0A                        this.out.println(line);%0A                    %7D%0A                %7D%0A                catch (IOException e) %7B%0A                    this.out.println(%22Cannot determine Linux distribution!%22);%0A                %7D%0A            %7D
+os.getArch());%0A%0A        if (osName.equals(%22Linux%22)) %7B%0A            println(os.getVendor() + %22 %22 + os.getVendorVersion());
 %0A   

use OS class to get linux vendor info