Var bolCustomCreatedLegacy

!macro CustomCodePreInstall
	${If} ${FileExists} "$INSTDIR\Data\settings\byond.reg"
	${AndIfNot} ${FileExists} "$INSTDIR\Data\BYONDData\*.*"
		CreateDirectory "$INSTDIR\Data\BYONDData"
		WriteINIStr "$INSTDIR\Data\settings\Dantom.reg" "HKEY_CURRENT_USER\Software\Dantom\BYOND" `"userpath"` `"DEFAULTBYONDPORTABLEBASEPATH\\Data\\BYONDData"`
		${If} ${FileExists} "$PROFILE\Documents\BYOND\*.*"
			CopyFiles /SILENT "$PROFILE\Documents\BYOND\*.*" "$INSTDIR\Data\BYONDData"
			MessageBox MB_OK|MB_ICONINFORMATION "Your BYOND data was copied from '$PROFILE\Documents\BYONDData' to BYOND Portable.  If it was previously saved there in error, you can remove the files using Windows Explorer. Our apologies for this inconvenience."
		${Else}
			MessageBox MB_OK|MB_ICONINFORMATION "BYOND Portable may have previously saved your settings to your local Documents folder but the installer could not locate it automatically. You can use Windows Explorer to open your Documents folder and then the BYOND folder within. Copy the contents of that folder to '$INSTDIR\Data\BYONDData' and your settings will be preserved. If you don't use BYOND locally, you can then delete the folder from your Documents folder. Our apologies for this inconvenience."
		${EndIf}
	${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 < 10000 ;Windows 7/8
		${If} ${FileExists} "$INSTDIR\App\AppInfo\appinfo.ini"
			ReadINIStr $0 "$INSTDIR\App\AppInfo\appinfo.ini" "Version" "PackageVersion"
			${VersionCompare} $0 "5.516.1659.0" $R0
			${If} $R0 = 2
				${GetParent} $INSTDIR $1
				${IfNot} ${FileExists} "$1\BYONDPortableLegacyWin7\*.*"
					${GetParent} $INSTDIR $1
					CreateDirectory "$1\BYONDPortableLegacyWin7"
					CopyFiles /SILENT "$INSTDIR\*.*" "$1\BYONDPortableLegacyWin7"
					Rename "$1\BYONDPortableLegacyWin7\BYONDPortable.ini" "$1\BYONDPortableLegacyWin7\BYONDPortableLegacyWin7.ini"
					StrCpy $bolCustomCreatedLegacy true
					Rename "$1\BYONDPortableLegacyWin7\Data\BYONDPortableSettings.ini" "$1\BYONDPortableLegacyWin7\Data\BYONDPortableLegacyWin7Settings.ini"
				${EndIf}	
			${EndIf}
		${EndIf}
	${EndIf}
!macroend

!macro CustomCodePostInstall
	${If} $bolCustomCreatedLegacy == true
		${GetParent} $INSTDIR $1
		Delete "$1\BYONDPortableLegacyWin7\BYONDPortable.exe"
		Delete "$1\BYONDPortableLegacyWin7\App\AppInfo\*.ini"
		Delete "$1\BYONDPortableLegacyWin7\App\AppInfo\Launcher\*.*"
		CopyFiles /SILENT "$INSTDIR\Other\Source\Legacy\BYONDPortableLegacyWin7.exe" "$1\BYONDPortableLegacyWin7"
		CopyFiles /SILENT "$INSTDIR\Other\Source\Legacy\BYONDPortableLegacyWin7.ini" "$1\BYONDPortableLegacyWin7\App\AppInfo\Launcher"
		CopyFiles /SILENT "$INSTDIR\Other\Source\Legacy\custom.nsh" "$1\BYONDPortableLegacyWin7\App\AppInfo\Launcher"
		CopyFiles /SILENT "$INSTDIR\Other\Source\Legacy\appinfo.ini" "$1\BYONDPortableLegacyWin7\App\AppInfo"
		${If} ${FileExists} "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableSettings.ini"
			Rename "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableSettings.ini" "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableLegacyWin7Settings.ini"
			ReadINIStr $0 "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableLegacyWin7Settings.ini" "BYONDPortableSettings" "LastDrive"
			WriteINIStr "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableLegacyWin7Settings.ini" "BYONDPortableLegacyWin7Settings" "LastDrive" "$0"
			ReadINIStr $0 "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableLegacyWin7Settings.ini" "BYONDPortableSettings" "LastDirectory"
			WriteINIStr "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableLegacyWin7Settings.ini" "BYONDPortableLegacyWin7Settings" "LastDirectory" "$0"
			DeleteINISec "$1\BYONDPortableLegacyWin7\Data\settings\BYONDPortableLegacyWin7Settings.ini" "BYONDPortableSettings"
		${EndIf}
	${EndIf}
!macroend
