<?xml version="1.0" encoding="UTF-8"?>
<installer-gui-script minSpecVersion='1'>
    <script>
	var updateName = "HardDrivePerformanceUpdate";

	var EntityToFind = "AppleMCP79AHCI";
	var propertyToTest = "Chipset Name";
	var propertyMustHaveValue = "MCP79 AHCI";

	var path = "/System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext";
	var minVersion = "0";
	var LeopardMaxVersion = "1.2.3"
	var SnowLeopardMaxVersion = "1.6.0";
</script>
    <title>SU_TITLE</title>
    <welcome file="SUDescription.html"/>
    <license auto='false' file="License.rtf" sla="EA0325"/>
    <background file="background.tif" scaling="proportional" alignment="bottomleft"/>
    <options hostArchitectures='i386,ppc' customize='never' rootVolumeOnly='true'/>
    <platforms>
        <client arch="i386,ppc"/>
        <server arch="i386,ppc"/>
    </platforms>
    <!-- Manual -->
    <choices-outline>
        <line choice='manual'/>
    </choices-outline>
    <choice id='manual' title='SU_TITLE' versStr='SU_VERS'>
        <pkg-ref auth='Root' id='manual' onConclusion="RequireRestart">#PerformanceUpdate.pkg</pkg-ref>
    </choice>
    <!-- SU -->
    <choices-outline ui='SoftwareUpdate'>
        <line choice='su'/>
    </choices-outline>
    <choice id='su' title='SU_TITLE' description='SU_DESCRIPTION' description-mime-type='text/html' versStr='SU_VERS' suDisabledGroupID='HDDUpdate10' visible='needsUpdate(path,minVersion,LeopardMaxVersion)' start_selected='true'>
        <pkg-ref auth='Root' id='su' onConclusion="RequireRestart">PerformanceUpdate.pkg</pkg-ref>
    </choice>
    <installation-check script='InstallationCheck()'/>
    <script>
function InstallationCheck()
{
	if (system.env.OS_INSTALL == 1) 
		return true;

	debuglog("needsUpdate returned " + needsUpdate(path,minVersion,LeopardMaxVersion));

	// if they don't have harware that needs the kext
	if ( !hasValidHW() ) 
	{
		debuglog("hw check fail");
		my.result.title		= system.localizedStringWithFormat('TITLE_HW');
		my.result.message	= system.localizedStringWithFormat('ERR_HW');
		my.result.type		= 'Fatal';
		return false;	       
	}
	
	// if they have Snow, but do not have a new enough kext (they do need the other update)
	if ( hasAnySnowLeopard() &amp;&amp; needsUpdate(path,minVersion,SnowLeopardMaxVersion) )
	{
		debuglog("Snow Leopard system could use the update.");
		my.result.title		= system.localizedStringWithFormat('TITLE_SNOW');
		my.result.message	= system.localizedStringWithFormat('ERR_SNOW');
		my.result.type		= 'Fatal';
		return false;	       
	}

	// if they have Snow, but have a new enough kext (they don't need the other update)
	if ( hasAnySnowLeopard() &amp;&amp; !needsUpdate(path,minVersion,SnowLeopardMaxVersion) )
	{
		debuglog("Snow leopard system already has a newer kext.");
		my.result.title		= system.localizedStringWithFormat('TITLE_NEWER');
		my.result.message	= system.localizedStringWithFormat('ERR_NEWER');
		my.result.type		= 'Fatal';
		return false;	       
	}
	
	// They must have 10.5 (otherwise this package wouldn't be running) but not 10.6 (because we test for that above)
	// So if they already have a new enough kext...
	if ( !canHasUpdate(path,minVersion,LeopardMaxVersion) ) 
	{
		debuglog("Leopard system already has a newer kext version.");
		my.result.title		= system.localizedStringWithFormat('TITLE_NEWER');
		my.result.message	= system.localizedStringWithFormat('ERR_NEWER');
		my.result.type		= 'Fatal';
		return false;	       
	}
	
	// Do they have a too-old Leopard?
	if ( !hasGoodLeopard() ) 
	{
		debuglog("Leopard system too old.");
		my.result.title		= system.localizedStringWithFormat('TITLE_OS');
		my.result.message	= system.localizedStringWithFormat('ERR_OS');
		my.result.type		= 'Fatal';
		return false;	       
	}
	
	debuglog("InstallationCheck passes.");
	return true;	   
}

