// Filename: SA.js
// The JavaSscipt code for the Sys Admin Widget
//
// Date: Saturday 27 August 2005
// Creator: Mihalis Tsoukalos
//
// This file is provided without any warranties.
// This file is provided for demonstration purposes.


var timerInterval = null;

function getData()
{
	var OUTPUT_DATA = "";

	// The UNIX uptime command contains both uptime and load average info.
	var uptime = widget.system("/usr/bin/uptime",null).outputString;
	var load_average = uptime;

	// uptime data
	OUTPUT_DATA += "<u>Uptime</u>: ";
	pre=uptime.split("up");
    uptime=pre[1];
    sec=uptime.split(",");
    uptime='';
    for(y=0; y<sec.length-1; y++)
    {
    	if(sec[y].match("day") || sec[y].match("mins") ||  sec[y].match(":")||  sec[y].match("hrs")||  sec[y].match("hour"))
    	{
    		uptime+=sec[y];
    	}
	}

	OUTPUT_DATA += uptime;

	// load average data
	OUTPUT_DATA += "<br><u>Load average</u>: ";
	pre = load_average.split("averages: ");
	load_average = pre[1];
	OUTPUT_DATA += load_average;

	// Hard Disk Usage data
	var df = widget.system("/bin/df -l -m",null).outputString;
	par = df.split(/(\n)/);
	df = "";

	OUTPUT_DATA += "<br><u>Disk information:</u>";

	// Staring the df table.
	OUTPUT_DATA += "<table bgcolor='#4F4F4F' border cellspacing=0 cellpadding=2>";

	// The table header column.
	OUTPUT_DATA += "<tr>";
	OUTPUT_DATA += "<td style='font-size:9px;color:lightgray;'>Mount Point</td>";
	OUTPUT_DATA += "<td style='font-size:9px;color:lightgray;'>Size</td>";
	OUTPUT_DATA += "<td style='font-size:9px;color:lightgray;'>Occupied</td>";
	OUTPUT_DATA += "<td style='font-size:9px;color:lightgray;'>Free</td>";
	OUTPUT_DATA += "<td style='font-size:9px;color:lightgray;'>Used</td>";
	OUTPUT_DATA += "</tr>";

	for(y=1; y<par.length-1; y++)
	{
		df += "<tr>";

		// mount point
		details = par[y].split("%");
		df += "<td nowrap style='font-size:9px;align:center;'>";
		df += details[1] + "</td>";

		details = par[y].split(/\s+/);

		// size
		details[1] = parseFloat(details[1])/1024;
		df += "<td nowrap style='font-size:10px;align:center;'>";
		df += details[1].toFixed(2) + "Gb</td>";
		
		// Occupied
		details[2] = parseFloat(details[2])/1024;
		df += "<td nowrap style='font-size:10px;align:center;'>";
		df += details[2].toFixed(2) + "Gb</td>";
		
		// free
		details[3] = parseFloat(details[3])/1024;
		df += "<td nowrap style='font-size:10px;align:center;'>";
		df += details[3].toFixed(2) + "Gb</td>";

		// used
		df += "<td nowrap style='font-size:10px;align:center;'>";
		df += details[4] + "</td>";
		
		df += "</tr>";
	}
	OUTPUT_DATA += df;

	// Finishing the df table.
	OUTPUT_DATA += "</table>";

	document.getElementById("OurInfo").innerHTML = OUTPUT_DATA;
}

function UpdateWidget()
{
	if (window.widget)
	{
		widget.onshow = show; 
		widget.onhide = hide;  
	}
	show();
}

function show()
{
    getData();
    if (timerInterval == null)
    { 
        timerInterval = setInterval("UpdateWidget();", 5000);
    }
}

function hide()
{
    if (timerInterval != null)
    {
        clearInterval(timerInterval); 
        timerInterval = null; 
    }
}
