${SegmentFile}

${SegmentInit}
    ${If} $Bits = 64
        ${SetEnvironmentVariablesPath} FullAppDir $EXEDIR\App\StreamWriter64
    ${Else}
        ${SetEnvironmentVariablesPath} FullAppDir $EXEDIR\App\StreamWriter32
    ${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'
	${EndIf}
	
	ExpandEnvStrings $1 "%PortableApps.comMusic%"
	${If} $1 == ""
	${OrIfNot} ${FileExists} "$1\*.*"
		ExpandEnvStrings $1 "%PortableApps.comDocuments%"
		${If} ${FileExists} "$1\Music\*.*"
			StrCpy $2 "$1\Music"
		${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\Music\*.*"
				StrCpy $2 "$1\Music"
			${Else}
				${GetRoot} $EXEDIR $1
				${If} ${FileExists} "$1\Music\*.*"
					StrCpy $2 "$1\Music"
				${Else}
					StrCpy $2 "$1"
				${EndIf}
			${EndIf}
		${EndIf}
		System::Call 'Kernel32::SetEnvironmentVariable(t, t) i("PortableApps.comMusic", "$2").r0'
	${EndIf}
!macroend

;${SegmentPreExec}
;	StrCpy $0 "$EXEDIR\App\StreamWriter32\streamwriter_settings.ini"
;	${If} $Bits = 64
;	${AndIf} ${FileExists} "$EXEDIR\App\StreamWriter64\streamwriter_settings.ini"
;		StrCpy $0 "$EXEDIR\App\StreamWriter64\streamwriter_settings.ini"
;	${EndIf}
;	${If} ${FileExists} $0
;		ReadINIStr $1 $0 "Settings" "Dir"
;		MessageBox MB_OK "$1"
;		${IfNot} ${FileExists} $1
;			CreateDirectory $1
;		${EndIf}
;		ReadINIStr $1 $0 "Settings" "DirAuto"
;		${IfNot} ${FileExists} $1
;			CreateDirectory $1
;		${EndIf}
;	${EndIf}
;!macroend
