<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated during build -->
<?include "..\dokanver.xml"?>
<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"
     xmlns:netfx='http://schemas.microsoft.com/wix/NetFxExtension'>
  <Bundle Name="Keybase" Version="$(env.KEYBASE_WINVER)" Manufacturer="Keybase, Inc." UpgradeCode="418432ab-0366-40fd-a396-8cc0c4200252">

    <Variable Name="DokanInstallFolder" Type="string" Value="[ProgramFiles6432Folder]Dokan\Dokan Library-1.0.0"/>

    <Variable Name="nodriver" bal:Overridable="yes" />
    
    <Log PathVariable="LOGPATH_PROP"/>
    <util:FileSearchRef Id='WINTRUST_FileSearch' />
    <util:RegistrySearchRef Id='InnoCLIUninstall' />
    <util:RegistrySearchRef Id='InnoCLIUninstall64' />
    <util:RegistrySearchRef Id='InnoKBFSUninstall' />
    <util:RegistrySearchRef Id='InnoKBFSUninstall64' />
    <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.HyperlinkLicense">
      <bal:WixStandardBootstrapperApplication
            LicenseUrl="https://keybase.io/docs/terms"
            ThemeFile="HyperlinkTheme.xml"
            LocalizationFile="HyperlinkTheme.wxl"
            LogoFile="$(env.GOPATH)\src\github.com\keybase\client\media\icons\icon-keybase-logo-windows-64.png"
            LicenseFile="license.rtf"
            ShowVersion="yes"
            SuppressOptionsUI="yes"
            />
    </BootstrapperApplicationRef>

    <!-- Dokany 
         This is the target we need to install with. If it's not there,
         we may need to invoke the dokan cleaner. -->
    <util:RegistrySearch Id="DokanUninstall"
             Variable="DokanUninstallString"
             Root="HKLM"
             Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(var.DokanProductCodeX86)"
             Value="UninstallString"
             Format="raw"
             />
    <util:RegistrySearch Id="DokanUninstall64"
                 After="DokanUninstall"
                 Condition="NOT DokanUninstallString"
                 Variable="DokanUninstallString"
                 Root="HKLM"
                 Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(var.DokanProductCodeX64)"
                 Value="UninstallString"
                 Format="raw"
                 Win64="yes"
                 />

    <util:FileSearch Id="SearchSystem"
                     Path="[SystemFolder]advapi32.dll"
                     Variable="ADVAPIVERSION"
                     Result="version"
                     />

    <util:FileSearch Id="SearchSystem2"
                     Path="[SystemFolder]ucrtbase.dll"
                     Variable="UCRTVERSION"
                     Result="version"
                     />

    <util:FileSearch Id="SearchSystem3"
                     Path="[WindowsFolder]System32\ucrtbase.dll"
                     Variable="UCRTVERSION64"
                     Result="version"
                       />

    <util:RegistrySearch Id="vcredist_2015_x86_check" Root="HKLM" Key="SOFTWARE\Microsoft\DevDiv\VC\Servicing\14.0\RuntimeMinimum" Value="Install" Variable="vcredist_2015_x86_installed" Win64="no"/>
    <util:RegistrySearch Id="vcredist_2015_x86_versioncheck" Root="HKLM" Key="SOFTWARE\Classes\Installer\Dependencies\Microsoft.VS.VC_RuntimeMinimumVSU_x86,v14" Value="Version" Variable="vcredist_2015_x86_version" Result="exists" Win64="no"/>
    <util:RegistrySearch Id="vcredist_2015_x86_versionnumbercheck" Root="HKLM" Key="SOFTWARE\Classes\Installer\Dependencies\Microsoft.VS.VC_RuntimeMinimumVSU_x86,v14" Value="Version" Variable="vcredist_2015_x86_versionnumber" Result="value" Win64="no"/>

    <util:RegistrySearch Id="vcredist_2015_x64_check" Root="HKLM" Key="SOFTWARE\Microsoft\DevDiv\VC\Servicing\14.0\RuntimeMinimum" Value="Install" Variable="vcredist_2015_x64_installed" Win64="yes"/>
    <util:RegistrySearch Id="vcredist_2015_x64_versioncheck" Root="HKLM" Key="SOFTWARE\Classes\Installer\Dependencies\Microsoft.VS.VC_RuntimeMinimumVSU_amd64,v14" Value="Version" Variable="vcredist_2015_x64_version" Result="exists" Win64="yes"/>
    <util:RegistrySearch Id="vcredist_2015_x64_versionnumbercheck" Root="HKLM" Key="SOFTWARE\Classes\Installer\Dependencies\Microsoft.VS.VC_RuntimeMinimumVSU_amd64,v14" Value="Version" Variable="vcredist_2015_x64_versionnumber" Result="value" Win64="yes"/>

    <bal:Condition Message="Installation failed because your version of Windows is too old. Dokan requires Windows 7 SP1 or newer."><![CDATA[Installed OR VersionNT > v6.1 OR (VersionNT = v6.1 AND ServicePackLevel >= 1)]]></bal:Condition>
    
    <Chain>
      <PackageGroupRef Id="NetFx40Web"/>
      
      <!-- Remember: if DetectCondition is true, this package is considered already present. 
        WixBundleAction of 4 means install. 3 is uninstall, 5 is upgrade.
        Making this permanent prevents it from being invoked on uninstall or other removal.
      -->
      <ExePackage
        SourceFile="$(env.GOPATH)\src\github.com\keybase\client\go\tools\dokanclean\dokanclean.exe"
        DetectCondition="DokanUninstallString OR nodriver"
        Description="Remove previous drivers"
        PerMachine="yes"
        Permanent="yes">
        <ExitCode Value="1641" Behavior="forceReboot"/>
        <ExitCode Value="3010" Behavior="forceReboot"/>
        <ExitCode Value="0" Behavior="success" />
      </ExePackage>

      <!-- Note there are 2 VC redistributable package entries, one for x86 and one for x64,
 	   which need to remain in sync -->	    
      <ExePackage Id="vcredist_2015_x86.exe"
                  Name="vc_redist.x86.exe"
                  InstallCommand="/norestart /passive /chainingpackage ADMINDEPLOYMENT"
                  RepairCommand="/norestart /passive /chainingpackage ADMINDEPLOYMENT"
                  UninstallCommand="/norestart /passive /chainingpackage ADMINDEPLOYMENT"
                  Protocol="netfx4"
                  Permanent="yes"
                  DetectCondition="vcredist_2015_x86_installed AND vcredist_2015_x86_version AND vcredist_2015_x86_versionnumber &gt;= v14.0.23918"
                  PerMachine="yes"
                  Vital="yes"
                  Cache="no"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\VCRedist_2015\vc_redist.x86.exe"
                  DownloadUrl="https://download.microsoft.com/download/2/6/8/268f7be7-8d2a-42d6-a823-5c6941a3d70a/vc_redist.x86.exe"
      >
        <ExitCode Value="1641" Behavior="scheduleReboot"/>
        <ExitCode Value="3010" Behavior="scheduleReboot"/>
        <ExitCode Value="0" Behavior="success" />
      </ExePackage>

      <!-- Note there are 2 VC redistributable package entries, one for x86 and one for x64,
 	   which need to remain in sync -->	    
      <ExePackage Id="vcredist_2015_x64.exe"
                  Name="vc_redist.x64.exe"
                  InstallCommand="/norestart /passive /chainingpackage ADMINDEPLOYMENT"
                  RepairCommand="/norestart /passive /chainingpackage ADMINDEPLOYMENT"
                  UninstallCommand="/norestart /passive /chainingpackage ADMINDEPLOYMENT"
                  Protocol="netfx4"
                  Permanent="yes"
                  InstallCondition="VersionNT64"
                  DetectCondition="vcredist_2015_x64_installed AND vcredist_2015_x64_version AND vcredist_2015_x64_versionnumber &gt;= v14.0.23918"
                  PerMachine="yes"
                  Vital="yes"
                  Cache="yes"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\VCRedist_2015\vc_redist.x64.exe"
                  DownloadUrl="https://download.microsoft.com/download/6/3/a/63abaf83-2ca6-4460-90d7-12de8e815b1a/vc_redist.x64.exe"
                  >
        <ExitCode Value="1641" Behavior="scheduleReboot"/>
        <ExitCode Value="3010" Behavior="scheduleReboot"/>
        <ExitCode Value="0" Behavior="success" />
      </ExePackage>

      <MsuPackage Id="Win71_KB2999226_x86"
                  Name="Windows6.1-KB2999226-x86.msu"
                  Compressed="yes"
                  Permanent="yes"
                  InstallCondition="NOT VersionNT64 AND VersionNT = v6.1"
                  DetectCondition="UCRTVERSION &gt;= v6.2.10585.0"
                  Vital="yes"
                  Cache="no"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\KB2999226\Windows6.1-KB2999226-x86.msu"
                  DownloadUrl="https://download.microsoft.com/download/4/F/E/4FE73868-5EDD-4B47-8B33-CE1BB7B2B16A/Windows6.1-KB2999226-x86.msu"
                  />

      <MsuPackage Id="Win71_KB2999226_x64"
                  Name="Windows6.1-KB2999226-x64.msu"
                  Compressed="yes"
                  Permanent="yes"
                  InstallCondition="VersionNT64 AND VersionNT = v6.1"
                  DetectCondition="UCRTVERSION64 &gt;= v6.2.10585.0"
                  Vital="yes"
                  Cache="no"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\KB2999226\Windows6.1-KB2999226-x64.msu"
                  DownloadUrl="https://download.microsoft.com/download/1/1/5/11565A9A-EA09-4F0A-A57E-520D5D138140/Windows6.1-KB2999226-x64.msu"
                  />

      <MsuPackage Id="Win80_KB2999226_x86"
                  Name="Windows8-RT-KB2999226-x86.msu"
                  Compressed="yes"
                  Permanent="yes"
                  InstallCondition="NOT VersionNT64 AND VersionNT = v6.2"
                  DetectCondition="UCRTVERSION &gt;= v6.2.10585.0"
                  Vital="yes"
                  Cache="no"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\KB2999226\Windows8-RT-KB2999226-x86.msu"
                  DownloadUrl="https://download.microsoft.com/download/1/E/8/1E8AFE90-5217-464D-9292-7D0B95A56CE4/Windows8-RT-KB2999226-x86.msu"
                  />

      <MsuPackage Id="Win80_KB2999226_x64"
                  Name="Windows8-RT-KB2999226-x64.msu"
                  Compressed="yes"
                  Permanent="yes"
                  InstallCondition="VersionNT64 AND VersionNT = v6.2"
                  DetectCondition="UCRTVERSION64 &gt;= v6.2.10585.0"
                  Vital="yes"
                  Cache="no"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\KB2999226\Windows8-RT-KB2999226-x64.msu"
                  DownloadUrl="https://download.microsoft.com/download/A/C/1/AC15393F-A6E6-469B-B222-C44B3BB6ECCC/Windows8-RT-KB2999226-x64.msu"
                  />

      <MsuPackage Id="Win81_KB2999226_x86"
                  Name="Windows8.1-KB2999226-x86.msu"
                  Compressed="yes"
                  Permanent="yes"
                  InstallCondition="NOT VersionNT64 AND VersionNT &gt;= v6.3 AND ADVAPIVERSION &lt; v6.3.10000.0"
                  DetectCondition="UCRTVERSION &gt;= v6.2.10585.0"
                  Vital="yes"
                  Cache="no"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\KB2999226\Windows8.1-KB2999226-x86.msu"
                  DownloadUrl="https://download.microsoft.com/download/E/4/6/E4694323-8290-4A08-82DB-81F2EB9452C2/Windows8.1-KB2999226-x86.msu"
                  />

      <MsuPackage Id="Win81_KB2999226_x64"
                  Name="Windows8.1-KB2999226-x64.msu"
                  Compressed="yes"
                  Permanent="yes"
                  InstallCondition="VersionNT64 AND VersionNT &gt;= v6.3 AND ADVAPIVERSION &lt; v6.3.10000.0"
                  DetectCondition="UCRTVERSION64 &gt;= v6.2.10585.0"
                  Vital="yes"
                  Cache="no"
                  SuppressSignatureVerification="yes"
                  SourceFile="Redist\KB2999226\Windows8.1-KB2999226-x64.msu"
                  DownloadUrl="https://download.microsoft.com/download/9/6/F/96FD0525-3DDF-423D-8845-5F92F4A6883E/Windows8.1-KB2999226-x64.msu"
                  />

      <MsiPackage SourceFile="$(var.DOKAN_PATH)\dokan_wix\bin\x86\$(var.Configuration)\Dokan_x86.msi"
                  InstallCondition="NOT VersionNT64 AND NOT nodriver"
                  Compressed="yes"
                  Visible="yes"
                  Permanent="yes"
                  >
        <!--<MsiProperty Name="ADDLOCAL" Value="ALL" />-->
        <MsiProperty Name="MSIUNINSTALLSUPERSEDEDCOMPONENTS" Value="1"/>
        <MsiProperty Name="INSTALLDIR" Value="[DokanInstallFolder]" />
        <MsiProperty Name="INSTALLDEVFILES" Value="0" />
      </MsiPackage>

      <MsiPackage SourceFile="$(var.DOKAN_PATH)\dokan_wix\bin\x64\$(var.Configuration)\Dokan_x64.msi"
                  InstallCondition="VersionNT64 AND NOT nodriver"
                  Compressed="yes"
                  Visible="yes"
                  Permanent="yes"
                  >
        <!--<MsiProperty Name="ADDLOCAL" Value="ALL" />-->
        <MsiProperty Name="MSIUNINSTALLSUPERSEDEDCOMPONENTS" Value="1"/>
        <MsiProperty Name="INSTALLDIR" Value="[DokanInstallFolder]" />
        <MsiProperty Name="INSTALLDEVFILES" Value="0" />
      </MsiPackage>

      <MsiPackage Id="KeybasePrograms"
                  SourceFile="$(var.KeybaseApps.TargetPath)"
                  DisplayInternalUI='no'
                  Permanent="no">
        <MsiProperty Name="DOKANPRODUCT86" Value="$(var.DokanProductCodeX86)" />
        <MsiProperty Name="DOKANPRODUCT64" Value="$(var.DokanProductCodeX64)" />
      </MsiPackage>

    </Chain>
	</Bundle>
  <Fragment>    
    <util:FileSearch Id="WINTRUST_FileSearch"
                     Path="[SystemFolder]Wintrust.dll"
                     Variable="WINTRUSTVERSION"
                     Result="version"
                     />
    <!-- check for GDR/LDR version. See https://github.com/dokan-dev/dokany/commit/9782a6491775ef3660a71f40f381d3f48e511f14 -->
    <bal:Condition
      Message="Microsoft patch KB3033929 is not installed. A reboot is needed between the installation of said patch and the installation of Keybase. https://support.microsoft.com/en-us/kb/3033929">
      <![CDATA[Installed OR VersionNT > v6.1 OR (VersionNT = v6.1 AND ((WINTRUSTVERSION >= v6.1.7601.18741 AND WINTRUSTVERSION < v6.1.7601.22000) OR WINTRUSTVERSION >= v6.1.7601.22948))]]>
    </bal:Condition>
  </Fragment>
  <Fragment>
    <util:RegistrySearch
          Id='InnoCLIUninstall'
          Variable="InnoCLIInstalled"
          Result="exists"
          Root="HKLM"
          Key="Software\Microsoft\Windows\CurrentVersion\Uninstall\{70E747DE-4E09-44B0-ACAD-784AA9D79C02}_is1"
          Value="UninstallString" />

    <util:RegistrySearch
          Id='InnoCLIUninstall64'
          After="InnoCLIUninstall"
          Variable="InnoCLIInstalled"
          Condition="NOT InnoCLIInstalled"
          Result="exists"
          Root="HKLM"
          Key="Software\Microsoft\Windows\CurrentVersion\Uninstall\{70E747DE-4E09-44B0-ACAD-784AA9D79C02}_is1"
          Value="UninstallString" 
          Win64="yes"/>

    <!-- These two are for the older inno based KBFS -->
    <util:RegistrySearch Id="InnoKBFSUninstall"
                 Variable="InnoKBFSUninstallString"
                 Root="HKLM"
                 Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{357F272E-BE0E-409F-8E39-0BB9827F5716}_is1"
                 Value="QuietUninstallString"
                 Format="raw"
                 />
    <util:RegistrySearch Id="InnoKBFSUninstall64"
                 After="InnoKBFSUninstall"
                 Condition="NOT InnoKBFSUninstallString"
                 Variable="InnoKBFSUninstallString"
                 Root="HKLM"
                 Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{357F272E-BE0E-409F-8E39-0BB9827F5716}_is1"
                 Value="QuietUninstallString"
                 Format="raw"
                 Win64="yes"
                 />

    <bal:Condition
      Message="Please uninstall any previous Keybase versions before continuing.">
      <![CDATA[NOT InnoCLIInstalled AND NOT InnoKBFSUninstallString]]>
    </bal:Condition>
  </Fragment>

</Wix>
