; **************************************************************************
; === Define constants ===
; **************************************************************************
!define /date VER	"%Y.%m.%d.%H"	; version of launcher
!define APPNAME 	"uTorrent"	; complete name of program
!define APP 		"uTorrent"	; short name of program without space and accent  this one is used for the final executable an in the directory structure
!define APPEXE 		"uTorrent.exe"	; main exe name
!define APPDIR 		"App\uTorrent"	; main exe relative path
!define APPSWITCH 	``	; some default Parameters

; --- Define RegKeys ---
	!define REGKEY1 "HKEY_CURRENT_USER\Software\BitTorrent"
	!define REGKEY2 "HKEY_CURRENT_USER\Software\Classes\FalconBetaAccount"
	!define REGKEY3 "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ESENT\Process\uTorrent"
	!define REGKEY4 "HKEY_CURRENT_USER\Software\BitTorrentPlus"

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

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

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

; **************************************************************************
; === Other Actions ===
; **************************************************************************
Var DOWNLOADSPATH
Var TORRENTSPATH
Var DOWNLOADSPATHLENGTH
Var TORRENTSPATHLENGTH
Function Init
${If} ${FileExists} "$EXEDIR\Data\${APP}\settings.dat"
CopyFiles "$EXEDIR\Data\${APP}\*.dat" "$EXEDIR\${APPDIR}"
Delete "$EXEDIR\Data\${APP}\*.dat"
CopyFiles "$EXEDIR\Data\${APP}\*.old" "$EXEDIR\${APPDIR}"
Delete "$EXEDIR\Data\${APP}\*.old"
Rename "$EXEDIR\Data\${APP}\apps" "$EXEDIR\${APPDIR}\apps"
Rename "$EXEDIR\Data\${APP}\dlimagecache" "$EXEDIR\${APPDIR}\dlimagecache"
Rename "$EXEDIR\Data\${APP}\ie" "$EXEDIR\${APPDIR}\ie"
Rename "$EXEDIR\Data\${APP}\share" "$EXEDIR\${APPDIR}\share"
Rename "$EXEDIR\Data\${APP}\updates" "$EXEDIR\${APPDIR}\updates"

ReadINIStr $0 "$EXEDIR\Data\${APP}Portable.ini" "${APP}Portable" "LastDirectory"
StrCpy $1 $0 3
StrCpy $2 $EXEDIR 3

StrCmp $1 $2 +4
	${textreplace::ReplaceInFile} "$EXEDIR\${APPDIR}\settings.dat" "$EXEDIR\${APPDIR}\settings.dat" "$1" "$2" "" $0
	${textreplace::ReplaceInFile} "$EXEDIR\${APPDIR}\resume.dat" "$EXEDIR\${APPDIR}\resume.dat" "$1" "$2" "" $0
	${textreplace::Unload}

${Else}
	SetOutPath "$EXEDIR\${APPDIR}"
	File "settings.dat"

	StrCpy $DOWNLOADSPATH "$EXEDIR\Downloads"
	StrCpy $TORRENTSPATH "$EXEDIR\Torrents"
	StrLen $DOWNLOADSPATHLENGTH "$DOWNLOADSPATH"
	StrLen $TORRENTSPATHLENGTH "$TORRENTSPATH"

	CreateDirectory "$DOWNLOADSPATH"
	CreateDirectory "$TORRENTSPATH"

	${textreplace::ReplaceInFile} "$EXEDIR\${APPDIR}\settings.dat" "$EXEDIR\${APPDIR}\settings.dat" "DownloadsPath" "$DOWNLOADSPATH" "" $0
	${textreplace::ReplaceInFile} "$EXEDIR\${APPDIR}\settings.dat" "$EXEDIR\${APPDIR}\settings.dat" "TorrentsPath" "$TORRENTSPATH" "" $0
	${textreplace::ReplaceInFile} "$EXEDIR\${APPDIR}\settings.dat" "$EXEDIR\${APPDIR}\settings.dat" "dir_active_download13" "dir_active_download$DOWNLOADSPATHLENGTH" "" $0
	${textreplace::ReplaceInFile} "$EXEDIR\${APPDIR}\settings.dat" "$EXEDIR\${APPDIR}\settings.dat" "dir_torrent_files12" "dir_torrent_files$TORRENTSPATHLENGTH" "" $0
	${textreplace::Unload}

${EndIf}
; WriteRegBin HKEY_CURRENT_USER "Software\uTorrentPlus" "license" 0000000056388f1e00000000545d25ef0000000003e6b53b56816c2ab1513578b27135faec6e9e065f3b5447b527a58c303e930648bc9ce790413c0ae5c34ea38cd95dd5b84f02adacfed29bc88493633ad2042b142ca311283ea5a0a97f148ac619a70ad921eefb60749e54ce7ba80ba0aa928edfd5a9e9dfbcc255f3cb1f507ea2790c1f4e5c5cc5c939818a051e2506c56352cafebea1d0e637b5c6615ef5d7a70d2c5c081e9aa56630d530bf93914ace2e42edcfef3bb2027a4f61b53d93de12a1c2aafaf90bfb09fe58023fd90e4f68af7b69cc9c0cf41769078c2337ac826b722932812b417538b558f3d7ff429280243497a0c153e59c73aa8b7235c9645f9ac28335c9a1facd8358e57b4ebc2ee99be7305514b3e0dfbd9c2c74a1437588edd2a5f926574ad237538f32

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

FunctionEnd

Function Close
CreateDirectory "$EXEDIR\Data\${APP}"
CopyFiles "$EXEDIR\${APPDIR}\*.dat" "$EXEDIR\Data\${APP}"
Delete "$EXEDIR\${APPDIR}\*.dat"
CopyFiles "$EXEDIR\${APPDIR}\*.old" "$EXEDIR\Data\${APP}"
Delete "$EXEDIR\${APPDIR}\*.old"
Rename "$EXEDIR\${APPDIR}\apps" "$EXEDIR\Data\${APP}\apps"
Rename "$EXEDIR\${APPDIR}\dlimagecache" "$EXEDIR\Data\${APP}\dlimagecache"
Rename "$EXEDIR\${APPDIR}\ie" "$EXEDIR\Data\${APP}\ie"
Rename "$EXEDIR\${APPDIR}\share" "$EXEDIR\Data\${APP}\share"
Rename "$EXEDIR\${APPDIR}\updates" "$EXEDIR\Data\${APP}\updates"

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

FunctionEnd

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

Section "Main"

	Call CheckStart

	Call BackupLocalKeys

	Call Init

		Call SplashLogo
		Call Launch

	Call Restore

SectionEnd

Function Restore

	Call Close

	Call RestoreLocalKeys

FunctionEnd

; **************************************************************************
; === Run Application ===
; **************************************************************************
Function Launch
${GetParameters} $0
SetOutPath "$EXEDIR\${APPDIR}"
ExecWait `"$EXEDIR\${APPDIR}\${APPEXE}"${APPSWITCH} $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}"
	${registry::BackupKey} "${REGKEY3}"
	${registry::BackupKey} "${REGKEY4}"
FunctionEnd

Function RestoreLocalKeys
	${registry::RestoreBackupKey} "${REGKEY1}"
	${registry::RestoreBackupKey} "${REGKEY2}"
	${registry::RestoreBackupKey} "${REGKEY3}"
	${registry::RestoreBackupKey} "${REGKEY4}"
${registry::Unload}
FunctionEnd
