<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <?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,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>

    <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='afterInstallInitialize' DowngradeErrorMessage='A later version of [ProductName] is already installed.' />

    <Upgrade Id="$(var.Old64BitProductUpgradeCode)">
      <!-- 64 bit installers had a seperate upgrade code during the beta. As we do not actually want both versions
           to be installed at the same time this allows detecting the old 64 bit version and upgrading from it -->
      <UpgradeVersion Minimum="1.3.0" IncludeMinimum="yes" Maximum="$(var.ProductVersion)" IncludeMaximum="yes" OnlyDetect="no" Property="REMOVEOLD64BITPRODUCT" />
    </Upgrade>

    <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>

    <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>

        <?ifdef MergeModuleDir ?>
          <Merge Id="VCRedist" SourceFile="$(var.MergeModuleDir)\Microsoft_VC120_CRT_x86.msm" DiskId="1" Language="1033" />
        <?endif ?>
      </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">
      <?ifndef StaticBuild ?>
        <ComponentGroupRef Id="Qt" />

        <ComponentRef Id="libeay32.dll" />
        <ComponentRef Id="ssleay32.dll" />
        <?ifdef ZlibDir ?>
          <ComponentRef Id="zlib1.dll" />
        <?endif ?>

        <ComponentRef Id="qt.conf" />
      <?endif ?>

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

      <ComponentRef Id="dbghelp.dll" />

      <?ifndef StaticBuild ?>
        <?ifdef ProtoBufDir ?>
          <ComponentRef Id="libprotobuf.dll" />
        <?endif ?>
        <?ifdef MySQLDir ?>
          <ComponentRef Id="libmysql.dll" />
        <?endif ?>
      <?endif ?>

      <?ifdef IntelCppDir ?>
        <ComponentRef Id="libmmd.dll"/>
      <?endif ?>

      <?ifdef RedistDirVC12 ?>
        <ComponentRef Id="msvcp120.dll" />
        <ComponentRef Id="msvcr120.dll" />
      <?endif ?>
    </ComponentGroup>

    <Feature Id="Mumble" Title="!(loc.MUMBLE_SEC_MUMBLE)" Description="!(loc.DESC_SectionMumble)" Level="1" AllowAdvertise="no" ConfigurableDirectory="INSTALLDIR" Display="expand">
      <?ifndef StaticBuild ?>
        <?ifdef SndFileDir ?>
          <ComponentRef Id="libsndfile1.dll" />
        <?endif ?>
      <?endif ?>
      <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 ?>
      <?ifdef StaticBuild ?>
        <ComponentRef Id="mumble_app.dll" />
      <?endif ?>
      <ComponentRef Id="mumble.exe" />
      <ComponentRef Id="mumble_ol.dll" />
      <?ifdef G15 ?>
        <ComponentRef Id="mumble_g15_helper.exe" />
      <?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">
      <?ifndef StaticBuild ?>
        <?ifdef IceDir ?>
          <ComponentRef Id="bzip2.dll" />
          <ComponentRef Id="ice34.dll" />
          <ComponentRef Id="iceutil34.dll" />
        <?endif ?>
        <?ifdef DBusDir ?>
          <ComponentGroupRef Id="DBus" />
        <?endif ?>
      <?endif ?>

      <ComponentRef Id="murmur.exe" />
      <ComponentRef Id="murmur.ini" />
      <ComponentRef Id="Murmur.ice" />
      
      <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>

    <?ifdef MergeModuleDir ?>
    <Feature Id="VCRedist" Title="Visual C++ 10.0 Runtime" AllowAdvertise="no" Display="hidden" Level="1">
      <MergeRef Id="VCRedist"/>
    </Feature>
    <?endif ?>

    <InstallExecuteSequence>
      <AppSearch Sequence='1' />
      <Custom Action="SetNSISPath" After="AppSearch">NSISINSTALL</Custom>
      <Custom Action='NSISUninstall' After='RemoveExistingProducts'>NSISINSTALL AND NOT Installed</Custom>
    </InstallExecuteSequence>
  </Product>
</Wix>
