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

  <?include Version.wxi ?>
  <?include Global.wxi ?>

  <Bundle Name="$(var.ProductName)"
          Version="$(var.Version)"
          Manufacturer="Plex"
          UpgradeCode="$(var.ProductUUID)"
          HelpUrl="https://www.plex.tv/"
          Copyright="Plex"
          AboutUrl="https://www.plex.tv/"
          Tag="$(var.FullVersion)"
          IconSourceFile="Plex.ico"
          >
    <!-- VersionNT and VersionNT64 will always report 10.0.0.0 on windows 10 no matter what version/build it is -->
    <!-- ServicePackLevel means nothing on any windows after windows 7 -->
    <!-- WindowsBuild variable does not exist for burn -->

    <!-- These variables from the registry gives us more accurate version info and they are useful even just to log -->
    <util:RegistrySearch Id="WinRegProductName" Root='HKLM' Key='SOFTWARE\Microsoft\Windows NT\CurrentVersion' Value='ProductName' Variable='WinRegProductName' />
    <util:RegistrySearch Id="WinRegReleaseID" Root='HKLM' Key='SOFTWARE\Microsoft\Windows NT\CurrentVersion' Value='ReleaseID' Variable='WinRegReleaseID' />
    <util:RegistrySearch Id="WinRegEditionIDSearch" Root='HKLM' Key='SOFTWARE\Microsoft\Windows NT\CurrentVersion' Value='EditionID' Variable='WinRegEditionID' />
    <util:RegistrySearch Id="WinRegInstallationType" Root='HKLM' Key='SOFTWARE\Microsoft\Windows NT\CurrentVersion' Value='InstallationType' Variable='WinRegInstallationType' />
    <util:RegistrySearch Id="WinRegCurrentBuildSearch" Root='HKLM' Key='SOFTWARE\Microsoft\Windows NT\CurrentVersion' Value='CurrentBuild' Variable='WinRegCurrentBuild' />
    <util:RegistrySearch Id="WinRegCurrentBuildNumberSearch" Root='HKLM' Key='SOFTWARE\Microsoft\Windows NT\CurrentVersion' Value='CurrentBuildNumber' Variable='WinRegCurrentBuildNumber' />
    <util:RegistrySearch Id="WinRegMediaPlayerSearch" Root='HKLM' Key='SOFTWARE\Microsoft\MediaPlayer' Value='Installation Directory' Variable='WinRegMediaPlayer' />
    <util:RegistrySearch Variable="KB2919355_amd64"
                         Root="HKLM"
                         Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages\Package_for_KB2919355~31bf3856ad364e35~amd64~~6.3.1.14"
                         Value="CurrentState"
                         Win64="yes"
                         />
    <util:RegistrySearch Variable="KB2919355_x86"
                         Root="HKLM"
                         Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages\Package_for_KB2919355~31bf3856ad364e35~x86~~6.3.1.14"
                         Value="CurrentState"
                         />
    <bal:Condition Message="Plex Media Player can only be installed on Windows 7 or newer.">
      <![CDATA[VersionNT >= v6.1]]>
    </bal:Condition>
    <!-- Checking for prerequisites for windows versions where they are required -->
    <!-- We need to check for when ServicePackLevel is not defined as well, with (NOT ServicePackLevel) -->
    <bal:Condition Message="Plex Media Player on Windows 7 or Windows Server 2008 R2 requires Service Pack 1.">
      <![CDATA[NOT ((VersionNT = v6.1) AND ((NOT ServicePackLevel) OR (ServicePackLevel < 1)))]]>
    </bal:Condition>
    <!-- Windows 8 and 2012 have no prerequisites -->
    <bal:Condition Message="Plex Media Player on Windows 8.1 or Windows Server 2012 R2 requires KB2919355 installed.">
      <![CDATA[NOT ((VersionNT = v6.3) AND (KB2919355_x86 <> 112 AND KB2919355_amd64 <> 112))]]>
    </bal:Condition>

    <!-- msvcp140.dll will exist and ucrtbase.dll will not exist with a bad vcredist install-->
    <util:FileSearch Variable="Found_ucrtbase_dll"
                     Path="[SystemFolder]ucrtbase.dll"
                     Result="exists"
                     />
    <util:FileSearch Variable="Bad_VCRedist"
                     Path="[SystemFolder]msvcp140.dll"
                     Result="exists"
                     Condition="NOT Found_ucrtbase_dll"
                     />
                     
    <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.HyperlinkLicense">
      <bal:WixStandardBootstrapperApplication
        LicenseUrl=""
        ThemeFile="pmstheme.xml"
        LocalizationFile="HyperlinkTheme.wxl"
        />
      <Payload SourceFile="plex-chevron-85.png" />
      <Payload SourceFile="progress-bar.png" />
    </BootstrapperApplicationRef>

    <Variable Name="autolaunch" bal:Overridable="yes" Type="numeric" Value="0" />

    <Variable Name="InstallFolder" Type="string" Value="[ProgramFiles64Folder]Plex\Plex Media Player" />
    <!-- Search for InstallFolder in registry and set it to the PreviousInstallFolder -->
    <util:RegistrySearch
      Id="PreviousInstallFolderSearch" Root='HKCU' Key='Software\Plex, Inc.\Plex Media Player' Value='InstallFolder' Variable='PreviousInstallFolder' />
    <!-- Checks if PreviousInstallFolder exists event if it is defined in the Registry, sets it to InstallFolder if so -->
    <util:DirectorySearch
      Path='[PreviousInstallFolder]' Variable='InstallFolder' After='PreviousInstallFolderSearch' Condition='PreviousInstallFolder' />

    <Variable Name="LaunchTarget" Value="[InstallFolder]\PlexMediaPlayer.exe"/>

    <!-- Is Media Feature Pack present? (This should be enough for detecting if we are running on Windows N or not) -->
    <util:FileSearch Variable="FoundMfPlatDll" Path="[SystemFolder]mfplat.dll" Result="exists" />

    <!-- In order to avoid using a SourceFile, use heat to harvest the RemotePayload data out of the msu files: "heat payload xxx.msu -out out.xml"-->
    <!-- Add the <RemotePayload> element from the xml file to the package file and change SourceFile to Name with just the filename. -->
    <!-- For msu files <RemotePayload> needs a dummy Version, ProductName and Description attributes (These aren't available from the file itself and build fails if you omit them) -->

    <!-- When installing burn will automatically use a package (.msu, .msi, .exe) if its uncompressed and is next to the install bundle, instead of downloading it.-->
    <Chain>
      <!-- Media Feature Packs for Windows 7.0 -->
      <MsuPackage KB="KB968211"
                  DisplayName="Downloading Media Feature Pack for Windows 7 x86"
                  Id="MediaPackWin7x86"
                  Name="Windows6.1-KB968211-x86-RefreshPkg.msu"
                  DownloadUrl="https://download.microsoft.com/download/B/9/B/B9BED058-8669-490E-BA61-D502E4E8BEB1/Windows6.1-KB968211-x86-RefreshPkg.msu"
                  InstallCondition='VersionNT=v6.1 AND NOT VersionNT64 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="61E7707927AF8DEDA014EC17C6959A989EB68C57" CertificateThumbprint="9BF69D5E8D01A92F413B60A4BE003E323CB52F7F" Description="Windows6.1-KB968211-x86-RefreshPkg.msu" Hash="88C37CB34D871C1B52A9D1125477D812999CB28E" ProductName="Windows6.1-KB968211-x86-RefreshPkg.msu" Size="291852366" Version="1.1.1.1"/>
      </MsuPackage>
      <MsuPackage KB="KB968211"
                  DisplayName="Downloading Media Feature Pack for Windows 7 x64"
                  Id="MediaPackWin7x64"
                  Name="Windows6.1-KB968211-x64-RefreshPkg.msu"
                  DownloadUrl="https://download.microsoft.com/download/B/9/B/B9BED058-8669-490E-BA61-D502E4E8BEB1/Windows6.1-KB968211-x64-RefreshPkg.msu"
                  InstallCondition='VersionNT=v6.1 AND VersionNT64 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="61E7707927AF8DEDA014EC17C6959A989EB68C57" CertificateThumbprint="9BF69D5E8D01A92F413B60A4BE003E323CB52F7F" Description="Windows6.1-KB968211-x64-RefreshPkg.msu" Hash="79FC3C186F7AF88E3273A40E52F01A5E48103BF9" ProductName="Windows6.1-KB968211-x64-RefreshPkg.msu" Size="318337992" Version="1.1.1.1"/>
      </MsuPackage>

      <!-- Media Feature Packs for Windows 8.0 -->
      <MsuPackage KB="KB2703761"
                  DisplayName="Downloading Media Feature Pack for Windows 8.0 x86"
                  Id="MediaPackWin8x86"
                  Name="Windows6.2-KB2703761-x86.msu"
                  DownloadUrl="https://download.microsoft.com/download/7/A/D/7AD12930-3AA6-4040-81CF-350BF1E99076/Windows6.2-KB2703761-x86.msu"
                  InstallCondition='VersionNT=v6.2 AND NOT VersionNT64 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll AND WinRegMediaPlayer"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="D37F6D0F2894D56049061A44596FFA88CBFD1B5B" CertificateThumbprint="19F8F76F4655074509769C20349FFAECCECD217D" Description="Windows6.2-KB2703761-x86.msu" Hash="E94B6B38870333F33091630915D06C1C3CB3BD59" ProductName="Windows6.2-KB2703761-x86.msu" Size="60746899" Version="1.1.1.1"/>
      </MsuPackage>
      <MsuPackage KB="KB2703761"
                  DisplayName="Downloading Media Feature Pack for Windows 8.0 x64"
                  Id="MediaPackWin8x64"
                  Name="Windows6.2-KB2703761-x64.msu"
                  DownloadUrl="https://download.microsoft.com/download/7/A/D/7AD12930-3AA6-4040-81CF-350BF1E99076/Windows6.2-KB2703761-x64.msu"
                  InstallCondition='VersionNT=v6.2 AND VersionNT64 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll AND WinRegMediaPlayer"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="D37F6D0F2894D56049061A44596FFA88CBFD1B5B" CertificateThumbprint="19F8F76F4655074509769C20349FFAECCECD217D" Description="Windows6.2-KB2703761-x64.msu" Hash="B2A81F0BE816628096A22B37F2D17D9E8063DC6F" ProductName="Windows6.2-KB2703761-x64.msu" Size="82674179" Version="1.1.1.1"/>
      </MsuPackage>

      <!-- Media Feature Packs for Windows 8.1 -->
      <MsuPackage KB="KB2929699"
                  DisplayName="Downloading Media Feature Pack for Windows 8.1 x86"
                  Id="MediaPackWin81x86"
                  Name="Windows8.1-KB2929699-x86.msu"
                  DownloadUrl="https://download.microsoft.com/download/8/9/7/89775613-432E-4ECF-93A9-5BAFCB5B7807/Windows8.1-KB2929699-x86.msu"
                  InstallCondition='VersionNT=v6.3 AND NOT VersionNT64 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="A260A870BE1145ED71E2BB5AA19463A4FE9DCC41" CertificateThumbprint="108E2BA23632620C427C570B6D9DB51AC31387FE" Description="Windows8.1-KB2929699-x86.msu" Hash="386524559BAF25F86BF374F88EB5BAD6F07A0F9F" ProductName="Windows8.1-KB2929699-x86.msu" Size="63605391" Version="1.1.1.1"/>
      </MsuPackage>
      <MsuPackage KB="KB2929699"
                  DisplayName="Downloading Media Feature Pack for Windows 8.1 x64"
                  Id="MediaPackWin81x64"
                  Name="Windows8.1-KB2929699-x64.msu"
                  DownloadUrl="https://download.microsoft.com/download/8/9/7/89775613-432E-4ECF-93A9-5BAFCB5B7807/Windows8.1-KB2929699-x64.msu"
                  InstallCondition='VersionNT=v6.3 AND VersionNT64 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="A260A870BE1145ED71E2BB5AA19463A4FE9DCC41" CertificateThumbprint="108E2BA23632620C427C570B6D9DB51AC31387FE" Description="Windows8.1-KB2929699-x64.msu" Hash="0734AED9D4354F90A77811758F4BC554D731F6B0" ProductName="Windows8.1-KB2929699-x64.msu" Size="89383884" Version="1.1.1.1"/>
      </MsuPackage>

      <!-- Media Feature Packs for Windows 10 RTM -->
      <MsuPackage KB="KB3010081"
                  DisplayName="Downloading Media Feature Pack for Windows 10 x86"
                  Id="MediaPackWin10x86"
                  Name="Microsoft-Windows-MediaFeaturePack-OOB-Package.msu"
                  DownloadUrl="https://download.microsoft.com/download/7/F/2/7F2E00A7-F071-41CA-A35B-00DC536D4227/Microsoft-Windows-MediaFeaturePack-OOB-Package.msu"
                  InstallCondition='VersionNT>=v10.0 AND NOT VersionNT64 AND WinRegCurrentBuildNumber=10240 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="4C72A5297C20ADD16A1964578EFFC7CB4EF1D38D" CertificateThumbprint="49DA9A5E21EDC4682AD0211C85D552C86C422F13" Description="Microsoft-Windows-MediaFeaturePack-OOB-Package.msu" Hash="87ABA28404F07E7D1CEA496FDB4AD20B8C978E50" ProductName="Microsoft-Windows-MediaFeaturePack-OOB-Package.msu" Size="75898094" Version="1.1.1.1"/>
      </MsuPackage>
      <MsuPackage KB="KB3010081"
                  DisplayName="Downloading Media Feature Pack for Windows 10 x64"
                  Id="MediaPackWin10x64"
                  Name="Microsoft-Windows-MediaFeaturePack-OOB-Package_x64.msu"
                  DownloadUrl="https://download.microsoft.com/download/7/F/2/7F2E00A7-F071-41CA-A35B-00DC536D4227/Microsoft-Windows-MediaFeaturePack-OOB-Package_x64.msu"
                  InstallCondition='VersionNT>=v10.0 AND VersionNT64 AND WinRegCurrentBuildNumber=10240 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="4C72A5297C20ADD16A1964578EFFC7CB4EF1D38D" CertificateThumbprint="49DA9A5E21EDC4682AD0211C85D552C86C422F13" Description="Microsoft-Windows-MediaFeaturePack-OOB-Package_x64.msu" Hash="2D1FAE125F6A5FBEB3F56479DBA7A2BE8BC3EE77" ProductName="Microsoft-Windows-MediaFeaturePack-OOB-Package_x64.msu" Size="114150296" Version="1.1.1.1"/>
      </MsuPackage>

      <!-- Media Feature Packs for Windows 10 1511 -->
      <MsuPackage KB="KB3099229"
                  DisplayName="Downloading Media Pack for Windows 10 x86 1511"
                  Id="MediaPackWin10x861511"
                  Name="KB3099229_x86.msu"
                  DownloadUrl="https://download.microsoft.com/download/B/E/3/BE302763-5BFD-4209-9C98-02DF5B2DB452/KB3099229_x86.msu"
                  InstallCondition='VersionNT>=v10.0 AND NOT VersionNT64 AND WinRegReleaseID=1511 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="4C72A5297C20ADD16A1964578EFFC7CB4EF1D38D" CertificateThumbprint="49DA9A5E21EDC4682AD0211C85D552C86C422F13" Description="KB3099229_x86.msu" Hash="4C1CBED520159DA8F5DB69BF1E04C2D0DD7EDB23" ProductName="KB3099229_x86.msu" Size="76836258" Version="1.1.1.1"/>
      </MsuPackage>
      <MsuPackage KB="KB3099229"
                  DisplayName="Downloading Media Pack for Windows 10 x64 1511"
                  Id="MediaPackWin10x641511"
                  Name="KB3099229_x64.msu"
                  DownloadUrl="https://download.microsoft.com/download/B/E/3/BE302763-5BFD-4209-9C98-02DF5B2DB452/KB3099229_x64.msu"
                  InstallCondition='VersionNT>=v10.0 AND VersionNT64 AND WinRegReleaseID=1511 AND WinRegInstallationType~="Client"'
                  DetectCondition="FoundMfPlatDll"
                  Cache="no"
                  Compressed="no"
                  Permanent="yes"
                  Vital="yes">
        <RemotePayload CertificatePublicKey="4C72A5297C20ADD16A1964578EFFC7CB4EF1D38D" CertificateThumbprint="49DA9A5E21EDC4682AD0211C85D552C86C422F13" Description="KB3099229_x64.msu" Hash="DB92011AB439392E426A82A1F60EF6CC87A63864" ProductName="KB3099229_x64.msu" Size="115805700" Version="1.1.1.1"/>
      </MsuPackage>

      <!-- PMP's msi-->
      <MsiPackage Id="pmp"
                  Name="PMP.msi"
                  Compressed="yes"
                  SourceFile="$(var.OutputPath)/PMP.msi"
                  Vital="yes">
        <MsiProperty Name="INSTALLLOCATION" Value="[InstallFolder]" />
        <MsiProperty Name="AUTOLAUNCH" Value="[autolaunch]" />
        <MsiProperty Name="WINDOWSTYPE" Value="[WinRegInstallationType]" />
      </MsiPackage>
      <ExePackage Id="vcredist_repair"
                  SourceFile="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\redist\1033\vcredist_x64.exe"
                  Name="vcredist_x64.exe"
                  InstallCommand="/repair /quiet /norestart"
                  InstallCondition="Bad_VCRedist"
                  Compressed="yes"
                  Vital="yes"
                  Permanent="yes">
        <ExitCode Value="1638" Behavior="success"/>
      </ExePackage>
      <ExePackage Id="vcredist"
                  SourceFile="C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\redist\1033\vcredist_x64.exe"
                  Name="vcredist_x64.exe"
                  InstallCommand="/install /quiet /norestart"
                  InstallCondition="NOT Bad_VCRedist"
                  Compressed="yes"
                  Vital="yes"
                  Permanent="yes">
        <ExitCode Value="1638" Behavior="success"/>
      </ExePackage>
    </Chain>
  </Bundle>
</Wix>
