
; **************************************************************************
; === Define constants ===
; **************************************************************************
!define /date VER	"%Y.%m.%d.%H"
!define APPNAME "Skype"
!define APP "Skype"
!define APPDIR "App\Skype\Phone"
!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 "$LOCALAPPDATA\Skype"
	!define SUBDIR1 ""
	!define DEFAULTPORTABLEDIR1 "$EXEDIR\App\DefaultData\Skype"
	!define LOCALDIR2 "$APPDATA\Skype"
	!define SUBDIR2 ""

; --- Define RegServer Shared DLLs ---
	!define LOCALDLL1 "$COMMONFILES\Skype\Skype4COM.dll"
	!define PORTABLEDLL1 "$EXEDIR\App\Skype\Skype4COM.dll"

; **************************************************************************
; === 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

WriteRegStr HKEY_CURRENT_USER "Software\Skype\Phone" "SkypePath" "$EXEDIR\${APPDIR}\${APPEXE}"

CreateDirectory "$EXEDIR\Data"
SetFileAttributes "$EXEDIR\Data" SYSTEM
WriteINIStr "$EXEDIR\Data\desktop.ini" ".ShellClassInfo" "ConfirmFileOp" "0"
WriteINIStr "$EXEDIR\Data\desktop.ini" ".ShellClassInfo" "IconFile" "$EXEDIR\${APPDIR}\${APPEXE}"
WriteINIStr "$EXEDIR\Data\desktop.ini" ".ShellClassInfo" "IconIndex" "2"
WriteINIStr "$EXEDIR\Data\desktop.ini" ".ShellClassInfo" "InfoTip" "Settings of ${APPNAME}"
SetFileAttributes "$EXEDIR\Data\desktop.ini" HIDDEN|SYSTEM 

StrCpy $R0 $EXEDIR "" 2
nsisFirewall::AddAuthorizedApplication "$EXEDIR\${APPDIR}\${APPEXE}" "${APPNAME} Portable"

${registry::RestoreKey} "$EXEDIR\Data\$USER\ProtectedStorage.reg" $R0
Sleep 200

FunctionEnd

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

ReadINIStr $USER "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "User"
Delete "$EXEDIR\Data\$USER\ProtectedStorage.reg"
CreateDirectory "$EXEDIR\Data\$USER"
	${registry::SaveKey} "${REGKEY1}\ProtectedStorage" "$EXEDIR\Data\$USER\ProtectedStorage.reg" "/A=1" $R0
Sleep 100

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" /removable $0 $1`
	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 UnRegLocalDLL
	Call RegPortableDLL

	Call Init

		Call SplashLogo
		Call Launch

	Call Restore

SectionEnd

Function Restore

	Call Close

	Call UnRegPortableDLL
	Call RegLocalDLL

	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
SetShellVarContext all
	${directory::BackupLocal} "${LOCALDIR1}" "${SUBDIR1}"
SetShellVarContext current
	${directory::BackupLocal} "${LOCALDIR2}" "${SUBDIR2}"
FunctionEnd

Function RestorePortableDirs
SetShellVarContext all
	CreateDirectory "${LOCALDIR1}${SUBDIR1}"
	CopyFiles /SILENT "${DEFAULTPORTABLEDIR1}\*.*" "${LOCALDIR1}${SUBDIR1}"
FunctionEnd

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

; ************************************************************************
; ==== Actions on DLLs ====
; ************************************************************************
Function UnRegLocalDLL
	${dll::UnRegLocal} "${LOCALDLL1}"
FunctionEnd

Function RegPortableDLL
	${dll::RegPortable} "${PORTABLEDLL1}"
FunctionEnd

Function UnRegPortableDLL
	${dll::UnRegPortable} "${PORTABLEDLL1}"
FunctionEnd

Function RegLocalDLL
	${dll::RegLocal} "${LOCALDLL1}"
FunctionEnd
