; NSIS with Registry.nsh in Include and Registry.dll, FindProcDLL.dll, KillProcDLL.dll and SimpleSC.dll in Plugins

; **************************************************************************
; === Define constants ===
; **************************************************************************
!define VER "0.0.0.0"
!define APPNAME "Google Chrome"
!define APP "Chrome"
!define APPDIR "App\Chrome"
!define APPEXE "chrome.exe"
!define APPSWITCH ` --user-data-dir="$EXEDIR\Data\${APP}"`

; --- Define RegKeys ---
	!define REGKEY1 "HKEY_CURRENT_USER\Software\Google"
	!define REGKEY2 "HKEY_LOCAL_MACHINE\SOFTWARE\Google"
	!define REGKEY3 "HKEY_LOCAL_MACHINE\SOFTWARE\MozillaPlugins"

; **************************************************************************
; === Best Compression ===
; **************************************************************************
SetCompressor /SOLID lzma
SetCompressorDictSize 32

; **************************************************************************
; === Includes ===
; **************************************************************************
!include "..\_Include\Launcher.nsh" 

; **************************************************************************
; === Set basic information ===
; **************************************************************************
Name "${APPNAME} Portable"
OutFile "..\..\..\ChromePortable\${APP}Portable.exe"
Icon "${APP}.ico"

; **************************************************************************
; === Other Actions ===
; **************************************************************************

Var INCOGNITO_SWITCH
Var CACHE_SWITCH
Var PLUGINS_SWITCH
Var PARENTDIR

Function Init
IfFileExists "$EXEDIR\Data\${APP}\Default\Preferences" +3
CreateDirectory "$EXEDIR\Data\${APP}\Default"
CopyFiles "$EXEDIR\App\DefaultData\${APP}\Default\*.*" "$EXEDIR\Data\${APP}\Default"

StrCpy $INCOGNITO_SWITCH ""
ReadINIStr $0 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "Incognito"
WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "Incognito" "false"
StrCmp $0 "true" 0 +3
	WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "Incognito" "true"
	StrCpy $INCOGNITO_SWITCH " --incognito"

StrCpy $CACHE_SWITCH ""
ReadINIStr $0 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "TempCache"
StrCmp $0 "false" +4
	WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "TempCache" "true"
	CreateDirectory "$TEMP\${APP}Portable"
	StrCpy $CACHE_SWITCH ` --disk-cache-dir="$TEMP\${APP}Portable"`

StrCpy $PLUGINS_SWITCH ""
ReadINIStr $0 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "AllowOutdatedPlugins"
StrCmp $0 "false" +3
	WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "AllowOutdatedPlugins" "true"
	StrCpy $PLUGINS_SWITCH ` --allow-outdated-plugins`

WriteRegStr HKEY_LOCAL_MACHINE "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\AuthorizedApplications\List" "$EXEDIR\${APPDIR}\${APPEXE}" "$EXEDIR\${APPDIR}\${APPEXE}:*:Enabled:${APP}"
WriteRegStr HKEY_LOCAL_MACHINE "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List" "$EXEDIR\${APPDIR}\${APPEXE}" "$EXEDIR\${APPDIR}\${APPEXE}:*:Enabled:${APP}"

StrCpy $PARENTDIR $EXEDIR -15
	${registry::BackupKey} "${REGKEY3}"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\MozillaPlugins\@java.com/JavaPlugin" "Path" "$PARENTDIR\CommonFiles\Java\bin\plugin2\npjp2.dll"

FunctionEnd
;**********************************************************
Function Close
ReadINIStr $0 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "DeleteTempCache"
StrCmp $0 "false" +3
WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "DeleteTempCache" "true"
	RMDir "/r" "$TEMP\${APP}Portable"

DeleteRegValue HKEY_LOCAL_MACHINE "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\AuthorizedApplications\List" "$EXEDIR\${APPDIR}\${APPEXE}"
DeleteRegValue HKEY_LOCAL_MACHINE "SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\AuthorizedApplications\List" "$EXEDIR\${APPDIR}\${APPEXE}"

; RMDir "$DOCUMENTS\Downloads"

	${registry::RestoreBackupKey} "${REGKEY3}"

FunctionEnd

; **************************************************************************
; === Run Application ===
; **************************************************************************
Function Launch
ReadINIStr $1 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "AdditionalParameters"
StrCmp $1 "" 0 +2
WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "AdditionalParameters" ""

SetOutPath "$EXEDIR\${APPDIR}"
${GetParameters} $0
ExecWait `"$EXEDIR\${APPDIR}\${APPEXE}"${APPSWITCH}$INCOGNITO_SWITCH$CACHE_SWITCH$PLUGINS_SWITCH $1 $0`
WriteINIStr "$EXEDIR\Data\${APP}Portable.ini" "${APP}Portable" "GoodExit" "true"
newadvsplash::stop
FunctionEnd

; **************************************************************************
; ==== Actions on Registry Keys =====
; **************************************************************************
Function BackupLocalKeys
	${registry::BackupKey} "${REGKEY1}"
	${registry::BackupKey} "${REGKEY2}"
Sleep 50
FunctionEnd

Function RestoreLocalKeys
	${registry::RestoreBackupKey} "${REGKEY1}"
	${registry::RestoreBackupKey} "${REGKEY2}"
Sleep 50
${registry::Unload}
FunctionEnd

; **************************************************************************
; ==== Running ====
; **************************************************************************

Section "Main"

	Call CheckStart

	Call BackupLocalKeys

	Call Init

		Call SplashLogo
		Call Launch

	Call Restore

SectionEnd

Function Restore

	Call Close

	Call RestoreLocalKeys

FunctionEnd
