+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+; Uninstall/Reinstall page functions ;
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+!ifdef VER_MAJOR & VER_MINOR & VER_REVISION & VER_BUILD
+
+Var ReinstallPageCheck
+
+Function PageReinstall
+
+ ReadRegStr $R0 HKLM "Software\${PRODUCT_NAME}" ""
+
+ ${If} $R0 == ""
+ ReadRegStr $R0 HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
+ ${If} $R0 == ""
+ Abort
+ ${EndIf}
+ ${EndIf}
+
+ ReadRegDWORD $R0 HKLM "Software\${PRODUCT_NAME}" "VersionMajor"
+ ReadRegDWORD $R1 HKLM "Software\${PRODUCT_NAME}" "VersionMinor"
+ ReadRegDWORD $R2 HKLM "Software\${PRODUCT_NAME}" "VersionRevision"
+ ReadRegDWORD $R3 HKLM "Software\${PRODUCT_NAME}" "VersionBuild"
+ StrCpy $R0 $R0.$R1.$R2.$R3
+
+ ${VersionCompare} ${VER_MAJOR}.${VER_MINOR}.${VER_REVISION}.${VER_BUILD} $R0 $R0
+ ${If} $R0 == 0
+ StrCpy $R1 "${PRODUCT_NAME} ${PRODUCT_VERSION} is already installed. Select the operation you want to perform and click Next to continue."
+ StrCpy $R2 "Add/Reinstall components"
+ StrCpy $R3 "Uninstall ${PRODUCT_NAME}"
+ !insertmacro MUI_HEADER_TEXT "Already Installed" "Choose the maintenance option to perform."
+ StrCpy $R0 "2"
+ ${ElseIf} $R0 == 1
+ StrCpy $R1 "An older version of ${PRODUCT_NAME} is installed on your system. It's recommended that you uninstall the current version before installing. Select the operation you want to perform and click Next to continue."
+ StrCpy $R2 "Uninstall before installing"
+ StrCpy $R3 "Do not uninstall"
+ !insertmacro MUI_HEADER_TEXT "Already Installed" "Choose how you want to install ${PRODUCT_NAME}."
+ StrCpy $R0 "1"
+ ${ElseIf} $R0 == 2
+ StrCpy $R1 "A newer version of ${PRODUCT_NAME} is already installed! It is not recommended that you install an older version. If you really want to install this older version, it's better to uninstall the current version first. Select the operation you want to perform and click Next to continue."
+ StrCpy $R2 "Uninstall before installing"
+ StrCpy $R3 "Do not uninstall"
+ !insertmacro MUI_HEADER_TEXT "Already Installed" "Choose how you want to install ${PRODUCT_NAME}."
+ StrCpy $R0 "1"
+ ${Else}
+ Abort
+ ${EndIf}
+
+ nsDialogs::Create /NOUNLOAD 1018
+
+ ${NSD_CreateLabel} 0 0 100% 24u $R1
+ Pop $R1
+
+ ${NSD_CreateRadioButton} 30u 50u -30u 8u $R2
+ Pop $R2
+ ${NSD_OnClick} $R2 PageReinstallUpdateSelection
+
+ ${NSD_CreateRadioButton} 30u 70u -30u 8u $R3
+ Pop $R3
+ ${NSD_OnClick} $R3 PageReinstallUpdateSelection
+
+ ${If} $ReinstallPageCheck != 2
+ SendMessage $R2 ${BM_SETCHECK} ${BST_CHECKED} 0
+ ${Else}
+ SendMessage $R3 ${BM_SETCHECK} ${BST_CHECKED} 0
+ ${EndIf}
+
+ nsDialogs::Show
+
+FunctionEnd
+
+Function PageReinstallUpdateSelection
+
+ Pop $R1
+
+ ${NSD_GetState} $R2 $R1
+
+ ${If} $R1 == ${BST_CHECKED}
+ StrCpy $ReinstallPageCheck 1
+ ${Else}
+ StrCpy $ReinstallPageCheck 2
+ ${EndIf}
+
+FunctionEnd
+
+Function PageLeaveReinstall
+
+ ${NSD_GetState} $R2 $R1
+
+ StrCmp $R0 "1" 0 +2
+ StrCmp $R1 "1" reinst_uninstall reinst_done
+
+ StrCmp $R0 "2" 0 +3
+ StrCmp $R1 "1" reinst_done reinst_uninstall
+
+ reinst_uninstall:
+ ReadRegStr $R1 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "UninstallString"
+
+ ;Run uninstaller
+ HideWindow
+
+ ClearErrors
+ ExecWait '$R1 _?=$INSTDIR'
+
+ IfErrors no_remove_uninstaller
+ IfFileExists "$INSTDIR\bin\${PRODUCT_NAME}.exe" no_remove_uninstaller
+
+ Delete $R1
+ RMDir $INSTDIR
+
+ no_remove_uninstaller:
+
+ StrCmp $R0 "2" 0 +2
+ Quit
+
+ BringToFront
+
+ reinst_done:
+
+FunctionEnd
+
+!endif # VER_MAJOR & VER_MINOR & VER_REVISION & VER_BUILD