${SegmentFile}

Var CustomFirstWin32RunDone
Var CustomOriginalBits

${SegmentInit}
	ReadRegStr $0 HKLM "Software\Microsoft\Windows NT\CurrentVersion" "CurrentBuild"

	StrCpy $CustomOriginalBits $Bits
	
	${If} $0 > 9999
		;Windows 10/11
		${If} ${FileExists} "$EXEDIR\App\KeePassXCModern"
			Rename "$EXEDIR\App\KeePassXC" "$EXEDIR\App\KeePassXCLegacy"
			Rename "$EXEDIR\App\KeePassXCModern" "$EXEDIR\App\KeePassXC"
		${EndIf}
	${Else}
		;Windows 7/8/8.1
		${If} ${FileExists} "$EXEDIR\App\KeePassXCLegacy"
			Rename "$EXEDIR\App\KeePassXC" "$EXEDIR\App\KeePassXCModern"
			Rename "$EXEDIR\App\KeePassXCLegacy" "$EXEDIR\App\KeePassXC"
		${EndIf}
	${EndIf}

	ExpandEnvStrings $1 "%PortableApps.comDocuments%"
	${If} $1 == ""
	${OrIfNot} ${FileExists} "$1\*.*"
		${GetParent} $EXEDIR $3
		${GetParent} $3 $1
		${If} $1 == "" ;Be sure we didn't just GetParent on Root
			StrCpy $1 $3
		${EndIf}
		${If} ${FileExists} "$1\Documents\*.*"
			StrCpy $2 "$1\Documents"
		${Else}
			${GetRoot} $EXEDIR $1
			${If} ${FileExists} "$1\Documents\*.*"
				StrCpy $2 "$1\Documents"
			${Else}
				StrCpy $2 "$1"
			${EndIf}
		${EndIf}
		System::Call 'Kernel32::SetEnvironmentVariable(t, t) i("PortableApps.comDocuments", "$2").r0'
		${WordReplace} "$2" "\" "/" "+" $2
		System::Call 'Kernel32::SetEnvironmentVariable(t, t) i("PortableApps.comDocuments:ForwardSlash", "$2").r0'
	${EndIf}
!macroend

${SegmentPrePrimary}
	${If} $Bits = 32
		ClearErrors
		${GetParent} $EXEDIR $0
		${If} ${FileExists} "$0\KeePassXCPortableLegacy\*.*"
			MessageBox MB_ICONINFORMATION|MB_OK "KeePassXC has dropped support for 32-bit Windows. A legacy version of KeePassXC Portable has been created at $0\KeePassXCPortableLegacy. It will not receive further security updates or bug fixes. It is recommended you use KeePass Pro Portable which can read your existing kdbx files."
		${Else}
			MessageBox MB_ICONINFORMATION|MB_OK "KeePassXC has dropped support for 32-bit Windows. It is recommended you use KeePass Pro Portable which can read your existing kdbx files. You can download KeePassXC Portable Legacy from PortableApps.com as well, but it will not receive further security updates."
		${EndIf}
		StrCpy $CustomFirstWin32RunDone true
	${EndIf}
!macroend

${SegmentPost}
	${If} $Bits = 32
	${AndIf} $CustomFirstWin32RunDone == true
		WriteINIStr "$EXEDIR\Data\settings\KeePassXCPortableSettings.ini" "FirstWin32RunDone" "Done" "true"
	${EndIf}
!macroend