Var bolCustomCreatedLegacy

!macro CustomCodePreInstall
	${If} ${FileExists} "$INSTDIR\Data\user\*.*"
		Rename "$INSTDIR\Data\user" "$INSTDIR\Data\.rednotebook"
		${ConfigWrite} "$INSTDIR\Data\.rednotebook\configuration.cfg" "dataDir=" "$INSTDIR\Data\.rednotebook\data" $R0
	${EndIf}
	${If} ${FileExists} "$INSTDIR\Data\.rednotebook\*.*"
	${AndIfNot} ${FileExists} "$INSTDIR\Data\usersettings\*.*"
		CreateDirectory "$INSTDIR\Data\usersettings"
		Rename "$INSTDIR\Data\.rednotebook\templates" "$INSTDIR\Data\usersettings\templates"
		Rename "$INSTDIR\Data\.rednotebook\tmp" "$INSTDIR\Data\usersettings\tmp"
		Rename "$INSTDIR\Data\.rednotebook\configuration.cfg" "$INSTDIR\Data\usersettings\configuration.cfg"
		Rename "$INSTDIR\Data\.rednotebook\rednotebook.log" "$INSTDIR\Data\usersettings\rednotebook.log"
		CopyFiles /SILENT "$INSTDIR\Data\.rednotebook\data\*.*" "$INSTDIR\Data\usersettings\data"
		RMDir /r "$INSTDIR\Data\.rednotebook"
		${ConfigWrite} "$INSTDIR\Data\usersettings\configuration.cfg" "dataDir=" "../../Data/usersettings/data" $R0
	${EndIf}
	${If} ${FileExists} "$INSTDIR\Data\.rednotebook\*.*"
	${AndIf} ${FileExists} "$INSTDIR\Data\usersettings\*.*"
		CopyFiles /SILENT "$INSTDIR\Data\.rednotebook\data\*.*" "$INSTDIR\Data\usersettings\data"
		RMDir /r "$INSTDIR\Data\.rednotebook"
		${ConfigWrite} "$INSTDIR\Data\usersettings\configuration.cfg" "dataDir=" "../../Data/usersettings/data" $R0
	${EndIf}
	
	System::Call kernel32::GetCurrentProcess()i.s
	System::Call kernel32::IsWow64Process(is,*i.r0)
	ReadRegStr $1 HKLM "Software\Microsoft\Windows NT\CurrentVersion" "CurrentBuild"
	
	${If} $1 < 9600 ;Windows 7/8
	${OrIf} $0 == 0 ;or 32-bit 8.1/10
		${If} ${FileExists} "$INSTDIR\Data\usersettings\*.*"
			${VersionCompare} $0 "2.30.0.0" $R0
			${If} $R0 = 2
				${GetParent} $INSTDIR $1
				${IfNot} ${FileExists} "$1\RedNotebookPortableLegacyx86\*.*"
					${GetParent} $INSTDIR $1
					CreateDirectory "$1\RedNotebookPortableLegacyx86"
					CreateDirectory "$1\RedNotebookPortableLegacyx86\App"
					CreateDirectory "$1\RedNotebookPortableLegacyx86\App\AppInfo"
					CreateDirectory "$1\RedNotebookPortableLegacyx86\App\DefaultData"
					CreateDirectory "$1\RedNotebookPortableLegacyx86\Data"
					CreateDirectory "$1\RedNotebookPortableLegacyx86\Other"
					CopyFiles /SILENT /FILESONLY "$INSTDIR\*.*" "$1\RedNotebookPortableLegacyx86"
					CopyFiles /SILENT "$INSTDIR\App\AppInfo\*.*" "$1\RedNotebookPortableLegacyx86\App\AppInfo"
					CopyFiles /SILENT "$INSTDIR\App\DefaultData\*.*" "$1\RedNotebookPortableLegacyx86\App\DefaultData"
					${If} ${FileExists} "$INSTDIR\App\RednotebookLegacy\*.*"
						Rename "$INSTDIR\App\RednotebookLegacy" "$1\RedNotebookPortableLegacyx86\App\Rednotebook"
					${Else}
						Rename "$INSTDIR\App\Rednotebook" "$1\RedNotebookPortableLegacyx86\App\Rednotebook"
					${EndIf}
					CopyFiles /SILENT "$INSTDIR\Data\*.*" "$1\RedNotebookPortableLegacyx86\Data"
					CopyFiles /SILENT "$INSTDIR\Other\*.*" "$1\RedNotebookPortableLegacyx86\Other"
					WriteINIStr "$1\RedNotebookPortableLegacyx86\App\AppInfo\AppInfo.ini" "Details" "AppID" "RedNotebookPortableLegacyx86"
					WriteINIStr "$1\RedNotebookPortableLegacyx86\App\AppInfo\AppInfo.ini" "Details" "Name" "RedNotebook Portable (Legacy 32-bit)"
					WriteINIStr "$1\RedNotebookPortableLegacyx86\App\AppInfo\AppInfo.ini" "Version" "PackageVersion" "2.26.0.0"
					WriteINIStr "$1\RedNotebookPortableLegacyx86\App\AppInfo\AppInfo.ini" "Version" "DisplayVersion" "2.26"
					StrCpy $bolCustomCreatedLegacy true
				${EndIf}	
			${EndIf}
		${EndIf}
	${EndIf}
!macroend

!macro CustomCodePostInstall
	${ConfigWrite} "$INSTDIR\App\RedNotebook\files\default.cfg" "portable=" "1" $R0
	${ConfigWrite} "$INSTDIR\App\RedNotebook\files\default.cfg" "userDir=" "../../Data/usersettings" $R0
	${If} ${FileExists} "$INSTDIR\App\Rednotebook\etc\fonts\fonts.conf"
		Delete "$INSTDIR\App\Rednotebook\etc\fonts\fonts.conf"
	${EndIf}
	
	${If} $bolCustomCreatedLegacy == true
		${GetParent} $INSTDIR $1
		Delete "$1\RedNotebookPortableLegacyx86\RedNotebookPortable.exe"
		Delete "$1\RedNotebookPortableLegacyx86\App\AppInfo\Launcher\custom.nsh"
		Delete "$1\RedNotebookPortableLegacyx86\App\AppInfo\Launcher\RedNotebookPortable.ini"
		CopyFiles /SILENT "$INSTDIR\Other\Source\Legacy\RedNotebookPortable.exe" "$1\RedNotebookPortableLegacyx86"
		CopyFiles /SILENT "$INSTDIR\Other\Source\Legacy\custom.nsh" "$1\RedNotebookPortableLegacyx86\App\AppInfo\Launcher"
		CopyFiles /SILENT "$INSTDIR\Other\Source\Legacy\RedNotebookPortable.ini" "$1\RedNotebookPortableLegacyx86\App\AppInfo\Launcher"
	${EndIf}
!macroend
