<?xml version="1.0" encoding="UTF-8"?>
<installer-gui-script minSpecVersion="1">
    <options customize="never" hostArchitectures="i386" rootVolumeOnly="true"/>
    <title>SU_TITLE</title>
    <script>
</script>
    <installation-check script="InstallationCheck()"/>
    <script>
		var updateName = "ThunderBoltSoftwareUpdate";
		
function HasEligibleiMac121() {
	// check model
	var model = system.sysctl("hw.model");
	if (!model) return false;
	if (model != "iMac12,1") {
		debuglog("iMac121 Model Property fail; got: " + model);
		return false;
	}
	debuglog("Model property pass; got: " + model);
			
	// We know this is the correct model, now we verify the hardware
	// check for Bluetooth
	var Bluetooth = system.ioregistry.fromPath("IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/EHC2@1A,7/AppleUSBEHCI/Bluetooth USB Host Controller@fa111000");
	if (!Bluetooth) {
		debuglog("Can't find Bluetooth controller; assuming machine doesn't need update.");
		return false;
	}
	debuglog("Bluetooth found!; continuing ...");
			
	// check for IR
	var AppleIR = system.ioregistry.fromPath("IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/EHC1@1D,7/AppleUSBEHCI/IR Receiver@fd120000");
	if (!AppleIR) {
		debuglog("Can't find IR Receiver; assuming machine doesn't need update.");
		return false;
	}
	debuglog("IR Receiver found!; continuing ...");
				
	// This machine is eligible for the update
	debuglog("iMac12,1 has Bluetooth and AppleIR, machine is eligible for update. Returning true");
	return true;
}
		
function myEFIRangeCheck() {

	var model = system.sysctl("hw.model");
	var obj = system.ioregistry.fromPath("IODeviceTree:/rom@0");
	var property = obj.version;
	var components = property.split(".");

	var majorHex = parseInt(components[2],16);
	var minorHex = parseInt(components[3],16);

	if (!model) return false;
	if ((model == "iMac12,1")  ||  (model == "iMac12,2")) {
		var lowerEFIBound = "IM121.88Z.0047.B1D.1110171110";
		if (compareEFI(majorHex, minorHex, lowerEFIBound))
		return true;
	}
	else if ((model == "MacBookPro8,1") || (model == "MacBookPro8,2") || (model == "MacBookPro8,3")) {
		var lowerEFIBound = "MBP81.88Z.0047.B24.1110141131";
		if (compareEFI(majorHex, minorHex, lowerEFIBound))
		return true;
	}
	else
	return false;	
}

function compareEFI(majorHex, minorHex, lowerEFIBound) {
	//returns true if major/minor hex pair are >= the lowerEFIBound

	var components = lowerEFIBound.split(".");

	if (majorHex > parseInt(components[2],16) || ((majorHex == parseInt(components[2],16)) &amp;&amp; (!(minorHex &lt; parseInt(components[3],16)))))
	return true;
	else
	return false;
}
		
function debuglog(message) {
	// yes, this is secure
	if (system.files.fileExistsAtPath("/tmp/com.apple.pkg.testing")) system.log("********** " + updateName + ": " + message);
}
	</script>
    <script>
