;Copyright 2007-2015 John T. Haller of PortableApps.com
;Website: http://PortableApps.com/

;This software is OSI Certified Open Source Software.
;OSI Certified is a certification mark of the Open Source Initiative.

;This program is free software; you can redistribute it and/or
;modify it under the terms of the GNU General Public License
;as published by the Free Software Foundation; either version 2
;of the License, or (at your option) any later version.

;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.

;You should have received a copy of the GNU General Public License
;along with this program; if not, write to the Free Software
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

;EXCEPTION: The PortableApps.com Installer can be used with open source
;applications licensed under OSI-approved licenses as well as freeware provided
;it is unmodified and it adheres to the current PortableApps.com Format Specification
;as published at PortableApps.com/development. It may also be used with commercial
;software by contacting PortableApps.com.

!define PORTABLEAPPSINSTALLERVERSION "3.0.19.0"
!define PORTABLEAPPS.COMFORMATVERSION "3.0.19"

!if ${__FILE__} == "PortableApps.comInstallerPlugin.nsi"
	!include PortableApps.comInstallerPluginConfig.nsh
	!define PLUGININSTALLER
!else
	!include PortableApps.comInstallerConfig.nsh
!endif

# MANiFEST
!define RequestLevel USER
!define ResHacker    `${NSISDIR}\Contrib\Manifests\ResHacker.exe`
!define ManifDir     `${NSISDIR}\Contrib\Manifests`
!define Manifest     `NSIS_2.46_Win8`
!packhdr             `$%TEMP%\exehead.tmp` `"${Reshacker}" -addoverwrite "%TEMP%\exehead.tmp", "%TEMP%\exehead.tmp", "${ManifDir}\${Manifest}_${RequestLevel}.manifest", 24,1,1033`

# iNCLUDE (moved here so it's easier to see)
;!define Check64             ;=== check x64 minreq
;!define Include_x64.nsh
!define Include_WinVer.nsh
;!define Include_DotNetVer.nsh
;!define Include_GetBetween.nsh
;!define Include_ReplaceInFileWithTextReplace.nsh
;!define Include_LineWrite.nsh
;!define Include_ProcFunc.nsh
!define ExtractionOnly       ;=== enables $CMD variable
;!define COMMONFILESPLUGIN    ;=== set COMMONFILES plugin
;!define NON-SPECIFIC-PLUGIN  ;=== for Browser Plugins which support multiple apps.
;!define SHOWMAINSECTIONTITLE ;=== show MainSectionTitle as option
!define DISABLE_FINISH_RUN    ;=== uncomment for apps. which require Admin. Rights.

# TESTiNG
;!define NOCOMPRESSION

# DEFiNE
!define BUILD      0.0.0.0
!define APP        PowerISO
!define APP64      PowerISO64
!define FULLNAME   PowerISO
!define APPDIR     `$INSTDIR\App\${APP}`
!define APPDIR64   `$INSTDIR\App\${APP64}`
!define APPNAME    `${APP}Portable`
!define APPINFO    `$INSTDIR\App\AppInfo`
!define LAUNCHER   `${APPINFO}\Launcher`
!define DATA       `$INSTDIR\Data`
!define DEFDATA    `$INSTDIR\App\DefaultData`
!define DEFSET     `${DEFDATA}\settings`
!define SET        `$INSTDIR\Data\settings`
!define OTHER      `$INSTDIR\Other`
!define BRANDING   `Power Software Ltd`
!define EXE        `$INSTDIR\${APPNAME}.exe`
!define LEGAL      `Copyright (C) 2004-2015`
!define INI        `$INSTDIR\Backup_log.ini`
!define INS        `${APPINFO}\installer.ini`
;===
!define URL32      http://www.poweriso.com/BD92EC3F-82F9-887E-8857-FE98BC7E2755/PowerISO6-Full.exe
!define URL64      http://www.poweriso.com/BD92EC3F-82F9-887E-8857-FE98BC7E2755/PowerISO6-x64-Full.exe

# VARiABLE
!ifndef PLUGININSTALLER
	var ADDITIONALPARAMETERS
	var RUNLOCALLY
!endif

!ifdef ExtractionOnly
	var CMD
!endif
!ifdef Include_GetBetween.nsh
	var VERSION
!endif
!ifdef DownloadURL
	!define SETOUTDIR `$PLUGINSDIR\Downloaded`
!endif
!ifdef Check64
	!ifndef Include_x64.nsh
		!define Include_x64.nsh
	!endif
!endif

# SECTiON
!define MAINSECTIONIDX 0
!ifdef MAINSECTIONTITLE
	!define SECTION_ON ${SF_SELECTED}
	!include Sections.nsh
	!define FORCE_SELECTION    ;=== force user to make a selection
	;===
	!define OPTIONALSECTION1 1 ;=== define to enable
	!define OPTIONALSECTION2 2 ;=== define to enable
	!define OPTIONALSECTION3 3 ;=== define to enable
	!define OPTIONALSECTION4 4 ;=== define to enable
;	!define OPTIONALSECTION5 5 ;=== define to enable
;	!define OPTIONALSECTION6 6 ;=== define to enable
	;===
	!ifdef OPTIONALSECTION1
		var SECTION1
		!define DEFAULTSIZE           0
		!define OPTIONALSECTION1SIZE  6915
		!define OPTIONALSECTION1TITLE `${APP} x86 (32-bit)`
	!endif
	!ifdef OPTIONALSECTION2
		var SECTION2
		!define OPTIONALSECTION2SIZE  7464
		!define OPTIONALSECTION2TITLE `${APP} x64 (64-bit)`
	!endif
	!ifdef OPTIONALSECTION3
		var SECTION3
		!define OPTIONALSECTION3SIZE  0
		!define OPTIONALSECTION3TITLE `Shell Extension`
	!endif
	!ifdef OPTIONALSECTION4
		var SECTION4
		!define OPTIONALSECTION4SIZE  0
		!define OPTIONALSECTION4TITLE `Virtual Drive`
	!endif
	!ifdef OPTIONALSECTION5
		var SECTION5
		!define OPTIONALSECTION5SIZE  
		!define OPTIONALSECTION5TITLE ``
	!endif
	!ifdef OPTIONALSECTION6
		var SECTION6
		!define OPTIONALSECTION6SIZE  
		!define OPTIONALSECTION6TITLE ``
	!endif
!endif

# SPECiFiCATIONS
Name "${PORTABLEAPPNAME}" "${PORTABLEAPPNAMEDOUBLEDAMPERSANDS}"
OutFile "..\..\..\${FILENAME}.paf.exe"
!ifdef NON-SPECIFIC-PLUGIN
	!ifdef COMMONFILESPLUGIN
		InstallDir "\CommonFiles"
	!else
		InstallDir ""
	!endif
!else
	!ifdef COMMONFILESPLUGIN
		InstallDir "\CommonFiles\${APPID}"
	!else
		InstallDir "\${APPID}"
	!endif
!endif
Caption                          `${FULLNAME}`
VIProductVersion                 `${BUILD}`
VIAddVersionKey ProductName      `${FULLNAME}`
VIAddVersionKey Comments         http://portablewares.blogspot.com/
VIAddVersionKey CompanyName      `${BRANDING}`
VIAddVersionKey LegalCopyright   `${LEGAL}`
VIAddVersionKey FileDescription  `${FULLNAME}`
VIAddVersionKey FileVersion      `${BUILD}`
VIAddVersionKey ProductVersion   Portable
VIAddVersionKey InternalName     `${PORTABLEAPPNAME}`
;VIAddVersionKey LegalTrademarks  
VIAddVersionKey OriginalFilename `${FILENAME}.paf.exe`
VIAddVersionKey PortableApps.comInstallerVersion "${PORTABLEAPPSINSTALLERVERSION}"
VIAddVersionKey PortableApps.comFormatVersion "${PORTABLEAPPS.COMFORMATVERSION}"
VIAddVersionKey PortableApps.comAppID "${APPID}"
!ifdef DownloadURL ;advertise the needed bits to the PA.c Updater
	VIAddVersionKey PortableApps.comDownloadURL "${DownloadURL}"
	VIAddVersionKey PortableApps.comDownloadKnockURL "${DownloadKnockURL}"
	VIAddVersionKey PortableApps.comDownloadName "${DownloadName}"
	VIAddVersionKey PortableApps.comDownloadFileName "${DownloadFileName}"
	VIAddVersionKey PortableApps.comDownloadMD5 "${DownloadMD5}"
!endif

!ifdef NOCOMPRESSION
	SetCompress off
!else
	SetCompress Auto
	SetCompressor /SOLID lzma
	SetCompressorDictSize 32
	SetDatablockOptimize On
!endif

CRCCheck on
AutoCloseWindow True
RequestExecutionLevel user
AllowRootDirInstall true
;=== Include
!include MUI2.nsh
!include FileFunc.nsh
!include LogicLib.nsh
!ifdef PRESERVEFILE1
	!include PortableApps.comInstallerMoveFiles.nsh
!endif
!ifdef COPYLOCALFILES
	!include Registry.nsh
!endif
!include TextFunc.nsh
!include WordFunc.nsh
!include PortableApps.comInstallerDriveFreeSpaceCustom.nsh
!include PortableApps.comInstallerDumpLogToFile.nsh
!include PortableApps.comInstallerTBProgress.nsh

;=== CUSTOM iNCLUDES
!ifdef Include_x64.nsh
	!include x64.nsh
!endif
!ifdef Include_WinVer.nsh
	!include WinVer.nsh
!endif
!ifdef Include_GetBetween.nsh
	!include GetBetween.nsh
!endif
!ifdef Include_ReplaceInFileWithTextReplace.nsh
	!include ReplaceInFileWithTextReplace.nsh
!endif
!ifdef Include_LineWrite.nsh
	!include LineWrite.nsh
!endif
!ifdef Include_DotNetVer.nsh
	!include DotNetVer.nsh
!endif
!ifdef Include_ProcFunc.nsh
	!include ProcFunc.nsh
!endif
;=== Program Icon
Icon "PortableApps.comInstaller.ico"
!define MUI_ICON "PortableApps.comInstaller.ico"
!define MUI_UNICON "PortableApps.comInstaller.ico"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "PortableApps.comInstallerHeader.bmp"
!define MUI_HEADERIMAGE_BITMAP_RTL "PortableApps.comInstallerHeaderRTL.bmp"
!define MUI_HEADERIMAGE_RIGHT

;=== Icon & Stye ===
BrandingText "${LEGAL}"

;=== Pages
!ifdef COPYLOCALFILES
	!define MUI_CUSTOMFUNCTION_ABORT CustomAbortFunction
!endif
!define MUI_LANGDLL_WINDOWTITLE "${PORTABLEAPPNAME}"
!define MUI_LANGDLL_INFO "Please select language:"
!define MUI_WELCOMEFINISHPAGE_BITMAP "PortableApps.comInstaller.bmp"
!ifdef PLUGINNAME
	!define MUI_WELCOMEPAGE_TITLE "${PORTABLEAPPNAMEDOUBLEDAMPERSANDS}"
!else
	!define MUI_WELCOMEPAGE_TITLE "${PORTABLEAPPNAMEDOUBLEDAMPERSANDS}"
