From 2c020848d5663319552618841bb5e3838a983011 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anthony=20L=C3=A9onard?=
 <anthony.leonard@savoirfairelinux.com>
Date: Tue, 11 Apr 2017 11:04:33 -0400
Subject: [PATCH] setup: detect a previous install of Ring
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When Ring is installed, some keys are set in the Registry to indicate
various locations, including the uninstall.exe and the root install
folder.

Now, the installer check the presence of this last value and use it as
the default install location. With this change, updating Ring is just
about pressing “Next” even if it is installed in a custom location.

Change-Id: I48b28c840c094c94f03e763c72f29d833aaa690b
Reviewed-by: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
Tuleap: #1460
---
 ring.nsi | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)

diff --git a/ring.nsi b/ring.nsi
index 458d8b2..97e3f4e 100644
--- a/ring.nsi
+++ b/ring.nsi
@@ -46,6 +46,51 @@ outFile "ring-windows-nightly.exe"
 !include LogicLib.nsh
 !include "FileFunc.nsh"
 
+Function StrRep
+  Exch $R4 ; $R4 = Replacement String
+  Exch
+  Exch $R3 ; $R3 = String to replace (needle)
+  Exch 2
+  Exch $R1 ; $R1 = String to do replacement in (haystack)
+  Push $R2 ; Replaced haystack
+  Push $R5 ; Len (needle)
+  Push $R6 ; len (haystack)
+  Push $R7 ; Scratch reg
+  StrCpy $R2 ""
+  StrLen $R5 $R3
+  StrLen $R6 $R1
+loop:
+  StrCpy $R7 $R1 $R5
+  StrCmp $R7 $R3 found
+  StrCpy $R7 $R1 1 ; - optimization can be removed if U know len needle=1
+  StrCpy $R2 "$R2$R7"
+  StrCpy $R1 $R1 $R6 1
+  StrCmp $R1 "" done loop
+found:
+  StrCpy $R2 "$R2$R4"
+  StrCpy $R1 $R1 $R6 $R5
+  StrCmp $R1 "" done loop
+done:
+  StrCpy $R3 $R2
+  Pop $R7
+  Pop $R6
+  Pop $R5
+  Pop $R2
+  Pop $R1
+  Pop $R4
+  Exch $R3
+FunctionEnd
+
+!macro _StrReplaceConstructor ORIGINAL_STRING TO_REPLACE REPLACE_BY
+  Push "${ORIGINAL_STRING}"
+  Push "${TO_REPLACE}"
+  Push "${REPLACE_BY}"
+  Call StrRep
+  Pop $0
+!macroend
+
+!define StrReplace '!insertmacro "_StrReplaceConstructor"'
+
 !macro VerifyUserIsAdmin
 UserInfo::GetAccountType
 pop $0
@@ -61,12 +106,19 @@ ${EndIf}
 function .onInit
         setShellVarContext all
         !insertmacro VerifyUserIsAdmin
+        ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "InstallLocation"
+        StrCmp $0 "" notfound
+        ${StrReplace} $0 "$\"" ""
+        StrCpy $INSTDIR $0
+        Goto done
+notfound:
         StrCpy $INSTDIR "$PROGRAMFILES\${COMPANYNAME}\${APPNAME}"
         ${If} ${ARCH} == "x64"
          ${If} ${RunningX64}
             StrCpy $INSTDIR "$PROGRAMFILES64\${COMPANYNAME}\${APPNAME}"
          ${EndIf}
         ${EndIf}
+done:
 functionEnd
 
 Function LaunchLink
@@ -116,7 +168,7 @@ section "install"
         WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayName" ${APPNAME}
         WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
         WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "QuietUninstallString" "$\"$INSTDIR\uninstall.exe$\" /S"
-        WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "InstallLocation" "$\"$INSTDIR$\""
+        WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "InstallLocation" $INSTDIR
         WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayIcon" "$\"$INSTDIR\ring.ico$\""
         WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "Publisher" "${COMPANYNAME}"
         WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "HelpLink" "$\"${HELPURL}$\""
-- 
GitLab