<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">

  <?define RepoDir="$(var.ProjectDir)..\..\" ?>
  <?define BinX64Dir="$(var.RepoDir)x64\$(var.Configuration)\" ?>
  <Product Id="*"
       Name="PowerToys"
       Language="1033"
       Version="0.13.0"
       Manufacturer="Microsoft"
       UpgradeCode="42B84BF7-5FBF-473B-9C8B-049DC16F7708">

    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" InstallPrivileges="elevated" Platform="x64" />

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

    <Upgrade Id="42B84BF7-5FBF-473B-9C8B-049DC16F7708">
      <UpgradeVersion
         Minimum="0.11.0" Maximum="0.13.0"
         Property="PREVIOUSVERSIONSINSTALLED"
         IncludeMinimum="yes" IncludeMaximum="yes" />
    </Upgrade>
    
    <MediaTemplate EmbedCab="yes" />

    <Property Id="WINDOWSBUILDNUMBER" Secure="yes">
      <RegistrySearch Id="BuildNumberSearch" Root="HKLM" Key="SOFTWARE\Microsoft\Windows NT\CurrentVersion" Name="CurrentBuildNumber" Type="raw" />
    </Property>
    <Condition Message="This application is only supported on Windows 10 version 1803 (build 17134) or higher.">
      <![CDATA[(WINDOWSBUILDNUMBER >= 17134)]]>
    </Condition>

    <Icon Id="powertoys.ico" SourceFile="$(var.BinX64Dir)\svgs\icon.ico"/>
    <Property Id="ARPPRODUCTICON" Value="powertoys.ico" />
    <Feature Id="CoreFeature" Title="PowerToys" AllowAdvertise="no" Absent="disallow" TypicalDefault="install"
             Description="Contains the Shortcut Guide and Fancy Zones features.">
      <ComponentGroupRef Id="CoreComponents" />
    </Feature>
    <Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" />
    <UI>
      <UIRef Id="WixUI_PTInstallDir"/>
      <Publish Dialog="ExitDialog"
            Control="Finish"
            Event="DoAction"
            Value="LaunchApplication">WIXUI_EXITDIALOGOPTIONALCHECKBOX = 1 and NOT Installed</Publish>
      <Publish Dialog="MaintenanceTypeDlg" Control="RemoveButton" Property="_REMOVE_ALL" Value="Yes">1</Publish>
      <Publish Dialog="UserExit" Control="Finish" Event="DoAction" Value="TelemetryLogInstallCancel">NOT Installed</Publish>
      <Publish Dialog="FatalError" Control="Finish" Event="DoAction" Value="TelemetryLogInstallFail">NOT Installed</Publish>
      <Publish Dialog="UserExit" Control="Finish" Event="DoAction" Value="TelemetryLogUninstallCancel">Installed AND _REMOVE_ALL="Yes"</Publish>
      <Publish Dialog="FatalError" Control="Finish" Event="DoAction" Value="TelemetryLogUninstallFail">Installed AND _REMOVE_ALL="Yes"</Publish>
      <Publish Dialog="UserExit" Control="Finish" Event="DoAction" Value="TelemetryLogRepairCancel">Installed AND NOT (_REMOVE_ALL="Yes")</Publish>
      <Publish Dialog="FatalError" Control="Finish" Event="DoAction" Value="TelemetryLogRepairFail">Installed AND NOT (_REMOVE_ALL="Yes")</Publish>
    </UI>
    <WixVariable Id="WixUIBannerBmp" Value="$(var.ProjectDir)\Bitmaps\banner.bmp" />
    <WixVariable Id="WixUIDialogBmp" Value="$(var.ProjectDir)\Bitmaps\dialog.bmp" />
    <WixVariable Id="WixUILicenseRtf" Value="$(var.RepoDir)\License.rtf" />
    <Property Id="INSTALLSTARTMENUSHORTCUT" Value="1"/>
    <Property Id="CREATESCHEDULEDTASK" Value="1"/>
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOX" Value="1"/>
    <Property Id="WIXUI_EXITDIALOGOPTIONALCHECKBOXTEXT" Value="Launch PowerToys" />
    <Property Id="WixShellExecTarget" Value="[#PowerToys.exe]" />
    <CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" />

    <InstallExecuteSequence>
      <Custom Action="SetRegisterPowerToysSchTaskParam" Before="RegisterPowerToysSchTask" />
      <Custom Action="RegisterPowerToysSchTask" After="InstallFiles">
        NOT Installed and CREATESCHEDULEDTASK = 1
      </Custom>
      <Custom Action="WixCloseApplications" Before="RemoveFiles" />
      <Custom Action="RemovePowerToysSchTasks" After="RemoveFiles" />
      <Custom Action="TelemetryLogInstallSuccess" After="InstallFinalize">
        NOT Installed
      </Custom>
      <Custom Action="TelemetryLogUninstallSuccess" After="InstallFinalize">
        Installed and (NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")
      </Custom>
    </InstallExecuteSequence>

    <CustomAction Id="SetRegisterPowerToysSchTaskParam"
                  Property="RegisterPowerToysSchTask"
                  Value="[#PowerToys.exe]" />

    <!-- Needs to Impersonate="no" and Execute="deferred" in order to run elevated. -->
    <CustomAction Id="RegisterPowerToysSchTask"
                  Return="ignore"
                  Impersonate="no"
                  Execute="deferred"
                  BinaryKey="PTCustomActions"
                  DllEntry="CreateScheduledTaskCA"
                   />

    <CustomAction Id="RemovePowerToysSchTasks"
                  Return="ignore"
                  Impersonate="no"
                  Execute="deferred"
                  BinaryKey="PTCustomActions"
                  DllEntry="RemoveScheduledTasksCA"
                   />

    <CustomAction Id="TelemetryLogInstallSuccess"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogInstallSuccessCA"
                   />

    <CustomAction Id="TelemetryLogInstallCancel"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogInstallCancelCA"
                   />

    <CustomAction Id="TelemetryLogInstallFail"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogInstallFailCA"
                   />

    <CustomAction Id="TelemetryLogUninstallSuccess"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogUninstallSuccessCA"
                   />

    <CustomAction Id="TelemetryLogUninstallCancel"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogUninstallCancelCA"
                   />

    <CustomAction Id="TelemetryLogUninstallFail"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogUninstallFailCA"
                   />
    
    <CustomAction Id="TelemetryLogRepairCancel"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogRepairCancelCA"
                   />

    <CustomAction Id="TelemetryLogRepairFail"
                  Return="ignore"
                  Impersonate="yes"
                  BinaryKey="PTCustomActions"
                  DllEntry="TelemetryLogRepairFailCA"
                   />

    <!-- Close 'PowerToys.exe' before uninstall-->
    <Property Id="MSIRESTARTMANAGERCONTROL" Value="Disable" />
    <util:CloseApplication CloseMessage="yes" Target="PowerToys.exe" ElevatedCloseMessage="yes" RebootPrompt="no" TerminateProcess="0" />
  </Product>

  <Fragment>
    <Binary Id="PTCustomActions" SourceFile="$(var.PowerToysSetupCustomActions.TargetPath)" />
  </Fragment>

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFiles64Folder">
        <Directory Id="INSTALLFOLDER" Name="PowerToys">
          <Directory Id="SvgsInstallFolder" Name="svgs"/>
          <Directory Id="ModulesInstallFolder" Name="modules"/>
          <Directory Id="SettingsHtmlInstallFolder" Name="settings-html">
            <Directory Id="SettingsHtmlDistInstallFolder" Name="dist"/>
          </Directory>
        </Directory>
      </Directory>
      <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="PowerToys"/>
      </Directory>
      <Directory Id="DesktopFolder" Name="Desktop"/>
    </Directory>
  </Fragment>

  <Fragment>
    <DirectoryRef Id="INSTALLFOLDER" FileSource="$(var.BinX64Dir)">
      <Component Id="powertoys_exe" Guid="A2C66D91-3485-4D00-B04D-91844E6B345B" Win64="yes">
        <File Id="PowerToys.exe" KeyPath="yes" Checksum="yes">
          <Shortcut Id="ApplicationStartMenuShortcut"
                    Name="PowerToys"
                    Description="PowerToys - Windows system utilities to maximize productivity"
                    Directory="ApplicationProgramsFolder"
                    WorkingDirectory="INSTALLFOLDER"
                    Icon="powertoys.ico"
                    IconIndex="0"
                    Advertise="yes" />
        </File>

        <RemoveFolder Id="DeleteShortcutFolder" Directory="ApplicationProgramsFolder" On="uninstall" />
      </Component>
      <Component Id="settings_exe" Guid="A5A461A9-7097-4CBA-9D39-3DBBB6B7B80C" Win64="yes">
        <File Id="PowerToysSettings.exe" KeyPath="yes" Checksum="yes" />
      </Component>
      <Component Id="License_rtf" Guid="3E5AE43B-CFB4-449B-A346-94CAAFF3312E" Win64="yes">
        <File Source="$(var.RepoDir)\License.rtf" Id="License.rtf" KeyPath="yes" />
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="SvgsInstallFolder" FileSource="$(var.BinX64Dir)\svgs\">
      <Component Id="PowerToysSvgs" Guid="7C4D4EED-9338-423D-992C-DCE02F3E2D35" Win64="yes">
        <File Source="$(var.BinX64Dir)\svgs\0.svg" />
        <File Source="$(var.BinX64Dir)\svgs\1.svg" />
        <File Source="$(var.BinX64Dir)\svgs\2.svg" />
        <File Source="$(var.BinX64Dir)\svgs\3.svg" />
        <File Source="$(var.BinX64Dir)\svgs\4.svg" />
        <File Source="$(var.BinX64Dir)\svgs\5.svg" />
        <File Source="$(var.BinX64Dir)\svgs\6.svg" />
        <File Source="$(var.BinX64Dir)\svgs\7.svg" />
        <File Source="$(var.BinX64Dir)\svgs\8.svg" />
        <File Source="$(var.BinX64Dir)\svgs\9.svg" />
        <File Source="$(var.BinX64Dir)\svgs\no_active_window.svg" />
        <File Source="$(var.BinX64Dir)\svgs\overlay.svg" />
        <File Source="$(var.BinX64Dir)\svgs\overlay_portrait.svg" />
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="ModulesInstallFolder" FileSource="$(var.BinX64Dir)\modules\">
      <Component Id="Module_ShortcutGuide" Guid="CBD0AC09-91D3-428E-B2B3-05745ADF3473" Win64="yes">
        <File Source="$(var.BinX64Dir)\modules\shortcut_guide.dll" KeyPath="yes" />
      </Component>
      <Component Id="Module_FancyZones" Guid="C6B5272E-6ED4-4B80-B0E7-2FF0355D8CF4" Win64="yes">
        <File Source="$(var.BinX64Dir)\modules\fancyzones.dll" KeyPath="yes" />
        <File Source="$(var.BinX64Dir)\modules\FancyZonesEditor.exe" />
        <File Source="$(var.BinX64Dir)\modules\ControlzEx.dll" />
        <File Source="$(var.BinX64Dir)\modules\MahApps.Metro.dll" />
        <File Source="$(var.BinX64Dir)\modules\Microsoft.Xaml.Behaviors.dll" />
      </Component>
      <Component Id="Module_PowerRename" Guid="E4401D08-27FE-4F96-BA17-0C61FD79E684" Win64="yes">
        <File Source="$(var.BinX64Dir)\modules\PowerRenameExt.dll" KeyPath="yes" />
        <RegistryKey Root="HKCR" Key="CLSID\{0440049F-D1DC-4E46-B27B-98393D79486B}">
          <RegistryValue Type="string" Value="PowerRename Shell Extension" />
          <RegistryValue Type="string" Key="InprocServer32" Value="[ModulesInstallFolder]PowerRenameExt.dll" />
          <RegistryValue Type="string" Key="InprocServer32" Name="ThreadingModel" Value="Apartment" />
        </RegistryKey>
        <RegistryKey Root="HKCR" Key="AllFileSystemObjects\ShellEx\ContextMenuHandlers\PowerRenameExt">
            <RegistryValue Type="string" Value="{0440049F-D1DC-4E46-B27B-98393D79486B}"/>
        </RegistryKey>
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="SettingsHtmlInstallFolder" FileSource="$(var.RepoDir)\settings\settings-html\">
      <Component Id="settings_html" Guid="87881A99-E917-4B0D-B1D8-5C6EB9709F96" Win64="yes">
        <File Source="$(var.RepoDir)\src\settings\settings-html\index.html" KeyPath="yes" />
      </Component>
      <Component Id="settings_dark_html" Guid="855866C7-2F13-4B08-B5C1-B507354C2760" Win64="yes">
        <File Source="$(var.RepoDir)\src\settings\settings-html\index-dark.html" KeyPath="yes" />
      </Component>
    </DirectoryRef>
    <DirectoryRef Id="SettingsHtmlDistInstallFolder" FileSource="$(var.RepoDir)\settings\settings-html\dist\">
      <Component Id="settings_js_bundle" Guid="9EF539C1-2F50-421E-B074-C58ED3A9785C" Win64="yes">
        <File Source="$(var.RepoDir)\src\settings\settings-html\dist\bundle.js" KeyPath="yes" />
      </Component>
    </DirectoryRef>

    <DirectoryRef Id="DesktopFolder">
      <Component Id="DesktopShortcut" Guid="87321F2B-CC48-4326-881E-9C62CC260DC8">
        <Condition>INSTALLDESKTOPSHORTCUT</Condition>
        <RegistryValue Root="HKCU"
                        Key="Software\[Manufacturer]\[ProductName]"
                        Name="desktopshorcutinstalled"
                        Type="integer"
                        Value="1"
                        KeyPath="yes"/>
        <Shortcut Id="DesktopShortcutId"
                  Name="PowerToys"
                  Description="PowerToys - Windows system utilities to maximize productivity"
                  Target="[!PowerToys.exe]"
                  WorkingDirectory="INSTALLFOLDER"
                  Icon="powertoys.ico"
                  Directory="DesktopFolder"/>
      </Component>
    </DirectoryRef>
  </Fragment>

  <Fragment>
    <ComponentGroup Id="CoreComponents" Directory="INSTALLFOLDER">
      <ComponentRef Id="powertoys_exe" />
      <ComponentRef Id="License_rtf" />
      <ComponentRef Id="PowerToysSvgs" />
      <ComponentRef Id="Module_ShortcutGuide" />
      <ComponentRef Id="Module_FancyZones" />
      <ComponentRef Id="DesktopShortcut" />
      <ComponentRef Id="Module_PowerRename" />
      <ComponentRef Id="settings_exe" />
      <ComponentRef Id="settings_html" />
      <ComponentRef Id="settings_dark_html" />
      <ComponentRef Id="settings_js_bundle" />
    </ComponentGroup>
  </Fragment>
</Wix>