!endif
!define MUI_WELCOMEPAGE_TEXT "$(welcome)"
!define MUI_PAGE_CUSTOMFUNCTION_PRE PreWelcome
!define MUI_COMPONENTSPAGE_SMALLDESC
!insertmacro MUI_PAGE_WELCOME
!ifdef LICENSEAGREEMENT
	;!define MUI_LICENSEPAGE_CHECKBOX
	!define MUI_PAGE_CUSTOMFUNCTION_PRE PreLicense
	!define MUI_PAGE_CUSTOMFUNCTION_SHOW ShowLicense
	!define MUI_PAGE_CUSTOMFUNCTION_LEAVE LeaveLicense
	!insertmacro MUI_PAGE_LICENSE "..\..\App\AppInfo\${LICENSEAGREEMENT}"
!endif
!ifdef MAINSECTIONTITLE
	!define MUI_PAGE_CUSTOMFUNCTION_PRE PreComponents
	!insertmacro MUI_PAGE_COMPONENTS
!endif
!define MUI_DIRECTORYPAGE_VERIFYONLEAVE
!define MUI_PAGE_CUSTOMFUNCTION_PRE PreDirectory
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE LeaveDirectory
!insertmacro MUI_PAGE_DIRECTORY
!define MUI_PAGE_CUSTOMFUNCTION_SHOW ShowInstFiles
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_TEXT "$(finish)"
!define MUI_PAGE_CUSTOMFUNCTION_PRE PreFinish
!define MUI_FINISHPAGE_TITLE_3LINES
;!define MUI_FINISHPAGE_CANCEL_ENABLED ;Disabled due to bug in MUI2
!ifndef DISABLE_FINISH_RUN
	!ifndef PLUGINNAME
		!define MUI_FINISHPAGE_RUN_NOTCHECKED
		!define MUI_FINISHPAGE_RUN "$INSTDIR\${FINISHPAGERUN}"
	!endif
!endif
!insertmacro MUI_PAGE_FINISH

;=== Languages
!ifndef INSTALLERMULTILINGUAL
	!insertmacro MUI_LANGUAGE "${INSTALLERLANGUAGE}"
	!include PortableApps.comInstallerLanguages\${INSTALLERLANGUAGE}.nsh
!else
	!tempfile LangAutoDetectFile
	!macro IncludeLang _LANG_NAME
;		!ifdef USES_${_LANG_NAME}
			!insertmacro MUI_LANGUAGE "${_LANG_NAME}"
			!include PortableApps.comInstallerLanguages\${_LANG_NAME}.nsh
			!appendfile "${LangAutoDetectFile}" "${Case} ${LANG_${_LANG_NAME}}$\n"
;		!endif
	!macroend
	!define IncludeLang "!insertmacro IncludeLang"
	${IncludeLang} ARABIC
	${IncludeLang} ARMENIAN
	${IncludeLang} AZERBAIJANI
	${IncludeLang} BELARUSIAN
	${IncludeLang} BOSNIAN
	${IncludeLang} BULGARIAN
	${IncludeLang} CROATIAN
	${IncludeLang} CZECH
	${IncludeLang} DANISH
	${IncludeLang} DUTCH
	${IncludeLang} ENGLISH
	${IncludeLang} FARSI
	${IncludeLang} FINNISH
	${IncludeLang} FRENCH
	${IncludeLang} GERMAN
	${IncludeLang} GREEK
	${IncludeLang} HUNGARIAN
	${IncludeLang} INDONESIAN
	${IncludeLang} ITALIAN
	${IncludeLang} JAPANESE
	${IncludeLang} KAZAKH
	${IncludeLang} KOREAN
	${IncludeLang} LITHUANIAN
	${IncludeLang} MALAY
	${IncludeLang} NORWEGIAN
	${IncludeLang} POLISH
	${IncludeLang} PORTUGUESEBR
	${IncludeLang} ROMANIAN
	${IncludeLang} RUSSIAN
	${IncludeLang} SERBIAN
	${IncludeLang} SIMPCHINESE
	${IncludeLang} SLOVAK
	${IncludeLang} SLOVENIAN
	${IncludeLang} SPANISH
	${IncludeLang} SWEDISH
	${IncludeLang} THAI
	${IncludeLang} TRADCHINESE
	${IncludeLang} TURKISH
	${IncludeLang} UKRAINIAN
	${IncludeLang} URDU
	${IncludeLang} VIETNAMESE
	!insertmacro MUI_RESERVEFILE_LANGDLL
!endif

;=== Macros
!macro _ReDef _NAME _VAL
	!ifdef ${_NAME}
		!undef ${_NAME}
	!endif
	!define ${_NAME} `${_VAL}`
!macroend
!define ReDef "!insertmacro _ReDef "
Function GetAfterChar
	!macro _GetAfterChar _STR _CHAR _RET
		Push `${_STR}`
		Push `${_CHAR}`
		Call GetAfterChar
		Pop `${_RET}`
	!macroend
	!define GetAfterChar "!insertmacro _GetAfterChar"
	Exch $0
	Exch
	Exch $1
	Push $2
	Push $3
	StrCpy $2 0
	IntOp $2 $2 - 1
	StrCpy $3 $1 1 $2
	StrCmp $3 "" 0 +3
	StrCpy $0 ""
	Goto +5
	StrCmp $3 $0 +2
	Goto -6
	IntOp $2 $2 + 1
	StrCpy $0 $1 "" $2
	Pop $3
	Pop $2
	Pop $1
	Exch $0
FunctionEnd
Function RelGotoPage
	IntCmp $R9 0 0 Move Move
	StrCmp $R9 "X" 0 Move
	StrCpy $R9 "120"
	Move:
	SendMessage $HWNDPARENT "0x408" "$R9" ""
FunctionEnd
!define CABW         CabinetWClass
Function Close
	!macro _Close _CLASS _TITLE
		Push `${_TITLE}`
		Push `${_CLASS}`
		Call Close
	!macroend
	!define Close `!insertmacro _Close`
	Exch $R2
	Exch
	Exch $R1
	Push $R0
	Search:
		FindWindow $R0 `$R2` `$R1`
		IntCmp $R0 0 END 0 0
		IsWindow $R0 0 END
		System::Call `user32::PostMessage(i,i,i,i) i($R0,0x0010,0,0)`
		Sleep 100
		Goto Search
	END:
	Pop $R0
	Pop $R1
	Pop $R2
FunctionEnd
Function HasDotNETFramework
	!macro _HasDotNETFramework _a _b _t _f
		!insertmacro _LOGICLIB_TEMP
		Call HasDotNETFramework
		Pop $_LOGICLIB_TEMP
		!insertmacro _= $_LOGICLIB_TEMP 1 `${_t}` `${_f}`
	!macroend
	!define HasDotNETFramework `"" HasDotNETFramework ""`
	!define NET `SOFTWARE\Microsoft\.NETFramework`
	!define POL `SOFTWARE\Microsoft\.NETFramework\policy`
	Push $0
	Push $1
	Push $2
	Push $3
	Push $4
	ReadRegStr $4 HKLM `${NET}` InstallRoot
	Push $4
	Exch $EXEDIR
	Exch $EXEDIR
	Pop $4
	IfFileExists $4 0 +10
	StrCpy $0 0
	EnumRegKey $2 HKLM `${POL}` $0
	IntOp $0 $0 + 1
	StrCmp $2 "" +6
	StrCpy $1 0
	EnumRegValue $3 HKLM `${POL}\$2` $1
	IntOp $1 $1 + 1
	StrCmp $3 "" -6
	IfFileExists `$4\$2.$3` +3 -3
	StrCpy $0 0
	Goto +2
	StrCpy $0 1
	Pop $4
	Pop $3
	Pop $2
	Pop $1
	Exch $0
FunctionEnd
;Function CloseX
;	!macro _CloseX _PROCESS
;		Push `${_PROCESS}`
;		Call CloseX
;	!macroend
;	!define CloseX "!insertmacro _CloseX"
;	Exch $0
;	Push $1
;	Close:
;	${If} ${ProcessExists} `$0`
;		${CloseProcess} `$0` $1
;		${If} ${ProcessExists} `$0`
;			${TerminateProcess} `$0` $1
;			Sleep 1000
;			Goto Close
;		${EndIf}
;	${EndIf}
;	Pop $1
;	Pop $0
;FunctionEnd
!macro !insertmacro1-10 _m
!insertmacro ${_m} 1
!insertmacro ${_m} 2
!insertmacro ${_m} 3
!insertmacro ${_m} 4
!insertmacro ${_m} 5
!insertmacro ${_m} 6
!insertmacro ${_m} 7
!insertmacro ${_m} 8
!insertmacro ${_m} 9
!insertmacro ${_m} 10
!macroend
!define !insertmacro1-10 "!insertmacro !insertmacro1-10"

;=== Variables
Var FOUNDPORTABLEAPPSPATH
!ifdef MAINSECTIONTITLE
;	Var OPTIONAL1DONE
!endif
Var AUTOMATEDINSTALL
Var AUTOCLOSE
Var SILENTLANGUAGEMODE
Var HIDEINSTALLER
Var MINIMIZEINSTALLER
!ifdef LICENSEAGREEMENT
	Var EULAVERSIONMATCH
!endif
!ifdef COPYLOCALFILES
	Var CopyLocalFilesFrom
	Var CopyLocalFilesTo
	Var MISSINGFILEORPATH
!endif
!ifdef DOWNLOADURL
	Var MD5MISMATCH
	Var DOWNLOADRESULT
	Var DOWNLOADEDFILE
	Var DOWNLOADALREADYEXISTED
	Var SECONDDOWNLOADATTEMPT
	Var DownloadURLActual
!endif
Var INTERNALEULAVERSION
Var InstallingStatusString
Var bolAppUpgrade
Var bolLogFile
Var PAcLocaleID
Var strLastDirectory
Var strTimeStore

;=== Custom Code
!ifdef USESCUSTOMCODE
	!if ${__FILE__} == "PortableApps.comInstallerPlugin.nsi"
		!include PortableApps.comInstallerPluginCustom.nsh
	!else
		!include PortableApps.comInstallerCustom.nsh
	!endif
!endif

!ifdef INSTALLERMULTILINGUAL
	!macro CaseLang _LANG_NAME _LANG_ID
		!ifdef USES_${_LANG_NAME}
			${Case} ${_LANG_ID}
		!endif
	!macroend
	!define CaseLang "!insertmacro CaseLang"
!endif

