<?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 "Settings.wxi" ?>

  <Product Id="*" Name="$(var.ProductName) $(var.ProductVersion)" Language="!(loc.LANG)" Version="$(var.ProductVersion)" Manufacturer="$(var.ProductManufacturer)" UpgradeCode="$(var.ProductUpgradeCode)">
    <Package Id="*" InstallerVersion="300" Compressed="yes" Languages="0,1028,1029,1030,1031,1033,1034,1035,1036,1040,1041,1043,1044,1045,1046,1049,1053,1055,2052" />

    <Property Id='NSISINSTALL'>
      <RegistrySearch Id='NSISSearch' Win64='no' Root='HKLM' Key='SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Mumble' Type='directory' Name='InstallLocation' />
    </Property> 

    <CustomAction Id="SetNSISPath" Property="NSISPath" Value="[NSISINSTALL]Uninstall.exe" />
    <CustomAction Id="NSISUninstall" Property="NSISPath" ExeCommand="/S _?=[NSISINSTALL]" Execute="deferred" Impersonate="no" Return="check" />
    <UI>
      <ProgressText Action="NSISUninstall">Uninstalling old version</ProgressText>
    </UI>

    <util:CloseApplication Id="CloseMumble" RebootPrompt="no" EndSessionMessage="yes" Target="mumble.exe" />

    <Property Id="WixShellExecTarget" Value="[#mumble.exe]" />
    <CustomAction Id="LaunchApplication" BinaryKey="WixCA" DllEntry="WixShellExec" Impersonate="yes" />
    <UI>
      <Publish Dialog="MumbleExitDialog" Control="Finish" Order="1" Event="DoAction" Value="LaunchApplication">LAUNCHAPPONEXIT</Publish>
    </UI>

    <Property Id='INSTALLDIR'>
      <ComponentSearch Id='SearchMumbleComponent' Guid='$(var.MumbleExeGuid)' />
    </Property>

    <Condition Message='$(var.ProductName) requires at least Windows XP SP2 or Windows Server 2003 SP1'>
      VersionNT > 501 OR (VersionNT = 501 AND ServicePackLevel >= 2) OR (VersionNT = 502 AND ServicePackLevel >= 1)
    </Condition>

    <MajorUpgrade AllowDowngrades='no' AllowSameVersionUpgrades='yes' MigrateFeatures='yes' Schedule='afterInstallExecute' DowngradeErrorMessage='A later version of [ProductName] is already installed.' />

    <Media Id="1" Cabinet="$(var.ProductName).cab" EmbedCab="yes" CompressionLevel="high" />

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

    <Property Id="ALLUSERS" Value="1" />
    <Property Id="REINSTALLMODE" Value="amus" /> <!-- See http://msdn.microsoft.com/en-us/library/aa371182(v=vs.85).aspx -->
    <Property Id="ARPURLINFOABOUT" Value="http://www.mumble.info/" />
    <Property Id="ARPURLUPDATEINFO" Value="http://www.mumble.info/" />
    <Property Id='ARPPRODUCTICON'>mumble.ico</Property>
    <Property Id="MSIRESTARTMANAGERCONTROL" Value="Disable" />

    <WixVariable Id="WixUILicenseRtf" Value="$(var.SourceDir)\installer\gpl.rtf" />
    <WixVariable Id="WixUIBannerBmp" Value="$(var.SourceDir)\installer\bannrbmp.bmp" />
    <WixVariable Id="WixUIDialogBmp" Value="$(var.SourceDir)\installer\dlgbmp.bmp" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="$(var.ArchSpecificProgramFilesFolderId)">
        <Directory Id="INSTALLDIR" Name="$(var.ProductName)">
        </Directory>
      </Directory>

      <Directory Id="DesktopFolder" />

      <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="$(var.ProductName)" />
      </Directory>

      <Directory Id="AppDataFolder">
        <Directory Id="AppDataProgramFolder" Name="$(var.ProductName)">
          <Directory Id="AppDataPluginFolder" Name="Plugins" />
        </Directory>
      </Directory>
    </Directory>

    <Icon Id="mumble.ico" SourceFile="$(var.SourceDir)\icons\mumble.ico"/>
    <Icon Id="murmur.ico" SourceFile="$(var.SourceDir)\icons\murmur.ico"/>

    <DirectoryRef Id="ApplicationProgramsFolder">
      <Component Id="ApplicationProgramsFolderRemovalComponent" Guid="$(var.ApplicationProgramsFolderRemovalComponentGuid)">
        <RegistryValue Key="Software\Mumble\Uninstall" Root="HKCU" Value="1" Type="integer" KeyPath="yes" />
        <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall" />
      </Component>

      <Component Id="UninstallerComponent" Guid="$(var.UninstallerComponentGuid)">
          <Shortcut Id="UninstallerShortcut"
            Name="Uninstall"
            Target="[SystemFolder]msiexec.exe"
            Arguments="/i [ProductCode]"/>
          <RegistryValue Key="Software\Mumble\UninstallerShortcut" Root="HKCU" Value="1" Type="integer" KeyPath="yes" />
      </Component>
    </DirectoryRef>

    <DirectoryRef Id="AppDataPluginFolder">
      <Component Id="NSISUninstall" Guid="$(var.NSISUninstallComponentGuid)">
        <Condition>NOT Installed AND NSISINSTALL</Condition>
        <RegistryValue Key="Software\Mumble\Uninstall" Root="HKCU" Value="1" Type="integer" KeyPath="yes" />
        <RemoveFile Id="NSISUninstaller" Property="NSISINSTALL" Name="Uninstall.exe" On="install"/>
        <RemoveFile Id="DownloadedPlugins" Directory="AppDataPluginFolder" Name="*.dll" On="install" />
        <RemoveFolder Id="AppDataPluginFolder" On="uninstall" />
        <RemoveFolder Id="AppDataProgramFolder" Directory="AppDataProgramFolder" On="uninstall" />
      </Component>
    </DirectoryRef>

    <ComponentGroup Id="LibrariesCommon">
      <ComponentRef Id="licence.txt" />
      <ComponentRef Id="Readme.txt" />
      <ComponentRef Id="Changes.txt" />
      <ComponentRef Id="qt.txt" />
    </ComponentGroup>

    <Feature Id="Mumble" Title="!(loc.MUMBLE_SEC_MUMBLE)" Description="!(loc.DESC_SectionMumble)" Level="1" AllowAdvertise="no" ConfigurableDirectory="INSTALLDIR" Display="expand">
      <ComponentRef Id="speex.dll" />
      <ComponentRef Id="speex.txt" />

      <ComponentGroupRef Id="Plugins"/>
      <ComponentRef Id="celt0.0.7.0.dll" />
      <ComponentRef Id="celt0.0.11.0.dll" />
      <ComponentRef Id="opus.dll" />
      <?ifdef SSE ?>
        <ComponentRef Id="celt0.0.7.0.sse.dll" />
        <ComponentRef Id="celt0.0.11.0.sse.dll" />
      <?endif ?>
      <?ifdef SSE2 ?>
        <ComponentRef Id="celt0.0.7.0.sse2.dll" />
        <ComponentRef Id="celt0.0.11.0.sse2.dll" />
        <ComponentRef Id="opus.sse2.dll" />
      <?endif ?>
      <ComponentRef Id="mumble_app.dll" />
      <ComponentRef Id="mumble.exe" />
      <ComponentRef Id="mumble_ol.dll" />
      <ComponentRef Id="mumble_ol_helper.exe" />
      <ComponentRef Id="mumble_ol_x64.dll" />
      <ComponentRef Id="mumble_ol_helper_x64.exe" />
      <?ifdef G15 ?>
        <ComponentRef Id="mumble_g15_helper.exe" />
      <?endif ?>

      <?ifdef RedistDirVC14 ?>
        <ComponentRef Id="msvcp140.dll" />
        <ComponentRef Id="vcruntime140.dll" />
      <?endif ?>

      <?ifdef RedistDirUCRT ?>
        <?include "MumbleUCRTComponentRefs.wxi" ?>
      <?endif ?>

      <Feature Id="MumbleDesktopShortcutFeature" Title="!(loc.MUMBLE_SEC_DesktopShortcut)" Description="!(loc.DESC_DesktopShortcut)" InstallDefault="followParent" AllowAdvertise="no">
        <ComponentRef Id="MumbleDesktopShortcutComponent" />
      </Feature>
    </Feature>

    <Feature Id="Murmur" Title="!(loc.MUMBLE_SEC_MURMUR)" Description="!(loc.DESC_SectionMurmur)" Level="1000" ConfigurableDirectory="INSTALLDIR" Display="expand">
      <ComponentRef Id="murmur.exe" />
      <ComponentRef Id="murmur.ini" />
      <ComponentRef Id="Murmur.ice" />

      <?ifdef VersionSubDir ?>
        <?ifdef RedistDirVC14 ?>
          <ComponentRef Id="Murmur_msvcp140.dll" />
          <ComponentRef Id="Murmur_vcruntime140.dll" />
        <?endif ?>
        <?ifdef RedistDirUCRT ?>
          <?include "MurmurUCRTComponentRefs.wxi" ?>
        <?endif ?>
      <?endif ?>

      <Feature Id="MurmurDesktopShortcutFeature" Title="!(loc.MUMBLE_SEC_DesktopShortcut)" Description="!(loc.DESC_DesktopShortcut)"  InstallDefault="followParent" AllowAdvertise="no">
        <ComponentRef Id="MurmurDesktopShortcutComponent" />
      </Feature>
      
    </Feature>

    <Feature Id="Libraries" Title="Required Libraries" Display="hidden" AllowAdvertise="no" Absent="disallow" Level="1">
      <ComponentGroupRef Id="LibrariesCommon" />
      <ComponentRef Id="ApplicationProgramsFolderRemovalComponent" />
      <ComponentRef Id="UninstallerComponent" />
      <ComponentRef Id="NSISUninstall" />
    </Feature>

    <InstallExecuteSequence>
      <AppSearch Sequence='1' />
      <Custom Action="SetNSISPath" After="AppSearch">NSISINSTALL</Custom>
      <Custom Action='NSISUninstall' After='InstallInitialize'>NSISINSTALL AND NOT Installed</Custom>
      <Custom Action="WixCloseApplications" Before="InstallValidate">WIX_UPGRADE_DETECTED</Custom>
    </InstallExecuteSequence>
  </Product>
</Wix>
