<?xml version="1.0" encoding="utf-8"?>
<installer-gui-script minSpecVersion="2">
    <title>ESET Inspect Connector</title>
    <organization>com.eset</organization>
    <background file="background.jpg" mime-type="image/jpeg" alignment="topleft" scaling="none"/>
    <welcome file="Welcome.html" mime-type="text/html"/>
    <readme file="ReadMe.html" mime-type="text/html"/>
    <license file="License.html" mime-type="text/html"/>
    <conclusion file="Conclusion.html" mime-type="text/html"/>
    <domains enable_anywhere="false" enable_currentUserHome="false" enable_localSystem="true"/>
    <options customize="never" require-scripts="true" rootVolumeOnly="true" hostArchitectures="arm64"/>
    <installation-check script="checkForRequiredApplications();">
        <ram min-gb="0.25"/>
    </installation-check>
    <volume-check>
        <allowed-os-versions>
            <os-version min="10.15"/>
        </allowed-os-versions>
    </volume-check>
    <!-- List all component packages -->
    <pkg-ref id="com.eset.enterpriseinspector.agent" version="3.1.6017" auth="root" installKBytes="29082" updateKBytes="0">#output.pkg</pkg-ref>
    <!-- List them again here. They can now be organized
         as a hierarchy if you want. -->
    <choices-outline>
        <line choice="com.eset.enterpriseinspector.agent"/>
    </choices-outline>
    <!-- Define each choice above -->
    <choice id="com.eset.enterpriseinspector.agent" visible="false" title="ESET Inspect Connector" description="The daemon" start_selected="true">
        <pkg-ref id="com.eset.enterpriseinspector.agent"/>
    </choice>
    <script><![CDATA[
// JavaScript
function isEeaInstalled() {
    let bundleInfo = system.files.bundleAtPath('/Applications/ESET Endpoint Antivirus.app/')
    if (!bundleInfo) return false;

    system.log(`isEeaInstalled `);
    return system.compareVersions(bundleInfo.CFBundleShortVersionString, '6.9.60.0') >= 0;
}

function isEesInstalled() {
    let bundleInfo = system.files.bundleAtPath('/Applications/ESET Endpoint Security.app/')

    if (!bundleInfo) return false;

    system.log(`isEesInstalled `);
    return system.compareVersions(bundleInfo.CFBundleShortVersionString, '6.9.60.0') >= 0;
}

function isEsetManagementAgentInstalled() {
    let bundleInfo = system.files.bundleAtPath('/Applications/ESET Management Agent.app/')

    if (!bundleInfo) return false;
    return true; // no version check is needed since rename gives warranty of proper version
}

function isEsmcAgentInstalled() {
    let bundleInfo = system.files.bundleAtPath('/Applications/ESET Remote Administrator Agent.app/')

    if (!bundleInfo) return false;

    system.log(`isEsmcAgentInstalled `);
    return system.compareVersions(bundleInfo.CFBundleShortVersionString, '7.2.3008.0') >= 0;
}

function reportError(message) {
    system.log(`reportError `)
    my.result.title = 'Failure';
    my.result.type = 'Fatal';
    my.result.message = message;
    return false;
}

function checkForRequiredApplications() {
    system.log('checkForRequiredApplications')
    if (!isEeaInstalled() && !isEesInstalled()) {
        return reportError("ESET Endpoint Security (at least 6.9.60.0) or ESET Endpoint Antivirus (at least 6.9.60.0) is required by ESET Inspect Connector");
    }

    if (!isEsetManagementAgentInstalled()) {
        if (!isEsmcAgentInstalled()) {
            return reportError("ESET Management Agent or ESET Remote Administrator Agent (at least 7.2.3008.0) is required by ESET Inspect Connector");
        }
    }

    system.log('checkForRequiredApplications success!')
    return true;
}
]]></script>
    <pkg-ref id="com.eset.enterpriseinspector.agent">
        <bundle-version>
            <bundle CFBundleShortVersionString="3.1.6017.0" CFBundleVersion="3.1.6017.0" id="com.eset.eia" path="Library/Application Support/ESET/ESET Inspect Connector.app"/>
        </bundle-version>
    </pkg-ref>
    <product version="3.1.6017"/>
</installer-gui-script>