; Copyleft (c) n/a-n/a Azure Zanculmarktum

!include LogicLib.nsh
!include WordFunc.nsh
!insertmacro WordReplace

!ifdef NSIS_UNICODE
	Var _UpdateLocationsVar0
	Var _UpdateLocationsVar1

	!macro _UpdateLocations InputFile Slash
		${If} ${FileExists} "${InputFile}"
			${WordReplace} "$LASTDIR" "\" "${Slash}" "+" $_UpdateLocationsVar0
			${WordReplace} "$EXEDIR" "\" "${Slash}" "+" $_UpdateLocationsVar1

			StrCmp $LASTDIR "" +11
			StrCmp $LASTDIR $EXEDIR +10
				newtextreplace::_ReplaceInFile /NOUNLOAD "${InputFile}" "${InputFile}.NewFile" "$_UpdateLocationsVar0" "$_UpdateLocationsVar1" ""
				Pop $_UpdateLocationsVar0
				IntCmp $_UpdateLocationsVar0 "0" +6 +4 ""
					Delete "${InputFile}"
					Rename "${InputFile}.NewFile" "${InputFile}"
					Goto +4
					SetErrors
					Goto +2
					Delete "${InputFile}.NewFile"

			StrCmp $LASTDRIVE "" +11
			StrCmp $LASTDRIVE $ROOTDIR +10
				newtextreplace::_ReplaceInFile /NOUNLOAD "${InputFile}" "${InputFile}.NewFile" "$LASTDRIVE${Slash}" "$ROOTDIR${Slash}" ""
				Pop $_UpdateLocationsVar0
				IntCmp $_UpdateLocationsVar0 "0" +6 +4 ""
					Delete "${InputFile}"
					Rename "${InputFile}.NewFile" "${InputFile}"
					Goto +4
					SetErrors
					Goto +2
					Delete "${InputFile}.NewFile"

			newtextreplace::_Unload

			StrCpy $_UpdateLocationsVar0 ""
			StrCpy $_UpdateLocationsVar1 ""
		${EndIf}
	!macroend

	!define UpdateLocations "!insertmacro _UpdateLocations"
!else
	Var _UpdateLocationsVar0
	Var _UpdateLocationsVar1

	!macro _UpdateLocations InputFile Slash
		${If} ${FileExists} "${InputFile}"
			${WordReplace} "$LASTDIR" "\" "${Slash}" "+" $_UpdateLocationsVar0
			${WordReplace} "$EXEDIR" "\" "${Slash}" "+" $_UpdateLocationsVar1

			StrCmp $LASTDIR "" +11
			StrCmp $LASTDIR $EXEDIR +10
				textreplace::_ReplaceInFile /NOUNLOAD "${InputFile}" "${InputFile}.NewFile" "$_UpdateLocationsVar0" "$_UpdateLocationsVar1" ""
				Pop $_UpdateLocationsVar0
				IntCmp $_UpdateLocationsVar0 "0" +6 +4 ""
					Delete "${InputFile}"
					Rename "${InputFile}.NewFile" "${InputFile}"
					Goto +4
					SetErrors
					Goto +2
					Delete "${InputFile}.NewFile"

			StrCmp $LASTDRIVE "" +11
			StrCmp $LASTDRIVE $ROOTDIR +10
				textreplace::_ReplaceInFile /NOUNLOAD "${InputFile}" "${InputFile}.NewFile" "$LASTDRIVE${Slash}" "$ROOTDIR${Slash}" ""
				Pop $_UpdateLocationsVar0
				IntCmp $_UpdateLocationsVar0 "0" +6 +4 ""
					Delete "${InputFile}"
					Rename "${InputFile}.NewFile" "${InputFile}"
					Goto +4
					SetErrors
					Goto +2
					Delete "${InputFile}.NewFile"

			textreplace::_Unload

			StrCpy $_UpdateLocationsVar0 ""
			StrCpy $_UpdateLocationsVar1 ""
		${EndIf}
	!macroend

	!define UpdateLocations "!insertmacro _UpdateLocations"
!endif