!insertmacro GetParent

Var CustomCodeCreatedLegacy

!macro CustomCodePreInstall
	${If} ${FileExists} "$INSTDIR\App\KeePassXC\keepassxc.ini"
	${AndIfNot} ${FileExists} "$INSTDIR\Data\settings\keepassxc.ini"
		Rename "$INSTDIR\App\KeePassXC\keepassxc.ini" "$INSTDIR\Data\settings\keepassxc.ini"
	${EndIf}

	${If} ${FileExists} "$INSTDIR\Data\settings\keepassxc.ini"
	${AndIfNot} ${FileExists} "$INSTDIR\Data\settings\keepassxc_local.ini"
		CopyFiles /SILENT "$INSTDIR\Data\settings\keepassxc.ini" "$INSTDIR\Data\keepassxc_local.ini"
		Rename "$INSTDIR\Data\keepassxc_local.ini" "$INSTDIR\Data\settings\keepassxc_local.ini"
	${EndIf}
	
	${If} ${FileExists} "$INSTDIR\App\AppInfo\appinfo.ini"
		ReadINIStr $3 "$INSTDIR\App\AppInfo\appinfo.ini" "Version" "PackageVersion"
		${VersionCompare} $3 "2.7.0.3" $R0
		System::Call kernel32::GetCurrentProcess()i.s
		System::Call kernel32::IsWow64Process(is,*i.r0)
		${GetParent} $INSTDIR $1
		${If} $0 == 0
		${AndIf} $R0 == 2 
		${AndIf} $3 != "2.7.0.0"
		${AndIfNot} ${FileExists} "$1\KeePassXCPortableLegacy\*.*"
			;2.7+ is 64-bit only, make a backup for the 32-bit user
			CreateDirectory "$1\KeePassXCPortableLegacy"
			Rename "$INSTDIR\App" "$1\KeePassXCPortableLegacy\App"
			CreateDirectory "$1\KeePassXCPortableLegacy\Data"
			CopyFiles /SILENT "$INSTDIR\Data\*.*" "$1\KeePassXCPortableLegacy\Data"
			Rename "$1\KeePassXCPortableLegacy\Data\Settings\KeePassXCPortableSettings.ini" "$1\KeePassXCPortableLegacy\Data\Settings\KeePassXCPortableLegacySettings.ini"
			Rename "$INSTDIR\Other" "$1\KeePassXCPortableLegacy\Other"
			Rename "$INSTDIR\KeePassXCPortable.exe" "$1\KeePassXCPortableLegacy\KeePassXCPortableLegacy.exe"
			CopyFiles /SILENT "$INSTDIR\KeePassXCPortable.ini" "$1\KeePassXCPortableLegacy"
			Rename "$1\KeePassXCPortableLegacy\KeePassXCPortable.ini" "$1\KeePassXCPortableLegacy\KeePassXCPortableLegacy.ini"
			Rename "$INSTDIR\help.html" "$1\KeePassXCPortableLegacy\help.html"
			WriteINIStr "$1\KeePassXCPortableLegacy\App\AppInfo\appinfo.ini" "Details" "Name" "KeePassXC Portable Legacy"
			WriteINIStr "$1\KeePassXCPortableLegacy\App\AppInfo\appinfo.ini" "Details" "AppID" "KeePassXCPortableLegacy"
			WriteINIStr "$1\KeePassXCPortableLegacy\App\AppInfo\appinfo.ini" "Details" "Name" "KeePassXC Portable Legacy"
			WriteINIStr "$1\KeePassXCPortableLegacy\App\AppInfo\appinfo.ini" "Version" "PackageVersion" "2.6.6.0"
			WriteINIStr "$1\KeePassXCPortableLegacy\App\AppInfo\appinfo.ini" "Version" "DisplayVersion" "2.6.6 Copied"
			Rename "$1\KeePassXCPortableLegacy\App\AppInfo\Launcher\KeePassXCPortable.ini" "$1\KeePassXCPortableLegacy\App\AppInfo\Launcher\KeePassXCPortableLegacy.ini"
		${EndIf}			
	${EndIf}
!macroend

!macro CustomCodePostInstall
	${If} $CustomCodeCreatedLegacy == true
		${GetParent} $INSTDIR $1
		Delete "$1\KeePassXCPortableLegacy\KeePassXCPortableLegacy.exe"
		CopyFiles /SILENT "$INSTDIR\Other\Legacy\KeePassXCPortableLegacy.exe" "$1\KeePassXCPortableLegacy"
		Delete "$1\KeePassXCPortableLegacy\App\AppInfo\Launcher\KeePassXCPortableLegacy.ini"
		CopyFiles /SILENT "$INSTDIR\Other\Legacy\KeePassXCPortableLegacy.ini" "$1\KeePassXCPortableLegacy\App\AppInfo\Launcher"
		Delete "$1\KeePassXCPortableLegacy\App\AppInfo\Launcher\Custom.nsh"
		CopyFiles /SILENT "$INSTDIR\Other\Legacy\Custom.nsh" "$1\KeePassXCPortableLegacy\App\AppInfo\Launcher"
		RMDir /r "$1\KeePassXCPortableLegacy\App\KeePassXCModern"
		${If} ${FileExists} "$1\KeePassXCPortableLegacy\App\KeePassXCLegacy\*.*"
			RMDir /r "$1\KeePassXCPortableLegacy\App\KeePassXC"
			Rename "$1\KeePassXCPortableLegacy\App\KeePassXCLegacy" "$1\KeePassXCPortableLegacy\App\KeePassXC"
		${EndIf}
		${If} ${FileExists} "$1\KeePassXCPortableLegacy\Data\settings\KeePassXCPortableLegacySettings.ini"
			ReadINIStr $3 "$1\KeePassXCPortableLegacy\Data\settings\KeePassXCPortableLegacySettings.ini" "KeePassXCPortableSettings" "LastDrive"
			WriteINIStr "$1\KeePassXCPortableLegacy\Data\settings\KeePassXCPortableLegacySettings.ini" "KeePassXCPortableLegacySettings" "LastDrive" $3
			ReadINIStr $3 "$1\KeePassXCPortableLegacy\Data\settings\KeePassXCPortableLegacySettings.ini" "KeePassXCPortableSettings" "LastDirectory"
			WriteINIStr "$1\KeePassXCPortableLegacy\Data\settings\KeePassXCPortableLegacySettings.ini" "KeePassXCPortableLegacySettings" "LastDirectory" $3
			DeleteINISec "$1\KeePassXCPortableLegacy\Data\settings\KeePassXCPortableLegacySettings.ini" "KeePassXCPortableSettings"
		${EndIf}
	${EndIf}
!macroend