function InstallationCheck(prefix) {
	if (system.compareVersions(system.version.ProductVersion, '10.6.8') &lt; 0) {
		my.result.message = system.localizedStringWithFormat('ERROR_0', '10.6.8');
		my.result.type = 'Fatal';
		return false;
	}
	my.result.message = system.localizedString('ERROR_1');
	if (!LogicalAnd0("/")) {
		my.result.type = 'Fatal';
		return false;
	}
	return true;
}
function LogicalAnd0(prefix) {
	if (LogicalAnd1(prefix) &amp;&amp; LogicalAnd6(prefix)) {
		return true;
	}
	return false;
}
function LogicalAnd1(prefix) {
	if (LogicalOr2(prefix) &amp;&amp; Script5(prefix)) {
		return true;
	}
	return false;
}
function LogicalOr2(prefix) {
	if (ModelVersionCheck3(prefix) || Script4(prefix)) {
		return true;
	}
	return false;
}
function ModelVersionCheck3(prefix) {
	var versions = ['iMac12,2','MacBookPro8,1','MacBookPro8,2','MacBookPro8,3'];
	var property = system.sysctl('hw.model');
	if (property) {
		for (var i = 0; i &lt; versions.length; i++) {
			if (versions[i] == property) return true;
		}
	}
	return false;
}
function Script4(prefix) {
	if (!HasEligibleiMac121()) {
		return false;
	}
	return true;
}
function Script5(prefix) {
	if (!myEFIRangeCheck()) {
		return false;
	}
	return true;
}
function LogicalAnd6(prefix) {
	if (PlistCheck7(prefix) &amp;&amp; BundleCheck8(prefix)) {
		return true;
	}
	return false;
}
function PlistCheck7(prefix) {
	var plist = system.files.plistAtPath(prefix + '/System/Library/CoreServices/SystemVersion.plist');
	if (!plist) {
		return false;
	}
	var plistKeyValue = plist['ProductVersion'];
	if (!plistKeyValue) {
		return false;
	}
	if (system.compareVersions(plistKeyValue, '10.7') >= 0) {
		return false;
	}
	return true;
}
function BundleCheck8(prefix) {
	var bundle = system.files.bundleAtPath(prefix + '/System/Library/Extensions/IOThunderboltFamily.kext');
	if (!bundle) {
		return false;
	}
	var bundleKeyValue = bundle['CFBundleShortVersionString'];
	if (!bundleKeyValue) {
		return false;
	}
	if (system.compareVersions(bundleKeyValue, '1.6.2') &lt;= 0) {
		return false;
	}
	return true;
}
function LogicalAnd9(prefix) {
	if (BundleCheck8(prefix) &amp;&amp; BundleCheck10(prefix)) {
		return true;
	}
	return false;
}
function BundleCheck10(prefix) {
	var bundle = system.files.bundleAtPath(prefix + '/System/Library/Extensions/IOThunderboltFamily.kext');
	if (!bundle) {
		return false;
	}
	var bundleKeyValue = bundle['CFBundleShortVersionString'];
	if (!bundleKeyValue) {
		return false;
	}
	if (system.compareVersions(bundleKeyValue, '1.7.4') >= 0) {
		return false;
	}
	return true;
}
</script>
    <platforms>
        <client arch="i386"/>
        <server arch="i386"/>
    </platforms>
    <choices-outline ui="SoftwareUpdate">
        <line choice="su"/>
    </choices-outline>
    <choices-outline>
        <line choice="su"/>
    </choices-outline>
    <choice id="su" title="SU_TITLE" versStr="SU_VERS" description="SU_DESCRIPTION" description-mime-type="text/html" secondaryDescription="SU_SERVERCOMMENT" suDisabledGroupID="ThunderboltSoftwareUpdate1.1" visible="LogicalAnd9('/')">
        <pkg-ref id="AppleDisplay" auth="Root" packageIdentifier="com.apple.pkg.ThunderboltSoftwareUpdate1.1" onConclusion="RequireRestart">ThunderboltSoftwareUpdate1.1.pkg</pkg-ref>
    </choice>
    <localization>
        <strings language="pl"><![CDATA["SU_TITLE" = "Uaktualnienie oprogramowania Thunderbolt";
"SU_VERS" = "1.1";
"SU_SERVERCOMMENT" = "Dla komputerów klienckich i serwerów";
"SU_DESCRIPTION" = '<HTML><!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">
	p {margin: 0.0px 0.0px 0.0px 0.0px; font: 11px Lucida Grande;}
  </style>
</head>
<body>
<p>W tym uaktualnieniu usunięto problem, który w przypadku niektórych użytkowników Apple Thunderbolt Display mógł powodować okresowe występowanie czarnych ekranów. Poprawiono również stabilność urządzeń Thunderbolt.</p>
</body>
</html>
';
]]></strings>
    </localization>

	<pkg-ref id='AppleDisplay' installKBytes='127445' version='1.1.0.0.1.1238328574'/>
</installer-gui-script>