/*
$APPDATA\Microsoft\Skype for Desktop
$APPDATA\Skype

--datapath="path"	Changes the folder where the user data is stored (by default, Skype stores them in %APPDATA%\Microsoft\Skype for Desktop). If path does not exist, Skype creates all the necessary folders.
--secondary	Launches several instances of Skype at the same time. To run multiple accounts, you must specify the parameter --datapath
--shutdown	Closes Skype processes, started without the parameter --secondary
--_="URI scheme"	Launches Skype using parameters of the URI scheme skype: (for example, if you pass --_="skype:echo123?call&video=true", Skype will make a video call to the echo123 service).

In addition to these, there are several other parameters, but I am not sure yet what they are intended for. Some of these parameters are given below:
--application-name
--auto-start
--migration
--no-sandbox
--type
*/
; **************************************************************************
; === Define constants ===
; **************************************************************************
!define /date VER	"%Y.%m.%d.%H"
!define APPNAME "Skype"
!define APP "Skype"
!define APPDIR "App\Skype"
!define APPEXE "Skype.exe"
!define APPSWITCH 	``

; --- Define RegKeys ---
	!define REGKEY1 "HKEY_CURRENT_USER\Software\Skype"
	!define REGKEY2 "HKEY_CURRENT_USER\Software\IM Providers"

; ---Define Local Dirs, SubDirs (if any) beginnig with back-slash and Portable Dirs ---
	!define LOCALDIR1 "$APPDATA\Microsoft"
	!define SUBDIR1 "\Skype for Desktop"
	!define PORTABLEDIR1 "$EXEDIR\Data\Skype for Desktop"
	!define DEFAULTPORTABLEDIR1 "$EXEDIR\App\DefaultData\Skype for Desktop"
	!define LOCALDIR2 "$APPDATA\Skype"
	!define SUBDIR2 ""

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

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

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

;*************************************************************
;*=== Multi Users ===*
;*************************************************************
Var USER
Function MultiUser
ReadINIStr $USER "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "User"
StrCmp $USER "" 0 +3
WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "User" "Skype"
StrCpy $USER "Skype"
IfFileExists "$EXEDIR\Data\$USER\*.*" +2
CreateDirectory "$EXEDIR\Data\$USER"
FunctionEnd

; **************************************************************************
; === Other Actions ===
; **************************************************************************
Function Init
nsisFirewall::AddAuthorizedApplication "$EXEDIR\${APPDIR}\${APPEXE}" "${APPNAME} Portable"

FunctionEnd

Function Close
nsisFirewall::RemoveAuthorizedApplication "$EXEDIR\${APPDIR}\${APPEXE}"

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}" --datapath="$EXEDIR\Data\$USER" $0 $1`
WriteINIStr "$EXEDIR\Data\${APP}Portable.ini" "${APP}Portable" "GoodExit" "true"
newadvsplash::stop
FunctionEnd

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

Section "Main"

	Call MultiUser
	Call CheckStart

	Call BackupLocalKeys
	Call RestorePortableKeys

	Call BackupLocalDirs
	Call RestorePortableDirs

	Call Init

		Call SplashLogo
		Call Launch

	Call Restore

SectionEnd

Function Restore

	Call Close

	Call BackupPortableDirs
	Call RestoreLocalDirs

	Call BackupPortableKeys
	Call RestoreLocalKeys

FunctionEnd

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

Function RestorePortableKeys
${registry::RestoreKey} "$EXEDIR\Data\${APP}.reg" $R0
Sleep 200
FunctionEnd

Function BackupPortableKeys
Delete "$EXEDIR\Data\${APP}.reg"
CreateDirectory "$EXEDIR\Data"
	${registry::SaveKey} "${REGKEY1}" "$EXEDIR\Data\${APP}.reg" "/A=1" $R0
Sleep 100
FunctionEnd

Function RestoreLocalKeys
	${registry::RestoreBackupKey} "${REGKEY1}"
	${registry::RestoreBackupKey} "${REGKEY2}"

${registry::Unload}
FunctionEnd

; **************************************************************************
; ==== Actions on Folders =====
; **************************************************************************
Function BackupLocalDirs
	${directory::BackupLocal} "${LOCALDIR1}" "${SUBDIR1}"
	${directory::BackupLocal} "${LOCALDIR2}" "${SUBDIR2}"
FunctionEnd

Function RestorePortableDirs
	${directory::RestorePortable} "${PORTABLEDIR1}" "${DEFAULTPORTABLEDIR1}" "${LOCALDIR1}" "${SUBDIR1}"
FunctionEnd

Function BackupPortableDirs
	${directory::BackupPortable} "${PORTABLEDIR1}" "${LOCALDIR1}" "${SUBDIR1}"
FunctionEnd

Function RestoreLocalDirs
	${directory::RestoreLocal} "${LOCALDIR1}" "${SUBDIR1}"
	${directory::RestoreLocal} "${LOCALDIR2}" "${SUBDIR2}"
FunctionEnd
