<?xml version="1.0" encoding="UTF-8"?>
<installer-gui-script minSpecVersion='1'>
    <script>
var EntityToFind1	= "IOBluetoothHIDDriver";
var EntityToFind2	= "IOAppleBluetoothHIDDriver";
var vendorID		= 0x5AC;
var productIDArray	= [0x239, 0x23A, 0x23B];

var swPath			= "/System/Library/Extensions/AppleHIDKeyboard.kext";
var swMinVersion	= 0;
var	swMaxVersion	= "1.1.2";
</script>
    <title>SU_TITLE</title>
    <readme file='ReadMe.rtf'/>
    <license file="License.rtf" sla="EA0325" auto='false'/>
    <options customize='never' hostArchitectures='ppc,i386' rootVolumeOnly='true'/>
    <platforms>
        <client arch="ppc,i386"/>
        <server arch="ppc,i386"/>
    </platforms>
    <choices-outline ui='SoftwareUpdate'>
        <line choice='su'/>
    </choices-outline>
    <choice id='su' title='SU_TITLE' versStr='SU_VERS' description='SU_DESCRIPTION' description-mime-type='text/html' suDisabledGroupID='AlKybdSoftwareUpdate' visible='canSeeUpdate()'>
        <pkg-ref id='auto' auth='Root' onConclusion='RequireRestart'>AlKybdSoftwareUpdate.pkg</pkg-ref>
    </choice>
    <choices-outline>
        <line choice='manual'/>
    </choices-outline>
    <choice id='manual' title='SU_TITLE'>
        <pkg-ref id='manual' auth='Root' onConclusion='RequireRestart'>#AlKybdSoftwareUpdate.pkg</pkg-ref>
    </choice>
    <installation-check script='InstallationCheck()'/>
    <script>
function InstallationCheck()
{
		debuglog("Debug Logging for Aluminum Keyboard Software Update 2.0 enabled.");
	return (
				rootVolumeOSGood() &amp;&amp;
				canHasUpdateWithErrors(swPath,swMinVersion,swMaxVersion,"","NEWER")
			);
}

function rootVolumeOSGood()
{
	// Less than 10.5.8, show error
	if (-1 == system.compareVersions(system.version.ProductVersion, '10.5.8'))
	{
			debuglog("System software version is not correct, got: " + system.version.ProductVersion);
		my.result.message = system.localizedStringWithFormat('ERROR_OS', '10.5.8');
		my.result.type = 'Fatal';
		return false;
	}
	
	// Between 10.5.8 and 10.6 is good
	if (-1 == system.compareVersions(system.version.ProductVersion, '10.6'))
	{
			debuglog("System software version is OK.");
		return true;
	}
	
	// Between 10.6 and 10.6.2, tell them to get 10.6.2
	if (-1 == system.compareVersions(system.version.ProductVersion, '10.6.2'))
	{
			debuglog("System software version is not correct, got: " + system.version.ProductVersion);
		my.result.message = system.localizedStringWithFormat('ERROR_GET1062');
		my.result.type = 'Fatal';
		return false;
	}

	
	// 10.6.2 and newer, show generic error.
		debuglog("System software version is not correct, got: " + system.version.ProductVersion);
	my.result.message = system.localizedStringWithFormat('ERROR_NEWER');
	my.result.type = 'Fatal';
	return false;
}

function canSeeUpdate()
{
	return (needsUpdate(swPath,swMinVersion,swMaxVersion) &amp;&amp;
			(doesAnyoneMatch("canSeeUpdate",EntityToFind1,vendorID,productIDArray) ||
			doesAnyoneMatch("canSeeUpdate",EntityToFind2,vendorID,productIDArray)));
}

function doesAnyoneMatch(callerID,EntityToFind,vendorID,productIDArray)
{
	var devicesToTestArray = system.ioregistry.matchingClass(EntityToFind);
	for (var index=0; index &lt; 5; index++) 
	{
		if (devicesToTestArray.length != 0) break;

		// didn't find any, try again
		devicesToTestArray = system.ioregistry.matchingClass(EntityToFind);
	}
	if (index==0)
		debuglog(callerID + ": Found " + devicesToTestArray.length + " " + EntityToFind + " entities.");
	else
		debuglog(callerID + ": Had to retry " + index + " times to find " + devicesToTestArray.length + " " + EntityToFind + " entities.");


	for (var index=0; index &lt; devicesToTestArray.length; index++) 
	{
			// Is this device from Apple?
			var myVendor = devicesToTestArray[index].VendorID;
				debuglog(callerID + ": device #" + index + " has vendorID " + myVendor);
			if (myVendor != vendorID) continue;
			
			// Is this device the one for which we have the software?
			var myProductID = devicesToTestArray[index].ProductID;
				debuglog(callerID + ": device #" + index + " has productID " + myProductID);
			if (myProductID in oc(productIDArray)) return true;
	}
		
		debuglog(callerID + ": Didn't find any matching devices, so returning false.");
	return false;	
}

function canHasUpdateWithErrors(bundlePath, minVersionString, maxVersionString, minErrString, maxErrString)
{
	var bundleVersion = PathGetShortVersion(bundlePath);
		debuglog("canHasUpdateWithErrors checking " + bundlePath + " which has version " + bundleVersion);

	if (!bundleVersion &amp;&amp; minVersionString &amp;&amp; (minVersionString != 0))
	{
		my.result.message = system.localizedStringWithFormat('ERROR_' + minErrString);
		my.result.type = 'Fatal';
		return false;
	}
	
	if (system.compareVersions(bundleVersion,minVersionString) == -1)
	{
		my.result.message = system.localizedStringWithFormat('ERROR_' + minErrString);
		my.result.type = 'Fatal';
		return false;
	}
	
	if (system.compareVersions(bundleVersion,maxVersionString) == 1)
	{
		my.result.message = system.localizedStringWithFormat('ERROR_' + maxErrString);
		my.result.type = 'Fatal';
		return false;
	}
	
	return true;
}

function needsUpdate(bundlePath, minVersionString, maxVersionString)
{
	var bundleVersion = PathGetShortVersion(bundlePath);
		debuglog("needsUpdate checking " + bundlePath + " which has version " + bundleVersion);

	if (!bundleVersion) return true;
	
	return ((system.compareVersions(bundleVersion,minVersionString) >= 0) &amp;&amp;
			(system.compareVersions(bundleVersion,maxVersionString) &lt;0));
}

function PathGetShortVersion(path)
{
	var bundle = system.files.bundleAtPath(path);
	if (bundle == null)
		return null;
	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("********** " + message);
}
</script>
    <localization>
        <strings language="pl"><![CDATA["SU_TITLE" = "Uaktualnienie oprogramowania klawiatury aluminiowej";
"SU_VERS" = "2.0";
"SU_SERVERCOMMENT" = "Dla komputerów klienckich i serwerów.";

"SU_DESCRIPTION"='<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"></meta></HEAD>
<BODY><FONT FACE="Lucida Grande">
<p>
Instalacja tego oprogramowania pozwoli korzystać z funkcji specjalnych aluminiowej klawiatury bezprzewodowej Apple Wireless Keyboard (z 2009 roku).
</p>
</FONT></BODY></HTML>
';
]]></strings>
    </localization>

	<pkg-ref id='auto' installKBytes='48442' version='2.0.1.1191932192'/>
</installer-gui-script>