/*
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package org.hyperic.sigar.cmd;

import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import org.hyperic.sigar.SigarLoader;
/**
 * Display Sigar, java and system version information.
 */
public class Version extends SigarCommandBase {

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

    public Version() {
        super();
    }

    public String getUsageShort() {
        return "Display sigar and system version info";
    }

    private static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "unknown";
        }
    }

    private static String getFQDN() {
        Sigar sigar = new Sigar(); 
        try {
            return sigar.getFQDN();
        } catch (SigarException e) {
            return "unknown";
        } finally {
            sigar.close();
        }
    }

    private static void printNativeInfo(PrintStream os) {
        String version =
            "java=" + Sigar.VERSION_STRING +
            ", native=" + Sigar.NATIVE_VERSION_STRING;
        String build =
            "java=" + Sigar.BUILD_DATE +
            ", native=" + Sigar.NATIVE_BUILD_DATE;
        
        os.println("Sigar version......." + version);
        os.println("Build date.........." + build);
        os.println("Archlib............." +
                   SigarLoader.getNativeLibraryName());

        String fqdn = getFQDN();
        String host = getHostName();

        os.println("Current fqdn........" + fqdn);
        if (!fqdn.equals(host)) {
            os.println("Hostname............" + host);
        }        
    }
    
    public static void printInfo(PrintStream os) {
        try {
            printNativeInfo(os);
        } catch (UnsatisfiedLinkError e) {
            os.println("*******ERROR******* " + e);
        }

        os.println("Current user........" +
                   System.getProperty("user.name"));
        os.println("");
        
        OperatingSystem sys = OperatingSystem.getInstance();
        os.println("OS description......" + sys.getDescription());
        os.println("OS name............." + sys.getName());
        os.println("OS arch............." + sys.getArch());
        os.println("OS version.........." + sys.getVersion());
        os.println("OS patch level......" + sys.getPatchLevel());
        os.println("OS vendor..........." + sys.getVendor());
        os.println("OS vendor version..." + sys.getVendorVersion());
        if (sys.getVendorCodeName() != null) {
            os.println("OS code name........" + sys.getVendorCodeName());
        }
        os.println("OS data model......." + sys.getDataModel());
        os.println("OS cpu endian......." + sys.getCpuEndian());

        os.println("Java vm version....." + 
                   System.getProperty("java.vm.version"));
        os.println("Java vm vendor......" + 
                System.getProperty("java.vm.vendor"));
        os.println("Java home..........." +
                System.getProperty("java.home"));
    }

    public void output(String[] args) {
        printInfo(this.out);
    }

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

@@ -759,16 +759,37 @@
 r.cmd;%0A%0A
+import java.io.File;%0A
 import j
@@ -1562,263 +1562,8 @@
  %7D%0A%0A
-    private static String getFQDN() %7B%0A        Sigar sigar = new Sigar(); %0A        try %7B%0A            return sigar.getFQDN();%0A        %7D catch (SigarException e) %7B%0A            return %22unknown%22;%0A        %7D finally %7B%0A            sigar.close();%0A        %7D%0A    %7D%0A%0A
     
@@ -1857,32 +1857,106 @@
 _DATE;%0A        %0A
+        String archlib =%0A            SigarLoader.getNativeLibraryName();%0A%0A
         os.print
@@ -2045,24 +2045,25 @@
 build);%0A
+%0A
         
 os.print
@@ -2058,43 +2058,93 @@
     
-os.println(%22Archlib.............%22 +
+String host = getHostName();%0A        String fqdn;%0A        Sigar sigar = new Sigar(); 
 %0A   
@@ -2140,32 +2140,38 @@
 gar(); %0A        
+try %7B%0A
            Sigar
@@ -2169,45 +2169,131 @@
     
-SigarLoader.getNativeLibrary
+ File lib = sigar.getNativeLibrary();%0A            if (lib != null) %7B%0A                archlib = lib.get
 Name()
-)
 ;%0A
+            %7D
 %0A   
@@ -2301,22 +2301,19 @@
     
-String
+   
  fqdn = 
 getF
@@ -2308,16 +2308,22 @@
  fqdn = 
+sigar.
 getFQDN(
@@ -2337,34 +2337,176 @@
     
-String host = getHostName(
+%7D catch (SigarException e) %7B%0A            fqdn = %22unknown%22;%0A        %7D finally %7B%0A            sigar.close();%0A        %7D%0A%0A        os.println(%22Archlib.............%22 + archlib
 );%0A%0A

use getNativeLibrary for archlib