<?xml version="1.0" encoding="UTF-8"?>

<?if $(sys.BUILDARCH) = x64 or $(sys.BUILDARCH) = arm64 ?>
    <?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
<?else ?>
    <?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
<?endif ?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" UpgradeCode="C19F9B41-CD13-4F0E-B27D-E0EF8CF1CE91" Version="0.1.0" Language="1033" Name="Grim" Manufacturer="Ardocrat">
        <Package Id="7a18ee67-b049-4462-b18f-9e7748685781" InstallerVersion="300" Compressed="yes"/>
        <Media Id="1" Cabinet="grim.cab" EmbedCab="yes" />

        <MajorUpgrade
            DowngradeErrorMessage = "A newer version of [ProductName] is already installed."
            AllowSameVersionUpgrades = "yes"
        />

        <Icon Id='ProductICO' SourceFile='wix\Product.ico'/>
        <Property Id='ARPPRODUCTICON' Value='ProductICO' />

        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id='$(var.PlatformProgramFilesFolder)'>
                <Directory Id="APPLICATIONROOTDIRECTORY" Name="Grim"/>
            </Directory>

            <Directory Id="DesktopFolder" Name="Desktop">
                <Component Id="ApplicationShortcutDesktop" Guid="14efa019-7ed7-4765-8263-fa5460f92495">
                    <Shortcut Id="ApplicationDesktopShortcut"
                              Name="Grim"
                              Icon="ProductICO"
                              Description="GUI for Grin"
                              Target="[APPLICATIONROOTDIRECTORY]grim.exe"
                              WorkingDirectory="APPLICATIONROOTDIRECTORY"/>
                    <RemoveFolder Id="DesktopFolder" On="uninstall"/>
                    <RegistryValue
                        Root="HKCU"
                        Key="Software\Ardocrat\Grim"
                        Name="installed"
                        Type="integer"
                        Value="1"
                        KeyPath="yes"/>
                </Component>
            </Directory>

            <!-- Step 1: Define the directory structure -->
            <Directory Id="ProgramMenuFolder">
                <Directory Id="ApplicationProgramsFolder" Name="Grim"/>
            </Directory>
        </Directory>

        <DirectoryRef Id="APPLICATIONROOTDIRECTORY">
            <Component Id="License" Guid="4b1d11d3-5d76-430e-b5ef-87f1a62cf21b">
                <File Id="LicenseFile" DiskId="1" Source="wix\License.rtf" KeyPath="yes"/>
            </Component>
            <Component Id="grim.exe" Guid="95444223-45BF-427A-85CA-61B035044305">
                <File Id="grim.exe" Source="$(var.CargoTargetBinDir)\grim.exe" KeyPath="yes" Checksum="yes"/>
            </Component>
        </DirectoryRef>

        <!-- Step 2: Add the shortcut to your installer package -->
        <DirectoryRef Id="ApplicationProgramsFolder">
            <Component Id="ApplicationShortcut" Guid="07f7fc68-bc3e-4715-9c10-0231a92b5ccb">
                <Shortcut Id="ApplicationStartMenuShortcut"
                          Name="Grim"
                          Description="Cross-platform GUI for Grin with focus on usability and availability to be used by anyone, anywhere."
                          Icon="ProductICO"
                          Target="[#grim.exe]"
                          WorkingDirectory="APPLICATIONROOTDIRECTORY"/>
                <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
                <RegistryValue Root="HKCU" Key="Software\Ardocrat\Grim" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
            </Component>
        </DirectoryRef>

        <Feature Id="MainApplication" Title="Grim" Level="1">
            <ComponentRef Id="grim.exe" />
            <ComponentRef Id="License" />
            <!-- Step 3: Tell WiX to install the shortcut -->
            <ComponentRef Id="ApplicationShortcutDesktop" />
            <ComponentRef Id="ApplicationShortcut" />
        </Feature>


        <Property Id='ARPHELPLINK' Value='https://github.com/ardocrat/grim'/>

        <UI>
            <UIRef Id="WixUI_Minimal" />
                <Publish Dialog="ExitDialog"
                         Control="Finish"
                         Event="DoAction"
                         Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish>
        </UI>
        <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch Application" />

        <Property Id="WixShellExecTarget" Value="[#grim.exe]" />
        <CustomAction Id="LaunchApplication"
                      BinaryKey="WixCA"
                      DllEntry="WixShellExec"
                      Impersonate="yes" />

        <WixVariable Id='WixUILicenseRtf' Value='wix\License.rtf'/>
    </Product>
</Wix>