<?xml version="1.0" encoding="UTF-8"?>
<installer-gui-script authoringTool="Packages" authoringToolVersion="1.2.10" authoringToolBuild="732" minSpecVersion="1.0">
    <options rootVolumeOnly="true" customize="never" hostArchitectures="ppc,i386,arm64"/>
    <installation-check script="installation_check()"/>
    <!--+==========================+
        |       Presentation       |
        +==========================+-->
    <title>DISTRIBUTION_TITLE</title>
    <!--+==========================+
        |         Installer        |
        +==========================+-->
    <choices-outline>
        <line choice="installer_choice_1"/>
        <line choice="installer_choice_2"/>
        <line choice="installer_choice_3"/>
    </choices-outline>
    <choice id="installer_choice_1" start_visible="false" start_enabled="false" start_selected="true" title="krisp-prev.uninstall" description="">
        <pkg-ref id="ai.krisp.krispMac.prevUninstaller"/>
    </choice>
    <choice id="installer_choice_2" start_visible="false" start_enabled="false" start_selected="true" title="krisp-audio-driver.sli" description="">
        <pkg-ref id="ai.krisp.krispMac.audioDriver.sli"/>
    </choice>
    <choice id="installer_choice_3" start_visible="false" start_enabled="false" start_selected="true" title="krisp-app.uli" description="">
        <pkg-ref id="ai.krisp.krispMac.uli"/>
    </choice>
    <!--+==========================+
        |    Package References    |
        +==========================+-->
    <pkg-ref id="ai.krisp.krispMac.prevUninstaller" version="2.1" installKBytes="0">#krisp-prev.uninstall.pkg</pkg-ref>
    <pkg-ref id="ai.krisp.krispMac.prevUninstaller">
        <must-close>
            <app id="ai.krisp.krispMac.uninstaller"/>
            <app id="ai.krisp.krispMac"/>
        </must-close>
    </pkg-ref>
    <pkg-ref id="ai.krisp.krispMac.uli" version="2.1" installKBytes="0">#krisp-app.uli.pkg</pkg-ref>
    <pkg-ref id="ai.krisp.krispMac.uli">
        <must-close>
            <app id="ai.krisp.krispMac.uninstaller"/>
            <app id="ai.krisp.krispMac"/>
        </must-close>
    </pkg-ref>
    <pkg-ref id="ai.krisp.krispMac.audioDriver.sli" version="2.1" installKBytes="0">#krisp-audio-driver.sli.pkg</pkg-ref>
    <!--+==========================+
        |    JavaScript Scripts    |
        +==========================+-->
    <script>

	const __IC_FLAT_DISTRIBUTION__=true;
	const IC_OS_DISTRIBUTION_TYPE_ANY=0;
	const IC_OS_DISTRIBUTION_TYPE_CLIENT=1;
	const IC_DISK_TYPE_DESTINATION=0;
	const IC_OS_DISTRIBUTION_TYPE_SERVER=2;
	const IC_DISK_TYPE_STARTUP_DISK=1;

	const IC_CPU_ARCHITECTURE_APPLESILICON=16777228;
	const IC_CPU_ARCHITECTURE_TYPE_ANY=0;
	const IC_CPU_ARCHITECTURE_TYPE_32=1;
	const IC_CPU_ARCHITECTURE_TYPE_64=2;
	const IC_CPU_ARCHITECTURE_INTEL=7;
	const IC_CPU_ARCHITECTURE_ANY=0;
	const IC_CPU_ARCHITECTURE_POWERPC=18;

	const IC_CONDITION_EXIST=0;
	const IC_CONDITION_DOES_NOT_EXIST=1;
	const IC_SELECTOR_ANY=0;
	const IC_SELECTOR_ALL=1;

	function IC_CheckOS(inDiskType,inMustBeInstalled,inMinimumVersion,inMaximumVersion,inDistributionType)
	{
		var tOSVersion=undefined;
		
		/* Check Version Constraints */
		
		if (inDiskType==IC_DISK_TYPE_DESTINATION)
		{
			if (my.target.systemVersion!=undefined)
			{
				tOSVersion=my.target.systemVersion.ProductVersion;
			}
			
			/* Check if no OS is installed on the potential target */
			
			if (tOSVersion==undefined)
			{
				return (inMustBeInstalled==false);
			}
			
			if (inMustBeInstalled==false)
			{
				return false;
			}
		}
		else
		{
			tOSVersion=system.version.ProductVersion;
		}
		
		if (system.compareVersions(tOSVersion,inMinimumVersion)==-1)
			return false;
			
		if (inMaximumVersion!=undefined &amp;&amp;
			system.compareVersions(tOSVersion,inMaximumVersion)==1)
			return false;
		
		/* Check Distribution Type */
		
		if (inDistributionType!=IC_OS_DISTRIBUTION_TYPE_ANY)
		{
			var tIsServer;
			
			if (system.compareVersions(tOSVersion,'10.8.0')==-1)
			{
				if (inDiskType==IC_DISK_TYPE_DESTINATION)
				{
					tIsServer=system.files.fileExistsAtPath(my.target.mountpoint+'/System/Library/CoreServices/ServerVersion.plist');
				}
				else
				{
					tIsServer=system.files.fileExistsAtPath('/System/Library/CoreServices/ServerVersion.plist');
				}
			}
			else
			{
				if (inDiskType==IC_DISK_TYPE_DESTINATION)
				{
					tIsServer=system.files.fileExistsAtPath(my.target.mountpoint+'/Applications/Server.app');
				}
				else
				{
					tIsServer=system.files.fileExistsAtPath('/Applications/Server.app');
				}
			}
			
			if (inDistributionType==IC_OS_DISTRIBUTION_TYPE_CLIENT &amp;&amp; tIsServer==true)
			{
				return false;
			}
			
			if (inDistributionType==IC_OS_DISTRIBUTION_TYPE_SERVER &amp;&amp; tIsServer==false)
			{
				return false;
			}
		}
		
		return true;
	}

	function IC_CheckCPU(inMinimumCoresCount,inSupportedArchitecture,inSupportedPowerPCArchitectureType,inSupportedIntelArchitectureType,inMinimumFrequency)
	{
		/* Check Minimum Core Count */
		
		if (system.sysctl('hw.ncpu')&gt;=inMinimumCoresCount)
		{
			var tArchitecture;
			var t64BitSupport;
			var tTranslated;

			tArchitecture=system.sysctl('hw.cputype');

			tTranslated=system.sysctl('sysctl.proc_translated');

			if (tTranslated!=null)
			{
				if (tTranslated==1)
				{
					tArchitecture=IC_CPU_ARCHITECTURE_APPLESILICON;
				}
			}
			
			/* Check Architecture */
			
			if (inSupportedArchitecture!=IC_CPU_ARCHITECTURE_ANY)
			{
				if (inSupportedArchitecture!=tArchitecture)
				{
					return false;
				}
			}
			
			/* Check Architecture Type */
			
			t64BitSupport=system.sysctl('hw.cpu64bit_capable');
			
			if (tArchitecture==IC_CPU_ARCHITECTURE_POWERPC)
			{
				if ((inSupportedPowerPCArchitectureType==IC_CPU_ARCHITECTURE_TYPE_32 &amp;&amp; t64BitSupport==1) ||
					(inSupportedPowerPCArchitectureType==IC_CPU_ARCHITECTURE_TYPE_64 &amp;&amp; t64BitSupport==0))
				{
					return false;
				}
			}
			else if (tArchitecture==IC_CPU_ARCHITECTURE_INTEL)
			{
				if ((inSupportedIntelArchitectureType==IC_CPU_ARCHITECTURE_TYPE_32 &amp;&amp; t64BitSupport==1) ||
					(inSupportedIntelArchitectureType==IC_CPU_ARCHITECTURE_TYPE_64 &amp;&amp; t64BitSupport==0))
				{
					return false;
				}
			}
			
			/* Check Minimum CPU Frequency (hw.cpufrequency_max does not exist on Apple Silicon) */
			
			var tFrequencyMax;
			
			tFrequencyMax=system.sysctl('hw.cpufrequency_max');
			
			if (tFrequencyMax==null || system.sysctl('hw.cpufrequency_max')&gt;=inMinimumFrequency)
			{
				return true;
			}
		}
		
		return false;
	}

	function versionCheck(newVersion) {
				let krPath = '/Applications/krisp.app'
				let krPl = system.files.bundleAtPath(krPath) || system.files.bundleAtPath(system.env.HOME + krPath)
				if (krPl) {
					let insDomain = system.files.fileExistsAtPath(krPath) ? 'system' : 'user'
					let krVersion = krPl['CFBundleShortVersionString']
					if (system.compareVersions(krVersion, newVersion) === 1) {
						system.log('===== Prevent update krisp to ' + newVersion + ' since newer version ' + krVersion + ' installed ' + insDomain + ' wide')
						return false
					}
					system.log('===== Update krisp from ' + krVersion + ' installed ' + insDomain + ' wide to ' + newVersion + ' version')
				}
				return true
			}

	function installation_check()
	{
		var tResult;

		tResult=IC_CheckOS(IC_DISK_TYPE_STARTUP_DISK,true,'12.0',undefined,IC_OS_DISTRIBUTION_TYPE_ANY);

		if (tResult==false)
		{
			my.result.title = system.localizedString('REQUIREMENT_FAILED_MESSAGE_INSTALLATION_CHECK_1');
			my.result.message = system.localizedString('REQUIREMENT_FAILED_DESCRIPTION_INSTALLATION_CHECK_1');
			my.result.type = 'Fatal';
		}

		if (tResult==true)
		{
			tResult=IC_CheckCPU(1,IC_CPU_ARCHITECTURE_INTEL,IC_CPU_ARCHITECTURE_TYPE_ANY,IC_CPU_ARCHITECTURE_TYPE_64,866666);

			if (tResult==false)
			{
				my.result.title = system.localizedString('REQUIREMENT_FAILED_MESSAGE_INSTALLATION_CHECK_2');
				my.result.message = system.localizedString('REQUIREMENT_FAILED_DESCRIPTION_INSTALLATION_CHECK_2');
				my.result.type = 'Fatal';
			}

			if (tResult==true)
			{
				tResult=(versionCheck('2.33.5')==true);

				if (tResult==false)
				{
					my.result.title = system.localizedString('REQUIREMENT_FAILED_MESSAGE_INSTALLATION_CHECK_3');
					my.result.message = system.localizedString('REQUIREMENT_FAILED_DESCRIPTION_INSTALLATION_CHECK_3');
					my.result.type = 'Fatal';
				}
			}
		}

		return tResult;
	}

    </script>
    <domains enable_currentUserHome="false" enable_anywhere="false" enable_localSystem="true"/>
</installer-gui-script>