<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<installer-gui-script minSpecVersion="1">
    <title>Flox</title>
    <license file="license.rtf" mime-type="text/rtf"/>
    <conclusion file="conclusion.html" mime-type="text/html"/>
    <background file="background.svg" mime-type="image/svg+xml" scaling="proportional" alignment="bottomleft"/>
    <background-darkAqua file="background-dark-aqua.svg" mime-type="image/svg+xml" scaling="proportional" alignment="bottomleft"/>
    <pkg-ref id="com.floxdev.flox">
        <bundle-version/>
    </pkg-ref>
    <options customize="always" allow-external-scripts="true" require-scripts="true" hostArchitectures="x86_64"/>
    <choices-outline>
        <line choice="Flox"/>
        <line choice="flox.take.over"/>
    </choices-outline>
    <choice id="Flox" title="Flox" visible="!isTakeOver()" start_selected="!isTakeOver()">
        <pkg-ref id="com.floxdev.flox"/>
    </choice>
    <choice id="flox.take.over" title="Flox with Nix re-install" description="An existing Nix installation has been detected. NOTE: To install Flox using Nix, see https://flox.dev/docs/install-flox. Proceeding with this installer will: 1. Remove Nix and its dependency cacert from the default system profile. 2. Replace the Nix daemon with a Flox provided version. 3. Revert any changes the Nix installer made to shell rc files in /etc but backup the current files by adding a .backup-with-nix-before-flox suffix." visible="isTakeOver()" start_selected="false">
        <pkg-ref id="com.floxdev.flox"/>
    </choice>
    <pkg-ref id="com.floxdev.flox" version="1.16.0" onConclusion="none" installKBytes="57957" updateKBytes="0">#flox-1.16.0.pkg</pkg-ref>
    <installation-check script="installationCheck()"/>
    <script><![CDATA[
        function isTakeOver() {
            return system.files.fileExistsAtPath("/nix/var/nix/db/db.sqlite") && ! system.files.fileExistsAtPath("/etc/flox-version") && ! system.files.fileExistsAtPath("/etc/flox-version.update")
        }

        function isCorrectArchitecture() {
            let isArm = Boolean(system.sysctl("hw.optional.arm64"))
            return ((isArm && "x86_64" == "arm64") || (!isArm && "x86_64" == "x86_64"))
        }

        // `installer(8)` sets `COMMAND_LINE_INSTALL` for command-line
        // installs; see `man installer`.
        function isCommandLineInstall() {
            let value = system.env["COMMAND_LINE_INSTALL"]
            return value !== undefined && value !== null && String(value).length > 0
        }

        function installationCheck() {
            if (!isCorrectArchitecture()) {
                let brandString = system.sysctl("machdep.cpu.brand_string")
                my.result.type = "Fatal"
                my.result.title = "Installer Architecture Mismatch"
                my.result.message = `
                  Tried running the installer for architecture "x86_64", but I'm a "${brandString}" machine.

                  Please download the correct installer and try again.
                `
                return false
            }
            // The `Flox` and `flox.take.over` choices both start
            // unselected when Nix is already present, so an unattended run
            // would install nothing and still report success.
            if (isTakeOver() && isCommandLineInstall()) {
                my.result.type = "Fatal"
                my.result.title = "Existing Nix Installation Detected"
                my.result.message = [
                    "Flox was not installed.",
                    "An existing Nix installation was detected. Installing Flox replaces the Nix daemon and removes Nix from the default system profile, so it must be confirmed in the graphical installer.",
                    "Use Flox with your existing Nix: https://flox.dev/docs/install-flox/install#nix",
                    "Replace your existing Nix: download the installer from https://flox.dev/docs/install-flox/install and select \"Flox with Nix re-install\"."
                ].join("\n\n")
                return false
            }
            return true
        }
        ]]></script>
    <product version="1.16.0"/>
</installer-gui-script>