${SegmentFile}

Var intCustomCurrentBuild

${Segment.OnInit}
	ReadINIStr $R0 "$EXEDIR\App\AppInfo\AppInfo.ini" "Details" "InstallType"

	;Hack until PA.c Launcher gets NSIS3
	${If} ${AtLeastWinXP}
		${registry::Read} "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentBuild" $R1 $R2
		${If} $R2 == "REG_SZ"
			StrCpy $intCustomCurrentBuild $R1
		${Else}
			StrCpy $intCustomCurrentBuild 0
		${EndIf}
	${Else}
		StrCpy $intCustomCurrentBuild 0
	${EndIf}

	${If} $intCustomCurrentBuild < 10000 ;earlier than Windows 10
	${AndIfNot} ${FileExists} "$EXEDIR\App\SigilLegacy\*.*"
	${AndIfNot} ${FileExists} "$EXEDIR\App\SigilModern\*.*"
	${AndIfNot} $R0 == "ModernPlusLegacy"
		StrCpy $AppName "Sigil"
		StrCpy $0 "10"
		StrCpy $1 "7/8/8.1"
	
		MessageBox MB_OK "$(LauncherIncompatibleMinOS)"
		Abort
	${EndIf}
	
	System::Call kernel32::GetCurrentProcess()i.s
	System::Call kernel32::IsWow64Process(is,*i.r0)
	${If} $0 == 0
	${AndIfNot} ${FileExists} "$EXEDIR\App\SigilLegacy\*.*"
	${AndIfNot} ${FileExists} "$EXEDIR\App\SigilModern\*.*"
	${AndIfNot} $R0 == "ModernPlusLegacy"
		StrCpy $AppName "Sigil"
		StrCpy $0 "10/11 64-bit"
		StrCpy $1 "32-bit"
		MessageBox MB_OK "$(LauncherIncompatibleMinOS)"
		Abort
	${EndIf}
	;MessageBox MB_OK $intCustomCurrentBuild
!macroend

${SegmentInit}
	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'
	${EndIf}
	
	ExpandEnvStrings $1 "%PortableApps.comBooks%"
	${If} $1 == ""
	${OrIfNot} ${FileExists} "$1\*.*"
		ExpandEnvStrings $1 "%PortableApps.comDocuments%"
		${If} ${FileExists} "$1\Books\*.*"
			StrCpy $2 "$1\Books"
		${Else}
			${GetParent} $EXEDIR $3
			${GetParent} $3 $1
			${If} $1 == "" ;Be sure we didn't just GetParent on Root
				StrCpy $1 $3
			${EndIf}
			${If} ${FileExists} "$1\Books\*.*"
				StrCpy $2 "$1\Books"
			${Else}
				${GetRoot} $EXEDIR $1
				${If} ${FileExists} "$1\Books\*.*"
					StrCpy $2 "$1\Books"
				${Else}
					StrCpy $2 "$1"
				${EndIf}
			${EndIf}
		${EndIf}
		System::Call 'Kernel32::SetEnvironmentVariable(t, t) i("PortableApps.comBooks", "$2").r0'
	${EndIf}
	
	ExpandEnvStrings $2 "%PortableApps.comBooks%"
	StrLen $0 $2
	${If} $0 == 2
		StrCpy $2 "$2\"
	${EndIf}
	${WordReplace} $2 "\" "/" "+" $2
	System::Call 'Kernel32::SetEnvironmentVariable(t, t) i("CustomPortableApps.comBooksForwardSlash", "$2").r0'
!macroend