function hasGoodLeopard()
{
	// if boot disk has 10.5.8 or newer, but not 10.6 or newer, it's good
	return ((-1 != system.compareVersions(system.version.ProductVersion, '10.5.8')) &amp;&amp;
			(-1 == system.compareVersions(system.version.ProductVersion, '10.6')));
}

function hasAnySnowLeopard()
{
	return (-1 != system.compareVersions(system.version.ProductVersion, '10.6'));
}


function hasValidHW()
{
	var devicesToTestArray = system.ioregistry.matchingClass(EntityToFind);					

	// avoid a bug... try a few times
	for(var index=0; index &lt; 5; index++) 
	{
		if (devicesToTestArray.length != 0) break;

		devicesToTestArray = system.ioregistry.matchingClass(EntityToFind);
	}

	debuglog("hasValidHW found " + devicesToTestArray.length + " " + EntityToFind + " entities.");
	
	// Check each of the items returned, see what version it has
	for(var index=0; index &lt; devicesToTestArray.length; index++) 
	{
	
		// check drive property
		var device = devicesToTestArray[index];
		var propertyValue = device[propertyToTest];
		if (propertyValue)
		{
			debuglog("	Entity #" + index + ": has propertyValue " + propertyValue);
			if (propertyValue != propertyMustHaveValue) 
			{
				debuglog("	Entity #" + index + ": wrong propertyValue.");
				continue;
			}
			
			// we have a winner!
			debuglog("	Entity #" + index + ": matches -- WIN.");
			return true;

		}
		else debuglog("	Entity #" + index + ": no propertyValue?!?!!");

	}
	
	debuglog("hasValidHW: No drives need update.");
	return false;
}

///////////////////////////////////////////////
function canHasUpdate(bundlePath, minVersionString, maxVersionString)
{
	debuglog("canHasUpdate bundleVersion is " + PathGetShortVersion(bundlePath));
	return ((system.compareVersions(PathGetShortVersion(bundlePath),minVersionString) >= 0) &amp;&amp;
			(system.compareVersions(PathGetShortVersion(bundlePath),maxVersionString) &lt;= 0));
}

function needsUpdate(bundlePath, minVersionString, maxVersionString)
{
	debuglog("needsUpdate bundleVersion is " + PathGetShortVersion(bundlePath));
	return ((system.compareVersions(PathGetShortVersion(bundlePath),minVersionString) >= 0) &amp;&amp;
			(system.compareVersions(PathGetShortVersion(bundlePath),maxVersionString) &lt;0));
}



///////////////////////////////////////////////
// converting decimal to hex and vice versa
function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}


function PathGetShortVersion(path)
{
	var bundle = system.files.bundleAtPath(path);
	if (bundle == null)
		return "0.0.0";
	var shortVers = bundle.CFBundleShortVersionString;
	shortVers = _PadVersionString(shortVers, 3);
	return shortVers;
}

function _PadVersionString(version, tupleCount)
{
	if (version == null)
		version = "0";
	var components = version.split(".");
	if (components.length > tupleCount)
		components = components.slice(0, tupleCount);
	else
		for (; components.length&lt;tupleCount; )
			components.push("0");
	return components.join(".");
}

function oc(a)
{
	var o = {};
	for(var i=0;i &lt; a.length;i++)
	{
		o[a[i]]='';
	}
	return o;
}

function debuglog(message)
{
	if (system.files.fileExistsAtPath("/tmp/com.apple.pkg.testing")) system.log("********** " + updateName + ": " + message);
}
	</script>
    <localization>
        <strings language="pl"><![CDATA["SU_TITLE"			= "Uaktualnienie podnoszące wydajność";
"SU_VERS"			= "1.0";
"SU_SERVERCOMMENT"	= "Dla komputerów klienckich i serwerów.";

"SU_DESCRIPTION"='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <style type="text/css">
	body {font: 11.0px Lucida Grande;}
    p {margin: 0.0px 0.0px 0.0px 0.0px;}
  </style>
</head>
<body>
<p>
To uaktualnienie rozwiązuje problem sporadycznych i zgłaszanych przez nielicznych użytkowników spadków wydajności komputera związanych z pracą dysku twardego.
</p>
</body>
</html>
';
]]></strings>
    </localization>

	<pkg-ref id='su' installKBytes='590' version='1.0.1.1191932192'/>
</installer-gui-script>