<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<installer-gui-script minSpecVersion="1">
    <title>C2 Identity</title>
    <allowed-os-versions>
        <os-version min="10.15"/>
    </allowed-os-versions>
    <domains enable_anywhere="false" enable_currentUserHome="false" enable_localSystem="true"/>
    <license file="License.rtf" mime-type="application/rtf"/>
    <pkg-ref id="com.synology.c2identityagent.service.pkg" onConclusionScript="getConclusionByBundle()" version="1.79.0-01321" installKBytes="50167">#C2IAService.pkg</pkg-ref>
    <pkg-ref id="com.synology.c2identityagent.login.pkg" onConclusion="None" version="1.79.0-01321" installKBytes="8343">#C2IALogin.pkg</pkg-ref>
    <options customize="never" require-scripts="false"/>
    <choices-outline>
        <line choice="c2identityagent.service"/>
        <line choice="c2identityagent.login"/>
    </choices-outline>
    <choice id="c2identityagent.service" title="C2 Identity Agent Service">
        <pkg-ref id="com.synology.c2identityagent.service.pkg"/>
    </choice>
    <choice id="c2identityagent.login" title="C2 Identity Agent Login" visible="false">
        <pkg-ref id="com.synology.c2identityagent.login.pkg"/>
    </choice>
    <installation-check script="InstallationCheck();"/>
    <script><![CDATA[
        var serviceBundlePath = '/opt/Synology/C2IdentityAgent/bin/C2 Identity.app';
        String.format = function(pattern) {
            var args = Array.prototype.slice.call(arguments, 0);
            return pattern.replace(/\$\$(\d+)/g, function(match, number) {
                return typeof args[number] != 'undefined' ? args[number] : match;
            });
        };

        function setDenyResult(msg) {
            my.result.type = 'Fatal';
            my.result.message = msg;
        }

        function InstallationCheck() {
            if(system.compareVersions(system.version.ProductVersion, '10.15.0') < 0) {
                setDenyResult(system.localizedString("err_os_version_too_low"));
                return false;
            }

            if (!bundleVersionCheck()) {
                return false;
            }

            return true;
        }

        function bundleVersionCheck() {
            var bundle = system.files.bundleAtPath(serviceBundlePath);
            if (!bundle) {
                return true;
            }

            var shortVersion = bundle['CFBundleShortVersionString'];
            if (!shortVersion) {
                return true;
            }

            var buildVersion = bundle['CFBundleVersion'];
            if (!buildVersion) {
                return true;
            }

            var onDiskVersion = shortVersion + '-' + buildVersion;
            // currentVersion variable would be replaced in build script.
            if (system.compareVersions(onDiskVersion, '1.79.0-01321') > 0) {
                setDenyResult(String.format(system.localizedString("INSTALLED_NEWER"), onDiskVersion));
                return false;
            }

            return true;
        }

        function getConclusionByBundle() {
            // Cannot rely on my.choice.packageUpgradeAction to determine it's
            // clean install or not. Because this value seems to be checked per
            // volume, and the result of the last volume would override the
            // others. Hence we check existence of service bundle.
            var bundle = system.files.bundleAtPath(serviceBundlePath);
            if (!bundle) {
                return "RequireRestart";
            }

            return "None";
        }
        ]]></script>
    <pkg-ref id="com.synology.c2identityagent.service.pkg">
        <bundle-version>
            <bundle CFBundleShortVersionString="1.79.0" CFBundleVersion="01321" id="com.synology.c2identityagent" path="Synology/C2IdentityAgent/bin/C2 Identity.app"/>
        </bundle-version>
    </pkg-ref>
    <pkg-ref id="com.synology.c2identityagent.login.pkg">
        <bundle-version>
            <bundle CFBundleShortVersionString="0.12.2" CFBundleVersion="1" id="com.stephencelis.SQLite" path="C2IALogin.bundle/Contents/Frameworks/SQLite.framework"/>
            <bundle CFBundleShortVersionString="1.4.3" CFBundleVersion="1" id="com.krzyzanowskim.CryptoSwift" path="C2IALogin.bundle/Contents/Frameworks/CryptoSwift.framework"/>
            <bundle CFBundleShortVersionString="1.0.0" CFBundleVersion="2920" id="com.synology.c2ialogin" path="C2IALogin.bundle"/>
        </bundle-version>
    </pkg-ref>
</installer-gui-script>