From eb2862af5c4db3e3a84a77ab1932c80cfdf7304b Mon Sep 17 00:00:00 2001
From: Andrey Volk <andywolk@gmail.com>
Date: Sat, 4 Feb 2017 15:44:29 +0300
Subject: [PATCH] FS-10010 [WIX] Fix windows WIX installer by including a
 proper vc runtime

---
 w32/Setup/Product.2015.wxs   | 48 ++++++++++++++++++++++++++++++------
 w32/Setup/Setup.2015.wixproj |  4 +--
 w32/Setup/Setup.wixproj      |  4 +--
 3 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/w32/Setup/Product.2015.wxs b/w32/Setup/Product.2015.wxs
index c19f86c4f4..5c126c35b5 100644
--- a/w32/Setup/Product.2015.wxs
+++ b/w32/Setup/Product.2015.wxs
@@ -1,13 +1,29 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- Platform specific variables -->
 <?if $(var.Platform) = x64 ?>
-<?define Win64 = "yes" ?>
-<?define ProductName = "FreeSWITCH (64 bit)" ?>
-<?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
+  <?define Win64 = "yes" ?>
+  <?if $(var.Configuration) = Debug ?>
+	<?define ProductName_Configuration = ", Debug" ?>
+  <?else ?>
+	<?define ProductName_Configuration = "" ?>
+  <?endif ?>
+  <?define ProductName = "FreeSWITCH (64 bit$(var.ProductName_Configuration))" ?>
+  <?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
 <?else ?>
-<?define Win64 = "no" ?>
-<?define ProductName = "FreeSWITCH" ?>
-<?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
+  <?define Win64 = "no" ?>
+  <?if $(var.Configuration) = Debug ?>
+	<?define ProductName_Configuration = " (Debug)" ?>
+  <?else ?>
+	<?define ProductName_Configuration = "" ?>
+  <?endif ?>
+  <?define ProductName = "FreeSWITCH$(var.ProductName_Configuration)" ?>
+  <?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
+<?endif ?>
+
+<?if $(var.Configuration) = Debug ?>
+  <?define CRT_Configuration = "Debug" ?>
+<?else ?>
+  <?define CRT_Configuration = "" ?>
 <?endif ?>
 
 <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
@@ -16,6 +32,22 @@
 
 		<Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
 
+		<CustomAction Id='CheckForUCRT' Error="Installation failed because 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" />
+
+		<Property Id="UCRTINSTALLED" Secure="yes">
+		  <DirectorySearch Id="searchSystem2" Path="[SystemFolder]" Depth="0">
+			<FileSearch Id="UCRT_FileSearch"
+					Name="ucrtbase.dll"
+					MinVersion="6.2.10585.0"/>
+		  </DirectorySearch>
+		</Property>
+
+		<InstallExecuteSequence>
+		  <Custom Action='CheckForUCRT' Before='LaunchConditions'>
+			<![CDATA[Not REMOVE="ALL" AND UCRTINSTALLED = ""]]>
+		  </Custom>
+		</InstallExecuteSequence>
+
 		<Directory Id="TARGETDIR" Name="SourceDir">
 			<Directory Id="$(var.PlatformProgramFilesFolder)">
 				<Directory Id="INSTALLLOCATION" Name="FreeSWITCH">
@@ -31,13 +63,13 @@
 					<!-- </Component> -->
 					<?if $(var.Platform) != x64 ?>
 						<Merge Id="CrtFiles_x86"
-						SourceFile="$(env.CommonProgramFiles)\Merge Modules\Microsoft_VC110_CRT_x86.msm"
+						SourceFile="$(env.CommonProgramFiles)\Merge Modules\Microsoft_VC140_$(var.CRT_Configuration)CRT_x86.msm"
 						DiskId="1"
 						Language="1033"/>
 					<?endif ?>
 					<?if $(var.Platform) = x64 ?>
 						<Merge Id="CrtFiles_x64"
-						SourceFile="$(env.CommonProgramFiles)\Merge Modules\Microsoft_VC110_CRT_x64.msm"
+						SourceFile="$(env.CommonProgramFiles)\Merge Modules\Microsoft_VC140_$(var.CRT_Configuration)CRT_x64.msm"
 						DiskId="1"
 						Language="1033"/>
 					<?endif ?>
diff --git a/w32/Setup/Setup.2015.wixproj b/w32/Setup/Setup.2015.wixproj
index ffb35e0310..a2f76824fe 100644
--- a/w32/Setup/Setup.2015.wixproj
+++ b/w32/Setup/Setup.2015.wixproj
@@ -19,7 +19,7 @@
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
     <OutputPath>bin\x86\release\</OutputPath>
     <IntermediateOutputPath>obj\X86\$(Configuration)\</IntermediateOutputPath>
-    <DefineConstants>Debug;FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\Win32\release\sounds;FreeSWITCHBaseDir=$(SolutionDir)Win32\$(Configuration);</DefineConstants>
+    <DefineConstants>FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\Win32\release\sounds;FreeSWITCHBaseDir=$(SolutionDir)Win32\$(Configuration);</DefineConstants>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
     <OutputPath>bin\x64\Debug\</OutputPath>
@@ -31,7 +31,7 @@
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
     <OutputPath>bin\x64\Release\</OutputPath>
     <IntermediateOutputPath>obj\X64\$(Configuration)\</IntermediateOutputPath>
-    <DefineConstants>Debug;FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\x64\release\sounds;FreeSWITCHBaseDir=$(SolutionDir)$(Platform)\$(Configuration);</DefineConstants>
+    <DefineConstants>FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\x64\release\sounds;FreeSWITCHBaseDir=$(SolutionDir)$(Platform)\$(Configuration);</DefineConstants>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="Fragments\FreeSWITCHConfFiles.wxs" />
diff --git a/w32/Setup/Setup.wixproj b/w32/Setup/Setup.wixproj
index 59b4ce719b..642d721145 100644
--- a/w32/Setup/Setup.wixproj
+++ b/w32/Setup/Setup.wixproj
@@ -19,7 +19,7 @@
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
     <OutputPath>bin\x86\release\</OutputPath>
     <IntermediateOutputPath>obj\X86\$(Configuration)\</IntermediateOutputPath>
-    <DefineConstants>Debug;FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\Win32\release\sounds;</DefineConstants>
+    <DefineConstants>FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\Win32\release\sounds;</DefineConstants>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
     <OutputPath>bin\x64\Debug\</OutputPath>
@@ -31,7 +31,7 @@
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
     <OutputPath>bin\x64\Release\</OutputPath>
     <IntermediateOutputPath>obj\X64\$(Configuration)\</IntermediateOutputPath>
-    <DefineConstants>Debug;FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\x64\release\sounds;</DefineConstants>
+    <DefineConstants>FreeSWITCHConfFilesDir=$(ProjectDir)..\..\conf\vanilla;FreeSWITCHSoundFilesDir=$(ProjectDir)..\..\x64\release\sounds;</DefineConstants>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="Fragments\FreeSWITCHConfFiles.wxs" />