Function .onInit
	!ifdef OPTIONALSECTION1SIZE
		SectionSetSize 1 ${OPTIONALSECTION1SIZE}
	!endif
	!ifdef OPTIONALSECTION2SIZE
		SectionSetSize 2 ${OPTIONALSECTION2SIZE}
	!endif
	!ifdef OPTIONALSECTION3SIZE
		SectionSetSize 3 ${OPTIONALSECTION3SIZE}
	!endif
	!ifdef OPTIONALSECTION4SIZE
		SectionSetSize 4 ${OPTIONALSECTION4SIZE}
	!endif
	!ifdef OPTIONALSECTION5SIZE
		SectionSetSize 5 ${OPTIONALSECTION5SIZE}
	!endif
	!ifdef OPTIONALSECTION6SIZE
		SectionSetSize 6 ${OPTIONALSECTION6SIZE}
	!endif

	!ifdef ExtractionOnly
		${GetParameters} $R0
		ClearErrors
		${GetOptions} $R0 -x $R1
		IfErrors +2
		StrCpy $CMD 1
		ClearErrors
		${GetOptions} $R0 /x $R1
		IfErrors +2
		StrCpy $CMD 1
	!endif

	SetSilent normal

	!ifdef DownloadURL
	StrCpy $R0 $EXEFILE "" -15
	${If} $R0 != "_online.paf.exe"
	${AndIf} $R0 != "line.paf[1].exe" ;Handle IE's renaming of files when run directly from a download
	${AndIf} $R0 != "line.paf[2].exe"
	${AndIf} $R0 != "line.paf[3].exe"
	${AndIf} $R0 != "line.paf[4].exe"
	${AndIf} $R0 != "line.paf[5].exe"
	${AndIf} $R0 != "line.paf[6].exe"
	${AndIf} $R0 != "line.paf[7].exe"
	${AndIf} $R0 != "line.paf[8].exe"
	${AndIf} $R0 != "line.paf[9].exe"
		MessageBox MB_OK|MB_ICONSTOP `PortableApps.com Installers that download files must end with "_online.paf.exe".  This is to ensure that users always know that an installer downloads files before it is run.  Please rename the file to end in _online.paf.exe before running.`
		Abort
	${EndIf}
	!endif

	InitPluginsDir

	!ifdef INSTALLERMULTILINGUAL
		ReadEnvStr $PAcLocaleID "PortableApps.comLocaleID"
		${Switch} $PAcLocaleID
			; Use the Case statements formed earlier.
			!include "${LangAutoDetectFile}"
			!delfile "${LangAutoDetectFile}"
			!undef LangAutoDetectFile
				StrCpy $LANGUAGE $PAcLocaleID
				${Break}
			${Default}
				${GetOptions} $CMDLINE "/DESTINATION=" $0
				${IfNot} ${Errors}
				${AndIf} ${FileExists} `$0\PortableApps.com\PortableAppsPlatform.exe`
					;Automated platform install but doesn't support the exact language
					
					;Language Fallbacks, if none, then English
					${If} $PAcLocaleID == 3082 ;SpanishInternational
					${AndIf} ${USES_SPANISH} == "true"
						StrCpy $LANGUAGE 1034 ;Spanish
					${Else}
						${If} $PAcLocaleID == 1034 ;Spanish
						${AndIf} ${USES_SPANISHINTERNATIONAL} == "true"
							StrCpy $LANGUAGE 3082 ;SpanishInternational
						${Else}
							${If} $PAcLocaleID == 1046 ;PortugueseBR
							${AndIf} ${USES_PORTUGUESE} == "true"
								StrCpy $LANGUAGE 2070 ;Portuguese
							${Else}
								${If} $PAcLocaleID == 2070 ;Portuguese
								${AndIf} ${USES_PORTUGUESEBR} == "true"
									StrCpy $LANGUAGE 1046 ;PortugueseBR
								${Else}
									StrCpy $LANGUAGE 1033 ;English as last fallback
								${EndIf}
							${EndIf}
						${EndIf}
					${EndIf}
				${Else}
					!insertmacro MUI_LANGDLL_DISPLAY
				${EndIf}
		${EndSwitch}
	!endif

	;=== Check for logging mode
	${GetOptions} $CMDLINE "/LOG=" $0
	
	${IfNot} ${Errors}
	${AndIf} $0 == "true"
		StrCpy $bolLogFile true
	${Else}
		ClearErrors
	${EndIf}
	
	;=== Check for a specified installation directory
	${GetOptions} $CMDLINE "/DESTINATION=" $0

	${IfNot} ${Errors}
		!ifdef COMMONFILESPLUGIN
			StrCpy $INSTDIR "$0CommonFiles\${APPID}"
		!else
			${GetOptions} $CMDLINE "/COPYNUMBER=" $1
			${IfNot} ${Errors}
				!ifdef NON-SPECIFIC-PLUGIN
					StrCpy $INSTDIR "$0_Copy_$1"
				!else
					StrCpy $INSTDIR "$0${APPID}_Copy_$1"
				!endif
			${Else}
				!ifdef NON-SPECIFIC-PLUGIN
					StrCpy $INSTDIR "$0"
				!else
					StrCpy $INSTDIR "$0${APPID}"
				!endif
			${EndIf}
		!endif

		!ifdef LICENSEAGREEMENT
			!ifndef EULAVERSION
				StrCpy $INTERNALEULAVERSION 1
			!else
				StrCpy $INTERNALEULAVERSION ${EULAVERSION}
			!endif
			${If} ${FileExists} "$INSTDIR\Data\PortableApps.comInstaller\license.ini"
				ReadINIStr $0 "$INSTDIR\Data\PortableApps.comInstaller\license.ini" "PortableApps.comInstaller" "EULAVersion"
				ClearErrors
				${If} $0 == $INTERNALEULAVERSION
					StrCpy $EULAVERSIONMATCH "true"
				${EndIf}
			${EndIf}
		!endif

		;=== Check for PortableApps.com Platform
		${GetParent} $INSTDIR $0
		!ifdef COMMONFILESPLUGIN
			${GetParent} $0 $0
		!endif

		;=== Check that it exists at the right location
		DetailPrint '$(checkforplatform)'

		${If} ${FileExists} `$0\PortableApps.com\PortableAppsPlatform.exe`
			;=== Check that it's the real deal
			MoreInfo::GetProductName `$0\PortableApps.com\PortableAppsPlatform.exe`
			Pop $1
			${If} $1 == "PortableApps.com Platform"
				MoreInfo::GetCompanyName `$0\PortableApps.com\PortableAppsPlatform.exe`
				Pop $1
				${If} $1 == "PortableApps.com"
					;=== Check that it's running
					FindProcDLL::FindProc "PortableAppsPlatform.exe"
					${If} $R0 == 1
						;=== Do a partially automated install
						StrCpy $AUTOMATEDINSTALL "true"

						ClearErrors
						${GetOptions} $CMDLINE "/AUTOCLOSE=" $R0
						${IfNot} ${Errors}
						${AndIf} $R0 == "true"
							StrCpy $AUTOCLOSE "true"
						${EndIf}

						ClearErrors
						${GetOptions} $CMDLINE "/HIDEINSTALLER=" $R0
						${IfNot} ${Errors}
						${AndIf} $R0 == "true"
							StrCpy $HIDEINSTALLER "true"
						${EndIf}

						ClearErrors
						${GetOptions} $CMDLINE "/MINIMIZEINSTALLER=" $R0
						${IfNot} ${Errors}
						${AndIf} $R0 == "true"
							StrCpy $MINIMIZEINSTALLER "true"
						${EndIf}

						ClearErrors
						${GetOptions} $CMDLINE "/SILENT=" $R0
						${IfNot} ${Errors}
						${AndIf} $R0 == "true"
							;Duplicate of the size calculation code, to be functionalized later
							SectionGetSize ${MAINSECTIONIDX} $1 ;=== Space Required for App
							
							!ifdef MAINSECTIONTITLE
								SectionGetFlags ${OPTIONALSECTIONIDX} $9
								IntOp $9 $9 & ${SF_SELECTED}
								${If} $9 >= ${SF_SELECTED}
									SectionGetSize ${OPTIONALSECTIONIDX} $2 ;=== Space Required for App
									IntOp $1 $1 + $2
								${EndIf}
							!endif
							${GetRoot} $INSTDIR $2
							;${DriveSpace} `$2\` "/D=F /S=M" $3 ;=== Space Free on Device
							${DriveFreeSpaceCustom} "$2\" $3
							
							;Convert app size to MB from KB
							IntOp $1 $1 / 1024
							
							${If} $1 == 0
							;If less than 1MB, round to 1MB
								StrCpy $1 1
							${EndIf}
							
							${If} $3 <= $1
								IntOp $1 $1 * 1024
								IntOp $3 $3 * 1024
								!ifndef PLUGININSTALLER ;=== If not a plugin installer, add the current install size to free space
									${If} ${FileExists} $INSTDIR
										${GetSize} `$INSTDIR` "/M=*.* /S=0K /G=0" $4 $5 $6 ;=== Current installation size
										IntOp $3 $3 + $4 ;=== Space Free + Current Root Install Size
										${GetSize} `$INSTDIR\App` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
										IntOp $3 $3 + $4 ;=== Space Free + Current App Install Size
										${GetSize} `$INSTDIR\Other` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
										IntOp $3 $3 + $4 ;=== Space Free + Current Other Install Size

										${If} `${ADDONSDIRECTORYPRESERVE}` != "NONE"
										${AndIf} ${FileExists} `$INSTDIR\${ADDONSDIRECTORYPRESERVE}`
												${GetSize} `$INSTDIR\${ADDONSDIRECTORYPRESERVE}` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Size of Data directory
												IntOp $3 $3 - $4 ;=== Remove the plugins directory from the free space calculation
										${EndIf}
									${EndIf}
								!else
									!ifdef COMMONFILESPLUGIN ;Duplicate code for now, to do above for CommonFiles as well
										${If} ${FileExists} $INSTDIR
											${GetSize} `$INSTDIR` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
											IntOp $3 $3 + $4 ;=== Space Free + Current Install Size
										${EndIf}
									!endif
								!endif
								${If} $3 <= $1
									MessageBox MB_OK|MB_ICONEXCLAMATION "$(notenoughspace)"
									Abort
								${EndIf}
							${EndIf}

							!ifdef LICENSEAGREEMENT
								${If} $EULAVERSIONMATCH == "true"
									SetSilent silent
								${EndIf}
							!else
								SetSilent silent
							!endif
						${EndIf}

						ClearErrors
						${GetOptions} $CMDLINE "/SILENTLANGUAGEMODE=" $R0
						${IfNot} ${Errors}
							${If} $R0 == "auto"
							${OrIf} $R0 == "never"
							${OrIf} $R0 == "always"
								StrCpy $SILENTLANGUAGEMODE $R0
							${Else}
								StrCpy $SILENTLANGUAGEMODE "auto"
							${EndIf}
						${Else}
							StrCpy $SILENTLANGUAGEMODE "auto"
						${EndIf}

					${EndIf}
				${EndIf}
			${EndIf}
		${EndIf}
	${Else}
		ClearErrors
		;=== Check legacy location
		${GetOptions} $CMDLINE "-o" $R0
		${IfNot} ${Errors}
			!ifdef COMMONFILESPLUGIN
				!ifdef NON-SPECIFIC-PLUGIN
					StrCpy $INSTDIR "$R0CommonFiles"
				!else
					StrCpy $INSTDIR "$R0CommonFiles\${APPID}"
				!endif
			!else
				!ifdef NON-SPECIFIC-PLUGIN
					StrCpy $INSTDIR "$R0"
				!else
					StrCpy $INSTDIR "$R0${APPID}"
				!endif
			!endif
		${Else}
			;=== No installation directory found
			ClearErrors
			${If} ${FileExists}	"$PROFILE\PortableApps\*.*"
				StrCpy $FOUNDPORTABLEAPPSPATH "$Profile\PortableApps"
			${Else}
				${GetDrives} "HDD+FDD" GetDrivesCallBack
			${EndIf}
			${If} $FOUNDPORTABLEAPPSPATH != ""
				!ifdef COMMONFILESPLUGIN
					!ifdef NON-SPECIFIC-PLUGIN
						StrCpy $INSTDIR "$FOUNDPORTABLEAPPSPATH\CommonFiles"
					!else
						StrCpy $INSTDIR "$FOUNDPORTABLEAPPSPATH\CommonFiles\${APPID}"
					!endif
				!else
					!ifdef NON-SPECIFIC-PLUGIN
						StrCpy $INSTDIR "$FOUNDPORTABLEAPPSPATH"
					!else
						StrCpy $INSTDIR "$FOUNDPORTABLEAPPSPATH\${APPID}"
					!endif
				!endif
			${Else}
				;If within Program Files, TEMP or IE Cache, no default install path
				${WordFind} "$EXEDIR\" "$PROGRAMFILES\" "*" $R0
				${WordFind} "$EXEDIR\" "$PROGRAMFILES64\" "*" $R1
				${WordFind} "$EXEDIR\" "$INTERNET_CACHE\" "*" $R2
				${WordFind} "$EXEDIR\" "$TEMP\" "*" $R3
					
				${If} $R0 > 0
				${OrIf} $R1 > 0
				${OrIf} $R2 > 0
				${OrIf} $R3 > 0
					StrCpy $INSTDIR ""
				${Else}
					!ifdef COMMONFILESPLUGIN
						!ifdef NON-SPECIFIC-PLUGIN
							StrCpy $INSTDIR "$EXEDIR\CommonFiles"
						!else
							StrCpy $INSTDIR "$EXEDIR\CommonFiles\${APPID}"
						!endif
					!else
						!ifdef NON-SPECIFIC-PLUGIN
							StrCpy $INSTDIR "$EXEDIR"
						!else
							StrCpy $INSTDIR "$EXEDIR\${APPID}"
						!endif
					!endif
				${EndIf}
			${EndIf}
		${EndIf}
	${EndIf}

;	!ifdef MAINSECTIONTITLE
;		!ifdef OPTIONALSECTIONPRESELECTEDIFNONENGLISHINSTALL
			;=== If it's not English, select the optional component (languages) by default
;			${IfThen} $LANGUAGE != 1033 ${|} SectionSetFlags 1 ${OPTIONALSECTIONIDX} ${|}
;		!endif
;		${If} ${Silent}
;			${If} "${OPTIONALSECTIONINSTALLEDWHENSILENT}" == "true"
;				SectionSetFlags 1 ${OPTIONALSECTIONIDX}
;			${ElseIf} "${OptionalSectionSelectedInstallType}" == "Multilingual"
;				${If} $SILENTLANGUAGEMODE != "never"
;					${If} $SILENTLANGUAGEMODE == "always"
;						SectionSetFlags 1 ${OPTIONALSECTIONIDX}
;					${Else}
;						${IfThen} $LANGUAGE != 1033 ${|} SectionSetFlags 1 ${OPTIONALSECTIONIDX} ${|}
;					${EndIf}
;				${EndIf}
;			${EndIf}
;		${EndIf}
;	!endif

	!ifdef COPYLOCALFILES
		StrCpy $CopyLocalFilesFrom ""

		${If} "${CopyFromRegPath}" != ""
			${registry::Read} "${CopyFromRegPath}" "${CopyFromRegKey}" $R0 $R1
			${If} $R0 != ""
				;Strip trailing slash if there
				StrCpy $1 $R0 "" -1
				${If} $1 == "\"
					StrCpy $R0 $R0 -1
				${EndIf}

				;Go up directories if needed
				${If} "${CopyFromRegRemoveDirectories}" != ""
					StrCpy $1 1
					${Do}
						${GetParent} $R0 $R0
						IntOp $1 $1 + 1
					${LoopUntil} $1 > "${CopyFromRegRemoveDirectories}"
				${EndIf}

				;Check for existence
				${If} ${FileExists} "$R0\*.*"
					StrCpy $CopyLocalFilesFrom $R0
				${EndIf}
			${EndIf}
		${EndIf}

		;Fallback to direct entry
		${If} $CopyLocalFilesFrom == ""
		${AndIf} "${CopyFromDirectory}" != ""
			StrCpy $CopyLocalFilesFrom "${CopyFromDirectory}"
			${WordReplace} $CopyLocalFilesFrom "%PROGRAMFILES%" $PROGRAMFILES + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%PROGRAMFILES32%" $PROGRAMFILES32 + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%PROGRAMFILES64%" $PROGRAMFILES64 + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%COMMONFILES%" $COMMONFILES + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%COMMONFILES32%" $COMMONFILES32 + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%COMMONFILES64%" $COMMONFILES64 + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%DESKTOP%" $DESKTOP + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%WINDIR%" $WINDIR + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%SYSDIR%" $SYSDIR + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%APPDATA%" $APPDATA + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%LOCALAPPDATA%" $LOCALAPPDATA + $CopyLocalFilesFrom
			${WordReplace} $CopyLocalFilesFrom "%TEMP%" $TEMP + $CopyLocalFilesFrom
		${EndIf}
		${If} ${FileExists} "$CopyLocalFilesFrom\*.*"
			SectionGetSize ${MAINSECTIONIDX} $0
			${GetSize} $CopyLocalFilesFrom "/M=*.* /S=0K /G=1" $1 $2 $3
			IntOp $0 $0 + $1
			SectionSetSize ${MAINSECTIONIDX} $0
		${EndIf}
	!endif
	!ifdef AdditionalInstallSize
		SectionGetSize ${MAINSECTIONIDX} $0
		IntOp $0 $0 + ${AdditionalInstallSize}
		SectionSetSize ${MAINSECTIONIDX} $0
	!endif
	!ifdef DEFAULTSIZE
		SectionSetSize ${MAINSECTIONIDX} `${DEFAULTSIZE}`
	!endif
	${If} "${CHECKRUNNING}" != "NONE"
		;=== Check if app is running?
		RunningTryAgain:
		FindProcDLL::FindProc "${CHECKRUNNING}"
		${If} $R0 == 1
			MessageBox MB_OKCANCEL|MB_ICONINFORMATION $(runwarning) IDOK RunningTryAgain IDCANCEL RunningCancel
			
			RunningCancel:
				Abort
		${EndIf}
	${EndIf}
FunctionEnd

Function PreWelcome
	${IfThen} $AUTOMATEDINSTALL == "true" ${|} Abort ${|}
FunctionEnd

!ifdef LICENSEAGREEMENT
Function PreLicense
	${If} $AUTOMATEDINSTALL == "true"
	${AndIf} $EULAVERSIONMATCH == "true"
		Abort
	${EndIf}

	!ifndef EULAVERSION
		StrCpy $INTERNALEULAVERSION "1"
	!else
		StrCpy $INTERNALEULAVERSION "${EULAVERSION}"
	!endif
	${If} ${FileExists} "$INSTDIR\Data\PortableApps.comInstaller\license.ini"
		ReadINIStr $0 "$INSTDIR\Data\PortableApps.comInstaller\license.ini" "PortableApps.comInstaller" "EULAVersion"
		ClearErrors
		${If} $0 == $INTERNALEULAVERSION
		${AndIf} $AUTOMATEDINSTALL == "true"
			Abort
		${EndIf}
	${EndIf}
FunctionEnd
Function ShowLicense
	${If} $AUTOMATEDINSTALL == "true"
		${TBProgress} 20
		${TBProgress_State} Paused
	${EndIf}
FunctionEnd
Function LeaveLicense
	${If} $AUTOMATEDINSTALL == "true"
		${TBProgress_State} NoProgress
	${EndIf}
FunctionEnd
!endif

	Function ShowInstFiles
	!ifdef Check64
		${IfNot} ${RunningX64}
			MessageBox MB_OKCANCEL|MB_ICONINFORMATION|MB_TOPMOST `$(64)` IDOK +3 IDCANCEL 0
			Call .onInstFailed
			Quit
		${EndIf}
	!endif
;	${IfNot} ${HasDotNet2.0}
;	${AndIfNot} ${HasDotNet4.0}
;		StrCpy $0 `Microsoft .NET Framework 2.0 or 4.0`
;		MessageBox MB_YESNO|MB_ICONQUESTION|MB_TOPMOST `$(MINREQ)` IDYES +3 IDNO 0
;		Call .onInstFailed
;		Quit
;	${EndIf}
;	${IfNot} ${HasDotNet4.0}
;		EnumRegKey $0 HKLM `SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full` 0
;		StrCmp $0 "" +4
;		ReadRegDWORD $0 HKLM `SOFTWARE\Microsoft\NET Framework Setup\NDP\$0` Version
;		StrCpy $0 $0 3
;		StrCmp $0 4.5 +5
;		StrCpy $0 `Microsoft .NET Framework 4.0`
;		MessageBox MB_YESNO|MB_ICONQUESTION|MB_TOPMOST `$(MINREQ)` IDYES +3 IDNO 0
;		Call .onInstFailed
;		Quit
;	${EndIf}
	!ifdef FORCE_SELECTION
		SectionGetFlags ${OPTIONALSECTION1} $0
		SectionGetFlags ${OPTIONALSECTION2} $1
		IntOp $0 $0 & ${SF_SELECTED}
		IntOp $1 $1 & ${SF_SELECTED}
		${If} $0 != ${SF_SELECTED}
		${AndIf} $1 != ${SF_SELECTED}
			MESSAGEBOX MB_ICONSTOP|MB_TOPMOST `$(SELECTION)`
			StrCpy $R9 -2
			Call RelGotoPage
			Abort
		${EndIf}
	!endif
	;=== messagebox already contains "or newer".
	${If} ${IsNT}
		${IfNot} ${AtLeastWinXP}
			StrCpy $0 `Windows XP`
			MessageBox MB_YESNO|MB_ICONQUESTION|MB_TOPMOST `$(MINREQ)` IDYES +3 IDNO 0
			Call .onInstFailed
			Quit
		${EndIf}
	${Else}
		StrCpy $0 `Windows XP`
		MessageBox MB_YESNO|MB_ICONQUESTION|MB_TOPMOST `$(MINREQ)` IDYES +3 IDNO 0
		Call .onInstFailed
		Quit
	${EndIf}
	${If} ${FileExists} `$INSTDIR\${APPNAME}.exe`
		MoreInfo::GetCompanyName `$INSTDIR\${APPNAME}.exe`
		Pop $0
		${Select} $0 
			${Case} `${BRANDING}`
			${CaseElse}
				MESSAGEBOX MB_OKCANCEL|MB_ICONINFORMATION|MB_TOPMOST `$(DEVELOPER)` IDOK +3 IDCANCEL 0
				Call .onInstFailed
				Quit
		${EndSelect}
	${EndIf}
	w7tbp::Start
	FunctionEnd

!ifdef MAINSECTIONTITLE
	Function PreComponents
		${If} $AUTOCLOSE != "true"
		${OrIfNot} ${FileExists} "$INSTDIR\App\AppInfo\appinfo.ini"
			Return
		${EndIf}

		ReadINIStr $0 "$INSTDIR\App\AppInfo\appinfo.ini" "Details" "InstallType"
		ClearErrors
		${If} $0 == "${OPTIONALSECTIONSELECTEDINSTALLTYPE}"
			SectionSetFlags 1 ${OPTIONALSECTIONIDX}
			Abort
		${EndIf}

		;=== Check not selected
		${If} $0 == "${OPTIONALSECTIONNOTSELECTEDINSTALLTYPE}"
			SectionSetFlags 0 ${OPTIONALSECTIONIDX}
			Abort
		${EndIf}
	FunctionEnd
!endif

Function PreDirectory
	${IfThen} $AUTOMATEDINSTALL != "true" ${|} Return ${|}

	SectionGetSize ${MAINSECTIONIDX} $1 ;=== Space Required for App
	!ifdef MAINSECTIONTITLE
		SectionGetFlags ${OPTIONALSECTIONIDX} $9
		IntOp $9 $9 & ${SF_SELECTED}
		${If} $9 >= ${SF_SELECTED}
			SectionGetSize ${OPTIONALSECTIONIDX} $2 ;=== Space Required for App
			IntOp $1 $1 + $2
		${EndIf}
	!endif
	${GetRoot} $INSTDIR $2
	;${DriveSpace} `$2\` "/D=F /S=M" $3 ;=== Space Free on Device
	${DriveFreeSpaceCustom} "$2\" $3
							
	IntOp $1 $1 / 1024

	${If} $3 <= $1
		IntOp $1 $1 * 1024
		IntOp $3 $3 * 1024

		!ifndef PLUGININSTALLER ;=== If not a plugin installer, add the current install size to free space
			${If} ${FileExists} $INSTDIR
				${GetSize} $INSTDIR "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
				IntOp $3 $3 + $4 ;=== Space Free + Current Install Size

				${If} ${FileExists} `$INSTDIR\Data`
					${GetSize} `$INSTDIR\Data` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Size of Data directory
					IntOp $3 $3 - $4 ;=== Remove the data directory from the free space calculation
				${EndIf}

				${If} `${ADDONSDIRECTORYPRESERVE}` != "NONE"
				${AndIf} ${FileExists} `$INSTDIR\${ADDONSDIRECTORYPRESERVE}`
						${GetSize} `$INSTDIR\${ADDONSDIRECTORYPRESERVE}` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Size of Data directory
						IntOp $3 $3 - $4 ;=== Remove the plugins directory from the free space calculation
				${EndIf}
			${EndIf}
		!else
			!ifdef COMMONFILESPLUGIN ;Duplicate code for now, to do above for CommonFiles as well
				${If} ${FileExists} $INSTDIR
					${GetSize} `$INSTDIR` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
					IntOp $3 $3 + $4 ;=== Space Free + Current Install Size
				${EndIf}
			!endif
		!endif

		${If} $3 <= $1
			MessageBox MB_OK|MB_ICONEXCLAMATION "$(notenoughspace)"
			Return
		${EndIf}
	${EndIf}

	;=== Check if app is running?
	${IfThen} "${CHECKRUNNING}" == "NONE" ${|} Abort ${|}
	FindProcDLL::FindProc "${CHECKRUNNING}"
	${IfThen} $R0 != "1" ${|} Abort ${|}
	MessageBox MB_OK|MB_ICONINFORMATION $(runwarning)
FunctionEnd

Function LeaveDirectory
	;=== Prevent destination string changes without user verification
	${GetTime} "" "LS" $0 $1 $2 $3 $4 $5 $6
	${If} $strTimeStore == "$0 $1 $2 $3 $4 $5 $6"
		${GetParent} $INSTDIR $0
		${GetParent} $0 $0
		StrCpy $1 $0 3 -6
		StrCpy $2 $0 1 -2
		${If} $2 == "e"
		${AndIf} $1 == "ber"
			Abort
		${EndIf}
	${EndIf}

	GetInstDirError $0

	;=== Does it already exist? (upgrade)
	${If} ${FileExists} $INSTDIR
	${AndIf} "${CHECKRUNNING}" != "NONE"
		!ifdef NON-SPECIFIC-PLUGIN
			!ifndef COMMONFILESPLUGIN
				${GetAfterChar} `$INSTDIR` \ `$0`
				${If} $0 == ChromePortable
					${ReDef} APPID $0
					CHROME:
					${If} ${ProcessExists} chrome.exe
					${AndIf} ${ProcessExists} ChromePortable.exe
						MessageBox MB_OKCANCEL|MB_ICONSTOP|MB_TOPMOST `$(CLOSE)` IDOK CHROME IDCANCEL 0
						Abort
					${EndIf}
				${ElseIf} $0 == ChromiumPortable
					${ReDef} APPID $0
					CHROMIUM:
					${If} ${ProcessExists} chrome.exe
					${AndIf} ${ProcessExists} ChromiumPortable.exe
						MessageBox MB_OKCANCEL|MB_ICONSTOP|MB_TOPMOST `$(CLOSE)` IDOK CHROMIUM IDCANCEL 0
						Abort
					${EndIf}
				${ElseIf} $0 == IronPortable
					${ReDef} APPID $0
					IRON:
					${If} ${ProcessExists} chrome.exe
					${AndIf} ${ProcessExists} IronPortable.exe
						MessageBox MB_OKCANCEL|MB_ICONSTOP|MB_TOPMOST `$(CLOSE)` IDOK IRON IDCANCEL 0
						Abort
					${EndIf}
				${EndIf}
			!endif
		!else
			FindProcDLL::FindProc "${CHECKRUNNING}"
			${If} $R0 = 1
				MessageBox MB_OK|MB_ICONINFORMATION $(runwarning)
				Abort
			${EndIf}
		!endif
	${EndIf}

	; 0 is valid, enough space, all fine
	${Select} $0
		${Case} 1
			MessageBox MB_OK|MB_ICONINFORMATION $(invaliddirectory)
			Abort

		${Case} 2
			${IfNot} ${FileExists} $INSTDIR ;=== Is upgrade
				MessageBox MB_OK|MB_ICONEXCLAMATION $(notenoughspace)
				Abort
			${EndIf}

			SectionGetSize ${MAINSECTIONIDX} $1 ;=== Space Required for App
			!ifdef MAINSECTIONTITLE
					SectionGetFlags ${OPTIONALSECTIONIDX} $9
					IntOp $9 $9 & ${SF_SELECTED}
					${If} $9 >= ${SF_SELECTED}
						SectionGetSize ${OPTIONALSECTIONIDX} $2 ;=== Space Required for App
						IntOp $1 $1 + $2
					${EndIf}
			!endif
			${GetRoot} $INSTDIR $2
			;${DriveSpace} `$2\` "/D=F /S=K" $3 ;=== Space Free on Device
			${DriveFreeSpaceCustom} "$2\" $3

			
			!ifndef PLUGININSTALLER ;=== If not a plugin installer, add the current install size to free space
				${GetSize} `$INSTDIR` "/M=*.* /S=0K /G=0" $4 $5 $6 ;=== Current installation size
				IntOp $3 $3 + $4 ;=== Space Free + Current Root Install Size
				${GetSize} `$INSTDIR\App` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
				IntOp $3 $3 + $4 ;=== Space Free + Current App Install Size
				${GetSize} `$INSTDIR\Other` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
				IntOp $3 $3 + $4 ;=== Space Free + Current Other Install Size

				${If} `${ADDONSDIRECTORYPRESERVE}` != "NONE"
				${AndIf} ${FileExists} `$INSTDIR\${ADDONSDIRECTORYPRESERVE}`
						${GetSize} `$INSTDIR\${ADDONSDIRECTORYPRESERVE}` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Size of Data directory
						IntOp $3 $3 - $4 ;=== Remove the plugins directory from the free space calculation
				${EndIf}
			!else
				!ifdef COMMONFILESPLUGIN ;Duplicate code for now, to do above for CommonFiles as well
					${GetSize} `$INSTDIR` "/M=*.* /S=0K /G=1" $4 $5 $6 ;=== Current installation size
					IntOp $3 $3 + $4 ;=== Space Free + Current Install Size
				!endif
			!endif

			${If} $3 <= $1
				MessageBox MB_OK|MB_ICONEXCLAMATION "$(notenoughspace)"
				Abort
			${EndIf}
	${EndSelect}
	
	;Check for Program Files
	ReadEnvStr $0 IPromiseNotToComplainWhenPortableAppsDontWorkRightInProgramFiles
	${If} $0 != "I understand that this may not work and that I can not ask for help with any of my apps when operating in this fashion."
		${WordFind} "$INSTDIR\" "$PROGRAMFILES\" "*" $R0
		${If} $R0 > 0
			MessageBox MB_OK|MB_ICONINFORMATION "$(invaliddirectory) [$PROGRAMFILES or sub-directories]"
			Abort
		${EndIf}
		${WordFind} "$INSTDIR\" "$PROGRAMFILES64\" "*" $R0
		${If} $R0 > 0
			MessageBox MB_OK|MB_ICONINFORMATION "$(invaliddirectory) [$PROGRAMFILES64 or sub-directories]"
			Abort
		${EndIf}
	${EndIf}
FunctionEnd

Function .onVerifyInstDir
	${If} $INSTDIR != ""
	${AndIf} $strLastDirectory != ""
		StrLen $0 $INSTDIR
		StrLen $1 $strLastDirectory
		IntOp $2 $1 + 2
		IntOp $3 $1 - 2
		${If} $0 > $2
		${OrIf} $0 < $3
			${GetTime} "" "LS" $0 $1 $2 $3 $4 $5 $6
			StrCpy $strTimeStore "$0 $1 $2 $3 $4 $5 $6"
		${EndIf}	
	${EndIf}
	StrCpy $strLastDirectory $INSTDIR
FunctionEnd

!ifndef SC_CLOSE
!define SC_CLOSE 0xF060
!endif

Function PreFinish
	${IfThen} $AUTOCLOSE == "true" ${|} Abort ${|}
	;Fix for bug in MUI2 with  MUI_FINISHPAGE_CANCEL_ENABLED
	EnableWindow $mui.Button.Cancel 1
	System::Call 'USER32::GetSystemMenu(i $hwndparent,i0)i.s'
	System::Call 'USER32::EnableMenuItem(is,i${SC_CLOSE},i0)'
FunctionEnd

Function GetDrivesCallBack
	;=== Skip usual floppy letters
	${If} $8 == "FDD"
		${If} $9 == "A:\"
		${OrIf} $9 == "B:\"
			Push $0
			Return
		${EndIf}
	${EndIf}

	${If} ${FileExists} $9PortableApps
		StrCpy $FOUNDPORTABLEAPPSPATH $9PortableApps
	${EndIf}

	Push $0
FunctionEnd

!ifdef SHOWMAINSECTIONTITLE
	Section "${MAINSECTIONTITLE}"
!else
	Section "-${MAINSECTIONTITLE}"
!endif
	${If} $MINIMIZEINSTALLER == "true"
		ShowWindow $HWNDPARENT ${SW_MINIMIZE}
	${EndIf}
	${If} $HIDEINSTALLER == "true"
		ShowWindow $HWNDPARENT ${SW_HIDE}
	${EndIf}

	${If} ${FileExists} "$INSTDIR\*.*"
		StrCpy $bolAppUpgrade true
	${EndIf}

	${If} $(installingstatus) != ""
		StrCpy $InstallingStatusString "$(installingstatus)"
	${Else}
		StrCpy $InstallingStatusString "$(MUI_TEXT_INSTALLING_TITLE)"
	${EndIf}

	SectionIn RO
	SetOutPath $INSTDIR

	${If} $bolAppUpgrade == true
		${If} $(prepareupgrade) == ""
			DetailPrint $InstallingStatusString
		${Else}
			DetailPrint $(prepareupgrade)
		${EndIf}
	${Else}
		DetailPrint $InstallingStatusString
	${EndIf}
	SetDetailsPrint ListOnly

	##############
	# POST INSTALL
	##############
	${If} ${FileExists} `$INSTDIR\${APPNAME}.exe`
		MoreInfo::GetCompanyName `$INSTDIR\${APPNAME}.exe`
		Pop $0
		${Select} $0 
			${Case} `${BRANDING}`
			${CaseElse}
				RMDIR /r `${DATA}`
				Delete `$INSTDIR\*.*`
		${EndSelect}
	${EndIf}

	!ifndef PLUGININSTALLER
		Delete `$INSTDIR\*.*`
	!endif
	RMDir /r `${DATA}\PortableApps.comInstaller`

	!ifndef PLUGININSTALLER
		${If} $CMD != 1
			StrCpy $R9 1
			${Do}
				ClearErrors
				ReadINIStr $R8 `${INS}` FilesToPreserve PreserveFile$R9
				${IfThen} ${Errors} ${|} ${ExitDo} ${|}
				WriteINIStr `${INI}` FilesToPreserve PreserveFile$R9 `$R8`
				${If} ${FileExists} `$INSTDIR\$R8`
					${GetFileName} `$R8` `$R7`
					Rename `$INSTDIR\$R8` `~$R9$R7`
				${EndIf}
				IntOp $R9 $R9 + 1
			${Loop}
			StrCpy $R9 1
			${Do}
				ClearErrors
				ReadINIStr $R8 `${INS}` DirectoriesToPreserve PreserveDirectory$R9
				${IfThen} ${Errors} ${|} ${ExitDo} ${|}
				WriteINIStr `${INI}` DirectoriesToPreserve PreserveDirectory$R9 `$R8`
				${If} ${FileExists} `$INSTDIR\$R8`
					${GetAfterChar} `$R8` `\` `$R7`
					Rename `$INSTDIR\$R8` `$INSTDIR\~$R9$R7`
				${EndIf}
				IntOp $R9 $R9 + 1
			${Loop}
		${EndIf}
	!endif

	!ifdef OPTIONALSECTION1
		SectionGetFlags ${OPTIONALSECTION1} $0
		IntOp $0 $0 & ${SF_SELECTED}
		${If} $0 >= ${SF_SELECTED}
			StrCpy $SECTION1 1
		${EndIf}
	!endif
	!ifdef OPTIONALSECTION2
		SectionGetFlags ${OPTIONALSECTION2} $0
		IntOp $0 $0 & ${SF_SELECTED}
		${If} $0 >= ${SF_SELECTED}
			StrCpy $SECTION2 1
		${EndIf}
	!endif
	!ifdef OPTIONALSECTION3
		SectionGetFlags ${OPTIONALSECTION3} $0
		IntOp $0 $0 & ${SF_SELECTED}
		${If} $0 >= ${SF_SELECTED}
			StrCpy $SECTION3 1
		${EndIf}
	!endif
	!ifdef OPTIONALSECTION4
		SectionGetFlags ${OPTIONALSECTION4} $0
		IntOp $0 $0 & ${SF_SELECTED}
		${If} $0 >= ${SF_SELECTED}
			StrCpy $SECTION4 1
		${EndIf}
	!endif
	!ifdef OPTIONALSECTION5
		SectionGetFlags ${OPTIONALSECTION5} $0
		IntOp $0 $0 & ${SF_SELECTED}
		${If} $0 >= ${SF_SELECTED}
			StrCpy $SECTION5 1
		${EndIf}
	!endif
	!ifdef OPTIONALSECTION6
		SectionGetFlags ${OPTIONALSECTION6} $0
		IntOp $0 $0 & ${SF_SELECTED}
		${If} $0 >= ${SF_SELECTED}
			StrCpy $SECTION6 1
		${EndIf}
	!endif

	!ifndef PLUGININSTALLER
		${If} ${FileExists} `$INSTDIR\${APPNAME}.ini`
			${ConfigRead} `$INSTDIR\${APPNAME}.ini` AdditionalParameters= $ADDITIONALPARAMETERS
			${ConfigRead} `$INSTDIR\${APPNAME}.ini` RunLocally= $RUNLOCALLY
		${EndIf}
	!endif

	!ifdef DOWNLOADURL
		ClearErrors
		RMDir /r `${APPINFO}`
		${If} ${Errors}
			${Close} `${CABW}` ""
			RMDir /r `${APPINFO}`
		${EndIf}
		ClearErrors
		RMDir /r `${DEFDATA}`
		${If} ${Errors}
			${Close} `${CABW}` ""
			RMDir /r `${DEFDATA}`
		${EndIf}
		ClearErrors
		Delete `$INSTDIR\App\Readme.txt`
		${If} ${Errors}
			${Close} `${CABW}` ""
			Delete `$INSTDIR\App\Readme.txt`
		${EndIf}
		ClearErrors
		RMDir /r `${OTHER}`
		${If} ${Errors}
			${Close} `${CABW}` ""
			RMDir /r `${OTHER}`
		${EndIf}
	!else
		ClearErrors
		RMDir /r `$INSTDIR\App`
		${If} ${Errors}
			${Close} `${CABW}` ""
			RMDir /r `$INSTDIR\App`
		${EndIf}
		ClearErrors
		RMDir /r `${OTHER}`
		${If} ${Errors}
			${Close} `${CABW}` ""
			RMDir /r `${OTHER}`
		${EndIf}
	!endif

	${If} $bolAppUpgrade == true
		SetDetailsPrint both
		DetailPrint $InstallingStatusString
		SetDetailsPrint ListOnly
	${EndIf}

	!ifndef PLUGININSTALLER
		SetOutPath `$INSTDIR`
		File /x thumbs.db `..\..\*.exe`
		SetOutPath `$INSTDIR\App`
		File /r /x thumbs.db `..\..\App\*.*`
	!else ifdef COMMONFILESPLUGIN
		SetOutPath `$INSTDIR`
		File /r /x thumbs.db `..\..\App\*.*`
	!else ; non-CommonFiles plugin installer
		SetOutPath `${APPINFO}`
		File /x thumbs.db `..\..\App\AppInfo\plugininstaller.ini`
		StrCmp $SECTION1 1 0 +3
		SetOutPath `${DATA}\${APP}`
		File /x thumbs.db `..\..\App\${APP}\*.*`
		StrCmp $SECTION2 1 0 +3
		SetOutPath `${DATA}\${APP64}`
		File /x thumbs.db `..\..\App\${APP64}\*.*`
	!endif

	!ifdef DOWNLOADURL
		StrCmp $CMD 1 OFFLINE
		!define FILE1 ${APP}.exe
		!define FILE2 ${APP64}.exe
		SetOutPath `${SETOUTDIR}`
		File `${NSISDIR}\..\7zip\7z.dll`
		File `${NSISDIR}\..\7zip\7z.exe`
		${If} $Section1 = 1
			${If} ${FileExists} `$EXEDIR\${FILE1}`
				StrCpy $DOWNLOADEDFILE `$EXEDIR\${FILE1}`
			${Else}
				${ReDef} DownloadFilename `${FILE1}`
				${ReDef} DownloadName     `${OPTIONALSECTION1TITLE}`
				StrCpy $DownloadURLActual ${URL32}
				StrCpy $DOWNLOADEDFILE    `${SETOUTDIR}\${FILE1}`
				CreateDirectory `${SETOUTDIR}`
				DOWNLOAD:
				SetDetailsPrint both
				DetailPrint $(downloading)
				SetDetailsPrint none
				Delete "$DOWNLOADEDFILE"
				inetc::get /CONNECTTIMEOUT 30 /NOCOOKIES /TRANSLATE $(downloading) $(downloadconnecting) $(downloadsecond) $(downloadminute) $(downloadhour) $(downloadplural) "%dkB (%d%%) of %dkB @ %d.%01dkB/s" " (%d %s%s $(downloadremaining))" "$DownloadURLActual" "$DOWNLOADEDFILE" /END
				SetDetailsPrint both
				DetailPrint $InstallingStatusString
				SetDetailsPrint ListOnly
				Pop $DOWNLOADRESULT
				${If} $DOWNLOADRESULT != OK
					Delete "$DOWNLOADEDFILE"
					${If} $SECONDDOWNLOADATTEMPT != true
					${AndIf} $DOWNLOADRESULT != Cancelled
						StrCpy $SECONDDOWNLOADATTEMPT true
						Goto DOWNLOAD
					${EndIf}
					SetDetailsPrint textonly
					DetailPrint ""
					SetDetailsPrint listonly
					${TBProgress_State} Error
					MessageBox MB_ICONEXCLAMATION|MB_TOPMOST $(downloadfailed)
					DetailPrint $(downloadfailed)
					${TBProgress_State} NoProgress
					Goto SECTION2
				${EndIf}
			${EndIf}
			${If} ${FileExists} `$DOWNLOADEDFILE`
				ClearErrors
				RMDir /r `${APPDIR}`
				${If} ${Errors}
					${Close} `${CABW}` ""
					RMDir /r `${APPDIR}`
				${EndIf}
				CreateDirectory `${APPDIR}`
				ExecDos::Exec /TOSTACK `"${SETOUTDIR}\7z.exe" x "$DOWNLOADEDFILE" -o"${APPDIR}" -aot -y` "" ""
				Pop $0
				RMDir /r `${APPDIR}\$$PLUGINSDIR`
				DELETE `${APPDIR}\$$0`
				RENAME `${APPDIR}\$$0_1` `${DEFSET}\scdemu64.sys`
				DELETE `${APPDIR}\$$0_2`
				DELETE `${APPDIR}\$$0_3`
				DELETE `${APPDIR}\$$0_4`
				RENAME `${APPDIR}\$$SYSDIR\Drivers\scdemu.sys` `${DEFSET}\scdemu.sys`
				RMDIR /R `${APPDIR}\$$SYSDIR`
				RMDIR /R `${APPDIR}\$$COMMONFILES`
				DELETE `${APPDIR}\License.txt`
				DELETE `${APPDIR}\uninstall.exe.nsis`
				DELETE `${APPDIR}\unicows.dll`
				DELETE `${APPDIR}\setup64.exe`
				RENAME `${APPDIR}\PWRISOSH.DLL` `${APPDIR}\PWRISOSH64.DLL`
				RENAME `${APPDIR}\PWRISOSH_1.DLL` `${APPDIR}\PWRISOSH.DLL`
				ClearErrors
				${GetFileVersion} `${APPDIR}\${FILE1}` $0
				IfErrors +2
				WriteINIStr `${APPINFO}\appinfo.ini` Version PackageVersion $0
			${Else}
				SetDetailsPrint textonly
				DetailPrint ""
				SetDetailsPrint listonly
				${TBProgress_State} Error
				MessageBox MB_ICONEXCLAMATION|MB_TOPMOST $(downloadfailed)
				DetailPrint $(downloadfailed)
				${TBProgress_State} NoProgress
			${EndIf}
		${Else}
			ClearErrors
			RMDir /r `${APPDIR}`
			${If} ${Errors}
				${Close} `${CABW}` ""
				RMDir /r `${APPDIR}`
			${EndIf}
		${EndIf}
		SECTION2:
		${If} $Section2 = 1
			${If} ${FileExists} `$EXEDIR\${FILE2}`
				StrCpy $DOWNLOADEDFILE `$EXEDIR\${FILE2}`
			${Else}
				${ReDef} DownloadFilename `${FILE2}`
				${ReDef} DownloadName     `${OPTIONALSECTION2TITLE}`
				StrCpy $DownloadURLActual ${URL64}
				StrCpy $DOWNLOADEDFILE `${SETOUTDIR}\${FILE2}`
				CreateDirectory `${SETOUTDIR}`
				DOWNLOAD64:
				SetDetailsPrint both
				DetailPrint $(downloading)
				SetDetailsPrint none
				Delete "$DOWNLOADEDFILE"
				inetc::get /CONNECTTIMEOUT 30 /NOCOOKIES /TRANSLATE $(downloading) $(downloadconnecting) $(downloadsecond) $(downloadminute) $(downloadhour) $(downloadplural) "%dkB (%d%%) of %dkB @ %d.%01dkB/s" " (%d %s%s $(downloadremaining))" "$DownloadURLActual" "$DOWNLOADEDFILE" /END
				SetDetailsPrint both
				DetailPrint $InstallingStatusString
				SetDetailsPrint ListOnly
				Pop $DOWNLOADRESULT
				${If} $DOWNLOADRESULT != OK
					Delete "$DOWNLOADEDFILE"
					${If} $SECONDDOWNLOADATTEMPT != true
					${AndIf} $DOWNLOADRESULT != Cancelled
						StrCpy $SECONDDOWNLOADATTEMPT true
						Goto DOWNLOAD64
					${EndIf}
					SetDetailsPrint textonly
					DetailPrint ""
					SetDetailsPrint listonly
					${TBProgress_State} Error
					MessageBox MB_ICONEXCLAMATION|MB_TOPMOST $(downloadfailed)
					DetailPrint $(downloadfailed)
					${TBProgress_State} NoProgress
					Goto OFFLINE
				${EndIf}
			${EndIf}
			${If} ${FileExists} `$DOWNLOADEDFILE`
				ClearErrors
				RMDir /r `${APPDIR64}`
				${If} ${Errors}
					${Close} `${CABW}` ""
					RMDir /r `${APPDIR64}`
				${EndIf}
				CreateDirectory `${APPDIR64}`
				ExecDos::Exec /TOSTACK `"${SETOUTDIR}\7z.exe" x "$DOWNLOADEDFILE" -o"${APPDIR64}" -aot -y` "" ""
				Pop $0
				RMDir /r `${APPDIR64}\$$PLUGINSDIR`
				DELETE `${APPDIR64}\$$0`
				DELETE `${DEFSET}\scdemu64.sys`
				RENAME `${APPDIR64}\$$0_1` `${DEFSET}\scdemu64.sys`
				DELETE `${APPDIR64}\$$0_2`
				DELETE `${APPDIR64}\$$0_3`
				DELETE `${APPDIR64}\License.txt`
				DELETE `${APPDIR64}\setup64.exe`
				DELETE `${APPDIR64}\uninstall.exe.nsis`
				ClearErrors
				${GetFileVersion} `${APPDIR64}\${FILE2}` $0
				IfErrors +2
				WriteINIStr `${APPINFO}\appinfo.ini` Version PackageVersion $0
			${Else}
				SetDetailsPrint textonly
				DetailPrint ""
				SetDetailsPrint listonly
				${TBProgress_State} Error
				MessageBox MB_ICONEXCLAMATION|MB_TOPMOST $(downloadfailed)
				DetailPrint $(downloadfailed)
				${TBProgress_State} NoProgress
			${EndIf}
		${Else}
			ClearErrors
			RMDir /r `${APPDIR64}`
			${If} ${Errors}
				${Close} `${CABW}` ""
				RMDir /r `${APPDIR64}`
			${EndIf}
		${EndIf}
		OFFLINE:
		StrCmp $CMD 1 0 +5
		StrCmp $Section1 1 0 +2
		CreateDirectory `${APPDIR}`
		StrCmp $Section2 1 0 +2
		CreateDirectory `${APPDIR64}`
	!endif

	StrCmp $LANGUAGE 1025 0 +3
	StrCpy $0 dword:00000401
		Goto EndLNG
	StrCmp $LANGUAGE 1067 0 +3
	StrCpy $0 dword:0000042b
		Goto EndLNG
	StrCmp $LANGUAGE 1068 0 +3
	StrCpy $0 dword:0000042c
		Goto EndLNG
	StrCmp $LANGUAGE 1059 0 +3
	StrCpy $0 dword:00000423
		Goto EndLNG
	StrCmp $LANGUAGE 5146 0 +3
	StrCpy $0 dword:0000141a
		Goto EndLNG
	StrCmp $LANGUAGE 1026 0 +3
	StrCpy $0 dword:00000402
		Goto EndLNG
	StrCmp $LANGUAGE 1050 0 +3
	StrCpy $0 dword:0000041a
		Goto EndLNG
	StrCmp $LANGUAGE 1029 0 +3
	StrCpy $0 dword:00000405
		Goto EndLNG
	StrCmp $LANGUAGE 1030 0 +3
	StrCpy $0 dword:00000406
		Goto EndLNG
	StrCmp $LANGUAGE 1043 0 +3
	StrCpy $0 dword:00000413
		Goto EndLNG
	StrCmp $LANGUAGE 1033 0 +3
	StrCpy $0 dword:00000409
		Goto EndLNG
	StrCmp $LANGUAGE 1065 0 +3
	StrCpy $0 dword:00000429
		Goto EndLNG
	StrCmp $LANGUAGE 1035 0 +3
	StrCpy $0 dword:0000040b
		Goto EndLNG
	StrCmp $LANGUAGE 1036 0 +3
	StrCpy $0 dword:0000040c
		Goto EndLNG
	StrCmp $LANGUAGE 1031 0 +3
	StrCpy $0 dword:00000407
		Goto EndLNG
	StrCmp $LANGUAGE 1032 0 +3
	StrCpy $0 dword:00000408
		Goto EndLNG
	StrCmp $LANGUAGE 1038 0 +3
	StrCpy $0 dword:0000040e
		Goto EndLNG
	StrCmp $LANGUAGE 1057 0 +3
	StrCpy $0 dword:00000421
		Goto EndLNG
	StrCmp $LANGUAGE 1040 0 +3
	StrCpy $0 dword:00000410
		Goto EndLNG
	StrCmp $LANGUAGE 1041 0 +3
	StrCpy $0 dword:00000411
		Goto EndLNG
	StrCmp $LANGUAGE 1087 0 +3
	StrCpy $0 dword:0000043f
		Goto EndLNG
	StrCmp $LANGUAGE 1042 0 +3
	StrCpy $0 dword:00000412
		Goto EndLNG
	StrCmp $LANGUAGE 1063 0 +3
	StrCpy $0 dword:00000427
		Goto EndLNG
	StrCmp $LANGUAGE 1086 0 +3
	StrCpy $0 dword:0000043e
		Goto EndLNG
	StrCmp $LANGUAGE 1044 0 +3
	StrCpy $0 dword:00000414
		Goto EndLNG
	StrCmp $LANGUAGE 1045 0 +3
	StrCpy $0 dword:00000415
		Goto EndLNG
	StrCmp $LANGUAGE 1046 0 +3
	StrCpy $0 dword:00000416
		Goto EndLNG
	StrCmp $LANGUAGE 1048 0 +3
	StrCpy $0 dword:00000418
		Goto EndLNG
	StrCmp $LANGUAGE 1049 0 +3
	StrCpy $0 dword:00000419
		Goto EndLNG
	StrCmp $LANGUAGE 3098 0 +3
	StrCpy $0 dword:00000c1a
		Goto EndLNG
	StrCmp $LANGUAGE 2052 0 +3
	StrCpy $0 dword:00000804
		Goto EndLNG
	StrCmp $LANGUAGE 1051 0 +3
	StrCpy $0 dword:0000041b
		Goto EndLNG
	StrCmp $LANGUAGE 1060 0 +3
	StrCpy $0 dword:00000424
		Goto EndLNG
	StrCmp $LANGUAGE 1034 0 +3
	StrCpy $0 dword:00002c0a
		Goto EndLNG
	StrCmp $LANGUAGE 1053 0 +3
	StrCpy $0 dword:0000041d
		Goto EndLNG
	StrCmp $LANGUAGE 1054 0 +3
	StrCpy $0 dword:0000041e
		Goto EndLNG
	StrCmp $LANGUAGE 1028 0 +3
	StrCpy $0 dword:00000404
		Goto EndLNG
	StrCmp $LANGUAGE 1055 0 +3
	StrCpy $0 dword:0000041f
		Goto EndLNG
	StrCmp $LANGUAGE 1058 0 +3
	StrCpy $0 dword:00000422
		Goto EndLNG
	StrCmp $LANGUAGE 1056 0 +3
	StrCpy $0 dword:00000420
		Goto EndLNG
	StrCmp $LANGUAGE 1066 0 +3
	StrCpy $0 dword:0000042a
		Goto +2
	StrCpy $0 dword:00000409
	EndLNG:
	IfFileExists `${SET}\${APP}.reg` 0 +2
	WriteINIStr `${SET}\${APP}.reg` `HKEY_CURRENT_USER\Software\PowerISO` `"Language"` $0
	WriteINIStr `${DEFSET}\${APP}.reg` `HKEY_CURRENT_USER\Software\PowerISO` `"Language"` $0

	!ifndef PLUGININSTALLER
		SetOutPath `${OTHER}`
		File /x thumbs.db "..\..\Other\*.*"
		SetOutPath `$INSTDIR`
		File /x thumbs.db `..\..\Other\${APPNAME}.ini`
		${If} $ADDITIONALPARAMETERS != ""
			${ConfigWrite} `$INSTDIR\${APPNAME}.ini` AdditionalParameters= $ADDITIONALPARAMETERS $R0
		${EndIf}
		${If} $RUNLOCALLY != ""
			${ConfigWrite} `$INSTDIR\${APPNAME}.ini` RunLocally= $RUNLOCALLY $R0
		${EndIf}
		${If} $SECTION3 = 1
			${ConfigWrite} `$INSTDIR\${APPNAME}.ini` ShellExtension= true $R0
		${Else}
			${ConfigWrite} `$INSTDIR\${APPNAME}.ini` ShellExtension= false $R0
		${EndIf}
		${If} $SECTION4 = 1
			${ConfigWrite} `$INSTDIR\${APPNAME}.ini` VirtualDrive= true $R0
		${Else}
			${ConfigWrite} `$INSTDIR\${APPNAME}.ini` VirtualDrive= false $R0
		${EndIf}
	!endif

	!ifndef PLUGININSTALLER
		${If} $CMD != 1
			StrCpy $R9 1
			${Do}
				ClearErrors
				ReadINIStr $R8 `${INI}` FilesToPreserve PreserveFile$R9
				${IfThen} ${Errors} ${|} ${ExitDo} ${|}
				WriteINIStr `${INS}` FilesToPreserve PreserveFile$R9 $R8 ;=== preserve user's settings.
				${GetFileName} `$R8` `$R7`
				${If} ${FileExists} `$INSTDIR\~$R9$R7`
					Delete `$INSTDIR\$R8`
					Rename `$INSTDIR\~$R9$R7` `$INSTDIR\$R8`
				${EndIf}
				IntOp $R9 $R9 + 1
			${Loop}
			StrCpy $R9 1
			${Do}
				ClearErrors
				ReadINIStr $R8 `${INI}` DirectoriesToPreserve PreserveDirectory$R9
				${IfThen} ${Errors} ${|} ${ExitDo} ${|}
				WriteINIStr `${INS}` DirectoriesToPreserve PreserveDirectory$R9 $R8 ;=== preserve user's settings.
				${GetAfterChar} `$R8` `\` `$R7`
				${If} ${FileExists} `$INSTDIR\~$R9$R7`
					RMDir /r `$INSTDIR\$R8`
					Rename `$INSTDIR\~$R9$R7` `$INSTDIR\$R8`
				${EndIf}
				IntOp $R9 $R9 + 1
			${Loop}
			FindFirst $R0 $R1 `$INSTDIR\*`
				${DoWhile} $R1 != ""
				${If} $R1 != "."
				${AndIf} $R1 != ".."
					StrCpy $R2 $R1 1
					StrCmp $R2 ~ 0 +3
					Delete `$INSTDIR\$R1`
					RMDir /r `$INSTDIR\$R1`
				${EndIf}
				FindNext $R0 $R1
				${Loop}
			FindClose $R0
			Delete `${INI}`
		${EndIf}
	!endif

	!ifndef PLUGININSTALLER
		;=== Refresh PortableApps.com Menu (not final version)
		${GetParent} $INSTDIR $0
		;=== Check that it exists at the right location
		SetDetailsPrint both
		DetailPrint '$(checkforplatform)'
		${If} ${FileExists} `$0\PortableApps.com\PortableAppsPlatform.exe`
			;=== Check that it's the real deal so we aren't hanging with no response
			MoreInfo::GetProductName `$0\PortableApps.com\PortableAppsPlatform.exe`
			Pop $1
			${If} $1 == "PortableApps.com Platform"
				MoreInfo::GetCompanyName `$0\PortableApps.com\PortableAppsPlatform.exe`
				Pop $1
				${If} $1 == "PortableApps.com"

					;=== Check that it's running
					FindProcDLL::FindProc "PortableAppsPlatform.exe"
					${If} $R0 == "1"

						;=== Send message for the Menu to refresh
						CreateDirectory "$0\PortableApps.com\Data"
						WriteINIStr "$0\PortableApps.com\Data\NewApp.ini" "NewApp" "AppID" "${APPID}"

						DetailPrint '$(refreshmenu)'
						${IfNot} ${FileExists} `$0\PortableApps.com\App\PortableAppsPlatform.exe`
							StrCpy $2 'PortableApps.comPlatformWindowMessageToRefresh$0\PortableApps.com\PortableAppsPlatform.exe'
							System::Call "user32::RegisterWindowMessage(t r2) i .r3"
							SendMessage 65535 $3 0 0 /TIMEOUT=1
						${Else} ; old message
							StrCpy $2 'PortableApps.comPlatformWindowMessageToRefresh$0\PortableApps.com\App\PortableAppsPlatform.exe'
							System::Call "user32::RegisterWindowMessage(t r2) i .r3"
							SendMessage 65535 $3 0 0 /TIMEOUT=1
						${EndIf}
					${EndIf}
				${EndIf}
			${EndIf}
		${EndIf}
	!endif
	DetailPrint $InstallingStatusString
	SetDetailsPrint listonly
	Delete "$INSTDIR\7zTemp\7z.exe"
	Delete "$INSTDIR\7zTemp\7z.dll"
	RMDir "$INSTDIR\7zTemp"

!ifdef DownloadURL
	Delete "$INTERNET_CACHE\${FILE1}"
	Delete "$INTERNET_CACHE\${FILE2}"
	Delete "$INTERNET_CACHE\${DownloadFileName}"
!endif

!ifndef PLUGININSTALLER
	DeleteINISec "${APPINFO}\appinfo.ini" "Installer"
!endif

	${If} $bolLogFile == true
		${DumpLogToFile} "$EXEDIR\$EXEFILE.log"
	${EndIf}
	SetOutPath $TEMP
SectionEnd

!ifdef MAINSECTIONTITLE
	!ifdef OPTIONALSECTION1TITLE
		Section /o "!${OPTIONALSECTION1TITLE}" "${OPTIONALSECTION1}"
		SectionEnd
	!endif
	!ifdef OPTIONALSECTION2TITLE
		Section /o "!${OPTIONALSECTION2TITLE}" "${OPTIONALSECTION2}"
		SectionEnd
	!endif
	!ifdef OPTIONALSECTION3TITLE
		Section /o "${OPTIONALSECTION3TITLE}" "${OPTIONALSECTION3}"
		SectionEnd
	!endif
	!ifdef OPTIONALSECTION4TITLE
		Section /o "${OPTIONALSECTION4TITLE}" "${OPTIONALSECTION4}"
		SectionEnd
	!endif
	!ifdef OPTIONALSECTION5TITLE
		Section /o "${OPTIONALSECTION5TITLE}" "${OPTIONALSECTION5}"
		SectionEnd
	!endif
	!ifdef OPTIONALSECTION6TITLE
		Section /o "${OPTIONALSECTION6TITLE}" "${OPTIONALSECTION6}"
		SectionEnd
	!endif
	LangString x ${LANG_ENGLISH} "requires Administrative Privileges"
	LangString x ${LANG_ARABIC} `يتطلب امتيازات إدارية`
	LangString x ${LANG_ARMENIAN} `պահանջում է վարչական արտոնությունները`
	LangString x ${LANG_AZERBAIJANI} `inzibati güzəştlər tələb edir`
	LangString x ${LANG_BELARUSIAN} `патрабуе адміністрацыйных прывілеяў`
	LangString x ${LANG_BOSNIAN} `zahtijeva administratorske privilegije`
	LangString x ${LANG_BULGARIAN} `изисква администраторски права`
	LangString x ${LANG_CROATIAN} `zahtijeva administratorske ovlasti`
	LangString x ${LANG_CZECH} `vyžaduje administrátorská práva`
	LangString x ${LANG_DANISH} `kræver Administrative privilegier`
	LangString x ${LANG_DUTCH} `vereist administratieve voorrechten`
	LangString x ${LANG_FARSI} `به اختیارات مدیریتی نیاز`
	LangString x ${LANG_FINNISH} `vaatii järjestelmänvalvojan oikeudet`
	LangString x ${LANG_FRENCH} `nécessite des privilèges administratifs`
	LangString x ${LANG_GERMAN} `Administratorrechte erforderlich`
	LangString x ${LANG_GREEK} `απαιτεί δικαιώματα διαχειριστή`
	LangString x ${LANG_HUNGARIAN} `rendszergazdai jogosultságokra van szükség`
	LangString x ${LANG_INDONESIAN} `membutuhkan Wewenang Administrasi`
	LangString x ${LANG_ITALIAN} `richiede privilegi amministrativi`
	LangString x ${LANG_JAPANESE} `管理者権限が必要です`
	LangString x ${LANG_KAZAKH} `әкімшілік артықшылықтар талап`
	LangString x ${LANG_KOREAN} `관리자 권한이 필요`
	LangString x ${LANG_LITHUANIAN} `reikia turėti administratoriaus teises`
	LangString x ${LANG_MALAY} `memerlukan Keistimewaan Pentadbiran`
	LangString x ${LANG_NORWEGIAN} `krever administratorrettigheter`
	LangString x ${LANG_POLISH} `wymaga uprawnień administratora`
	LangString x ${LANG_PORTUGUESEBR} `requer privilégios administrativos`
	LangString x ${LANG_ROMANIAN} `necesită privilegii administrative`
	LangString x ${LANG_RUSSIAN} `требует административных привилегий`
	LangString x ${LANG_SERBIAN} `захтева административне привилегије`
	LangString x ${LANG_SIMPCHINESE} `需要管理权限`
	LangString x ${LANG_SLOVAK} `vyžaduje administrátorské práva`
	LangString x ${LANG_SLOVENIAN} `zahteva skrbniške pravice`
	LangString x ${LANG_SPANISH} `requiere privilegios administrativos`
	LangString x ${LANG_SWEDISH} `kräver administrativa rättigheter`
	LangString x ${LANG_THAI} `ต้องใช้สิทธิในการบริหาร`
	LangString x ${LANG_TRADCHINESE} `需要管理權限`
	LangString x ${LANG_TURKISH} `Yönetici Ayrıcalıkları gerektirir`
	LangString x ${LANG_UKRAINIAN} `вимагає адміністративних привілеїв`
	LangString x ${LANG_URDU} `انتظامی استحقاق کی ضرورت ہے`
	LangString x ${LANG_VIETNAMESE} `đòi hỏi đặc quyền hành chính`
	!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
		!insertmacro MUI_DESCRIPTION_TEXT ${MAINSECTIONIDX}       "${MAINSECTIONDESCRIPTION}"
		!ifdef OPTIONALSECTION1TITLE
			!insertmacro MUI_DESCRIPTION_TEXT ${OPTIONALSECTION1} "$(86)"
		!endif
		!ifdef OPTIONALSECTION2TITLE
			!insertmacro MUI_DESCRIPTION_TEXT ${OPTIONALSECTION2} "$(64)"
		!endif
		!ifdef OPTIONALSECTION3TITLE
			!insertmacro MUI_DESCRIPTION_TEXT ${OPTIONALSECTION3} "$(x)"
		!endif
		!ifdef OPTIONALSECTION4TITLE
			!insertmacro MUI_DESCRIPTION_TEXT ${OPTIONALSECTION4} "$(x)"
		!endif
		!ifdef OPTIONALSECTION5TITLE
			!insertmacro MUI_DESCRIPTION_TEXT ${OPTIONALSECTION5} "$()"
		!endif
		!ifdef OPTIONALSECTION6TITLE
			!insertmacro MUI_DESCRIPTION_TEXT ${OPTIONALSECTION6} "$()"
		!endif
	!insertmacro MUI_FUNCTION_DESCRIPTION_END
!endif

;=== set custom sizes
!ifdef MAINSECTIONTITLE
Function .onSelChange
	Push $1
	Push $2
	SectionGetFlags ${OPTIONALSECTION1} $1
	SectionGetFlags ${OPTIONALSECTION2} $2
	IntOp $1 $1 & ${SF_SELECTED}
	IntOp $2 $2 & ${SF_SELECTED}
	${If} $1 >= ${SF_SELECTED}
	${AndIf} $2 >= ${SF_SELECTED}
		SectionSetSize ${MAINSECTIONIDX} 15710
	${ElseIf} $1 >= ${SF_SELECTED}
	${AndIf} $2 != ${SF_SELECTED}
		SectionSetSize ${MAINSECTIONIDX} 8130
	${ElseIf} $1 != ${SF_SELECTED}
	${AndIf} $2 >= ${SF_SELECTED}
		SectionSetSize ${MAINSECTIONIDX} 8692
	${ElseIf} $1 != ${SF_SELECTED}
	${AndIf} $2 != ${SF_SELECTED}
		SectionSetSize ${MAINSECTIONIDX} 0
	${EndIf}
	Pop $2
	Pop $1
FunctionEnd
!endif

Function .onInstFailed
	!ifdef COPYLOCALFILES
		${registry::Unload}
	!endif
	RMDir $INSTDIR ;remove directory if empty
FunctionEnd

!ifdef COPYLOCALFILES
	Function .onInstSuccess
		${registry::Unload}
	FunctionEnd
	Function CustomAbortFunction
		${registry::Unload}
	FunctionEnd
!endif