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

  <?include TransmissionConfig.wxi ?>

  <?if $(sys.BUILDARCH) = x64 ?>
    <?define ProductName = "Transmission $(var.TrVersionFull) (x64)" ?>
    <?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
    <?define PlatformSystemFolder = "System64Folder" ?>
  <?else ?>
    <?define ProductName = "Transmission $(var.TrVersionFull)" ?>
    <?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
    <?define PlatformSystemFolder = "SystemFolder" ?>
  <?endif ?>

  <Product Name="$(var.ProductName)" Id="*" UpgradeCode="1FB3C295-9BD4-4248-8C8B-B85CD11FE7C4" Language="1033" Codepage="1252" Version="$(var.TrVersionMsi)" Manufacturer="Transmission Project">
    <Package Id="*" Keywords="Installer" Description="Transmission $(var.TrVersion) Installer" Comments="A Fast, Easy, and Free BitTorrent Client" Manufacturer="Transmission Project" InstallerVersion="500" InstallScope="perMachine" Languages="1033" Compressed="yes" SummaryCodepage="1252" />
    <MajorUpgrade DowngradeErrorMessage="A later version of [ProductName] is already installed. Setup will now exit." AllowSameVersionUpgrades="yes" />

    <?if $(var.QtMajorVer) = 5 ?>
      <?define MinSystemName = "Windows Vista, Windows Server 2008, or higher" ?>
      <?define MinSystemCondition = "(VersionNT >= 600)" ?>
    <?else ?>
      <!-- https://support.microsoft.com/en-us/help/3202260/versionnt-value-for-windows-10-and-windows-server-2016 -->
      <Property Id="WINDOWSBUILDNUM" Secure="yes">
        <RegistrySearch Id="OsMajorVerRegSearch" Root="HKLM" Key="SOFTWARE\Microsoft\Windows NT\CurrentVersion" Name="CurrentMajorVersionNumber" Type="raw" />
      </Property>
      <?define MinSystemName = "Windows 10, Windows Server 2016, or higher" ?>
      <?define MinSystemCondition = "WINDOWSBUILDNUM" ?>
    <?endif ?>

    <Condition Message="This application is only supported on $(var.MinSystemName).">
      <![CDATA[Installed OR $(var.MinSystemCondition)]]>
    </Condition>

    <Property Id="TRQTWINSTALLDIR" Secure="yes">
      <RegistrySearch Id="TrQtWRegSearch" Root="HKLM" Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Transmission-Qt" Name="InstallLocation" Type="directory" Win64="no"/>
      <RegistrySearch Id="TrQtWRegSearch64" Root="HKLM" Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Transmission-Qt" Name="InstallLocation" Type="directory" Win64="yes"/>
    </Property>
    <Condition Message="Another version of Transmission is already installed at &quot;[TRQTWINSTALLDIR]&quot;. Uninstall it first and then try again.">
      NOT TRQTWINSTALLDIR
    </Condition>

    <Property Id="UCRTINSTALLED" Secure="yes">
      <DirectorySearch Id="UcrtDirSearch" Path="[$(var.PlatformSystemFolder)]" Depth="0">
        <FileSearch Id="UcrtFileSearch" Name="ucrtbase.dll" MinVersion="6.2.10585.0" />
      </DirectorySearch>
    </Property>
    <Condition Message="The Universal C Runtime is not installed. Please run Windows Update and install all required Windows updates. You can download the UCRT separately from here: https://support.microsoft.com/en-us/kb/2999226">
      UCRTINSTALLED
    </Condition>

    <Property Id="VCREDISTINSTALLED" Secure="yes">
      <RegistrySearch Id="VcRedistInstalledRegSearch" Root="HKLM" Key="SOFTWARE\Microsoft\VisualStudio\$(var.VcVerMajor).0\VC\Runtimes\$(sys.BUILDARCH)" Name="Installed" Type="raw"/>
    </Property>
    <Property Id="VCREDISTVERSIONMINOR" Secure="yes">
      <RegistrySearch Id="VcRedistVersionMinorRegSearch" Root="HKLM" Key="SOFTWARE\Microsoft\VisualStudio\$(var.VcVerMajor).0\VC\Runtimes\$(sys.BUILDARCH)" Name="Minor" Type="raw"/>
    </Property>
    <Condition Message="The Visual C++ Runtime is not installed or is outdated. Please download and install the latest version from here: https://aka.ms/vs/$(var.VsVerMajor)/release/vc_redist.$(sys.BUILDARCH).exe">
      VCREDISTINSTALLED = "#1" AND VCREDISTVERSIONMINOR &gt;= "#$(var.VcVerMinor)0"
    </Condition>

    <Media Id="1" Cabinet="main.cab" EmbedCab="yes" DiskPrompt="CD-ROM #1" />
    <Property Id="DiskPrompt" Value="Transmission $(var.TrVersion) Installation [1]" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="$(var.PlatformProgramFilesFolder)" Name="PFiles">
        <Directory Id="INSTALLDIR" Name="Transmission">
          <Directory Id="WEBINSTALLDIR" Name="public_html" />
        </Directory>
      </Directory>
      <Directory Id="ProgramMenuFolder" Name="Programs"/>
      <Directory Id="DesktopFolder" Name="Desktop" />
    </Directory>

    <Feature Id="CompleteInstall" Title="Transmission" Level="1" AllowAdvertise="no" Display="expand" ConfigurableDirectory="INSTALLDIR">
      <Feature Id="CommonLibs" Level="1" AllowAdvertise="no" Display="hidden">
        <ComponentGroupRef Id="CommonLibsComponents" />
      </Feature>
      <Feature Id="QtClient" Title="Qt client" Description="Native GUI application with local and remote sessions support." Level="1" AllowAdvertise="no">
        <ComponentGroupRef Id="QtClientComponents" />
      </Feature>
      <Feature Id="Daemon" Title="Daemon" Description="Background service accessible with Qt client, web UI, or remote CLI tool." Level="2" AllowAdvertise="no">
        <ComponentGroupRef Id="DaemonComponents" />
      </Feature>
      <Feature Id="CmdTools" Title="Command-line tools" Description="Various command-line tools useful for scripting (remote, create, edit, show)." Level="2" AllowAdvertise="no">
        <ComponentGroupRef Id="CliToolsComponents" />
      </Feature>
      <Feature Id="WebUi" Title="Web interface" Description="Web UI to access Qt client or daemon via web browser." Level="2" AllowAdvertise="no">
        <ComponentGroupRef Id="WebUiComponents" />
      </Feature>
    </Feature>

    <UIRef Id="WixUI_FeatureTree" />
    <UIRef Id="WixUI_ErrorProgressText" />

    <WixVariable Id="WixUILicenseRtf" Value="$(var.LicenseFile)" />

    <Icon Id="Tr.ico" SourceFile="transmission.ico" />
    <Property Id="ARPPRODUCTICON" Value="Tr.ico" />

    <Property Id="ARPURLINFOABOUT" Value="https://transmissionbt.com/" />
    <Property Id="ARPHELPLINK" Value="https://forum.transmissionbt.com/" />
  </Product>

</Wix>