From 8f51beda173e5270a24afb810a5ecd36d2572e11 Mon Sep 17 00:00:00 2001
From: howard-stearns <howard.stearns@gmail.com>
Date: Wed, 21 Jun 2017 16:54:47 -0700
Subject: [PATCH 1/2] installer allows /nSandboxIfNew command line switch,
 which defaults sandbox to off if this is a new install

---
 cmake/templates/NSIS.template.in | 73 +++++++++++++++++++++++++++++++-
 1 file changed, 72 insertions(+), 1 deletion(-)

diff --git a/cmake/templates/NSIS.template.in b/cmake/templates/NSIS.template.in
index bdba621cb5..baf685adaf 100644
--- a/cmake/templates/NSIS.template.in
+++ b/cmake/templates/NSIS.template.in
@@ -29,6 +29,63 @@
   !include "WinVer.nsh"
 
 ;--------------------------------
+; Utilities and Functions
+    ;--------------------------------
+    ; START String Contains Macro
+    ; Taken from http://nsis.sourceforge.net/StrContains
+    ;--------------------------------
+    ; StrContains
+    ; This function does a case sensitive searches for an occurrence of a substring in a string.
+    ; It returns the substring if it is found.
+    ; Otherwise it returns null("").
+    ; Written by kenglish_hi
+    ; Adapted from StrReplace written by dandaman32
+
+
+    Var STR_HAYSTACK
+    Var STR_NEEDLE
+    Var STR_CONTAINS_VAR_1
+    Var STR_CONTAINS_VAR_2
+    Var STR_CONTAINS_VAR_3
+    Var STR_CONTAINS_VAR_4
+    Var STR_RETURN_VAR
+
+    Function StrContains
+      Exch $STR_NEEDLE
+      Exch 1
+      Exch $STR_HAYSTACK
+      ; Uncomment to debug
+      ;MessageBox MB_OK 'STR_NEEDLE = $STR_NEEDLE STR_HAYSTACK = $STR_HAYSTACK '
+        StrCpy $STR_RETURN_VAR ""
+        StrCpy $STR_CONTAINS_VAR_1 -1
+        StrLen $STR_CONTAINS_VAR_2 $STR_NEEDLE
+        StrLen $STR_CONTAINS_VAR_4 $STR_HAYSTACK
+        loop:
+          IntOp $STR_CONTAINS_VAR_1 $STR_CONTAINS_VAR_1 + 1
+          StrCpy $STR_CONTAINS_VAR_3 $STR_HAYSTACK $STR_CONTAINS_VAR_2 $STR_CONTAINS_VAR_1
+          StrCmp $STR_CONTAINS_VAR_3 $STR_NEEDLE found
+          StrCmp $STR_CONTAINS_VAR_1 $STR_CONTAINS_VAR_4 done
+          Goto loop
+        found:
+          StrCpy $STR_RETURN_VAR $STR_NEEDLE
+          Goto done
+        done:
+       Pop $STR_NEEDLE ;Prevent "invalid opcode" errors and keep the
+       Exch $STR_RETURN_VAR
+    FunctionEnd
+
+    !macro _StrContainsConstructor OUT NEEDLE HAYSTACK
+      Push `${HAYSTACK}`
+      Push `${NEEDLE}`
+      Call StrContains
+      Pop `${OUT}`
+    !macroend
+
+    !define StrContains '!insertmacro "_StrContainsConstructor"'
+    ;--------------------------------
+    ; END String Contains Macro
+    ;--------------------------------
+;--------------------------------
 ;General
   ; leverage the UAC NSIS plugin to promote uninstaller to elevated privileges
   !include UAC.nsh
@@ -119,9 +176,11 @@ Var AR_RegFlags
   ClearErrors
   ;Reading component status from registry
   ReadRegDWORD $AR_RegFlags HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@\Components\${SecName}" "Installed"
-  IfErrors "default_${SecName}"
+  IfErrors "checkBeforeDefault_${SecName}"
     ;Status will stay default if registry value not found
     ;(component was never installed)
+
+  "set_initSection_${SecName}:"
   IntOp $AR_RegFlags $AR_RegFlags & ${SF_SELECTED} ;Turn off all other bits
   SectionGetFlags ${${SecName}} $AR_SecFlags  ;Reading default section flags
   IntOp $AR_SecFlags $AR_SecFlags & 0xFFFE  ;Turn lowest (enabled) bit off
@@ -137,6 +196,18 @@ Var AR_RegFlags
  "default_${SecName}:"
 
  !insertmacro LoadSectionSelectedIntoVar ${SecName} ${SecName}_selected
+ Goto "end_initSection_${SecName}"
+
+ "checkBeforeDefault_${SecName}:"
+ ${StrContains} $AR_RegFlags "/nSandboxIfNew" $CMDLINE
+ ${If} "${SecName}" == "server"
+ ${AndIfNot} $AR_RegFlags == ""
+   StrCpy $AR_RegFlags 0
+   Goto "set_initSection_${SecName}"
+ ${Else}
+   Goto "default_${SecName}"
+ ${EndIf}
+ "end_initSection_${SecName}:"
 !macroend
 
 !macro FinishSection SecName

From cec4b4e86b39c801d05204301e8229d51b313746 Mon Sep 17 00:00:00 2001
From: howard-stearns <howard.stearns@gmail.com>
Date: Thu, 22 Jun 2017 12:54:30 -0700
Subject: [PATCH 2/2] change var to a global per CR

---
 cmake/templates/NSIS.template.in | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/cmake/templates/NSIS.template.in b/cmake/templates/NSIS.template.in
index baf685adaf..033e9e0255 100644
--- a/cmake/templates/NSIS.template.in
+++ b/cmake/templates/NSIS.template.in
@@ -149,6 +149,7 @@
 ; inter-component dependencies.
 Var AR_SecFlags
 Var AR_RegFlags
+Var substringResult
 @CPACK_NSIS_SECTION_SELECTED_VARS@
 
 ; Loads the "selected" flag for the section named SecName into the
@@ -199,9 +200,9 @@ Var AR_RegFlags
  Goto "end_initSection_${SecName}"
 
  "checkBeforeDefault_${SecName}:"
- ${StrContains} $AR_RegFlags "/nSandboxIfNew" $CMDLINE
+ ${StrContains} $substringResult "/nSandboxIfNew" $CMDLINE
  ${If} "${SecName}" == "server"
- ${AndIfNot} $AR_RegFlags == ""
+ ${AndIfNot} $substringResult == ""
    StrCpy $AR_RegFlags 0
    Goto "set_initSection_${SecName}"
  ${Else}