; NSIS with Registry.nsh in Include and Registry.dll, FindProcDLL.dll, KillProcDLL.dll in Plugins
; http://wiki.videolan.org/VLC_command-line_help
; **************************************************************************
; === Define constants ===
; **************************************************************************
!define VER 		"0.0.0.0"	; version of launcher
!define APPNAME 	"VLC"	; complete name of program
!define APP 		"VLC"	; short name of program without space and accent  this one is used for the final executable an in the directory structure
!define APPEXE 		"vlc.exe"	; main exe name
!define APPDIR 		"App\VLCwin32"	; main exe relative path
!define APPEXE64 	"vlc.exe"	; main exe 64 name
!define APPDIR64 	"App\VLCwin64"	; main exe 64 relative path
!define APPSWITCH 	` --vlm-conf "$EXEDIR\Data\vlcrc" --no-plugins-cache --config "$EXEDIR\Data\vlcrc"`	; some default Parameters

; ---Define Local Dirs, SubDirs (if any) beginnig with back-slash and Portable Dirs ---
	!define LOCALDIR1 "$APPDATA\vlc"
	!define SUBDIR1 ""
	!define PORTABLEDIR1 "$EXEDIR\Data\VLC"
	!define DEFAULTPORTABLEDIR1 "$EXEDIR\App\DefaultData\VLC"

; --- Define RegServer Shared DLLs ---
	!define LOCALDLL1 "$WINDIR\axvlc.dll"
	!define PORTABLEDLL1 "$EXEDIR\${APPDIR}\axvlc.dll"
	!define PORTABLEDLL1_64 "$EXEDIR\${APPDIR64}\axvlc.dll"

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

; **************************************************************************
; === Includes ===
; **************************************************************************
!include "..\_Include\Launcher.nsh" 
!include "TextFunc.nsh"
!include "LogicLib.nsh"
!include "x64.nsh"
!insertmacro ConfigWrite

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


; **************************************************************************
; === Other Actions ===
; **************************************************************************
Function Init
CreateDirectory  "$EXEDIR\Data"
${ConfigWrite} "${LOCALDIR1}${SUBDIR1}\ml.xspf" "	<location>" "file://${LOCALDIR1}${SUBDIR1}\ml.xspf</location>" $R0
ReadINIStr $0 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "PortableSnapShots"
StrCmp $0 "false" +4
WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "PortableSnapShots" "true"
CreateDirectory  "$EXEDIR\SnapShots"
WriteINIStr "$EXEDIR\Data\vlcrc" "main" "snapshot-path" "$EXEDIR\SnapShots"
; WriteINIStr "$EXEDIR\Data\vlcrc" "main" "language" "fr"

FunctionEnd

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

Section "Main"

	Call CheckStart

	Call BackupLocalDirs
	Call RestorePortableDirs

	Call UnRegLocalDLL
	Call RegPortableDLL

	Call Init

		Call SplashLogo
		Call Launch

	Call Restore

SectionEnd

Function Restore

	Call UnRegPortableDLL
	Call RegLocalDLL

	Call BackupPortableDirs
	Call RestoreLocalDirs

FunctionEnd

; **************************************************************************
; === Run Application ===
; **************************************************************************
Function Launch
${GetParameters} $0
${If} ${RunningX64}
${AndIf} ${FileExists} "$EXEDIR\${APPDIR64}\${APPEXE64}"
SetOutPath "$EXEDIR\${APPDIR64}"
ExecWait `"$EXEDIR\${APPDIR64}\${APPEXE64}" $0${APPSWITCH}`
${Else}
SetOutPath "$EXEDIR\${APPDIR}"
ExecWait `"$EXEDIR\${APPDIR}\${APPEXE}" $0${APPSWITCH}`
${EndIf}
WriteINIStr "$EXEDIR\Data\${APP}Portable.ini" "${APP}Portable" "GoodExit" "true"
newadvsplash::stop
FunctionEnd

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

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

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

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

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

Function RegPortableDLL
${If} ${RunningX64}
${AndIf} ${FileExists} "${PORTABLEDLL1_64}"
	${dll::RegPortable} "${PORTABLEDLL1_64}"
${Else}
	${dll::RegPortable} "${PORTABLEDLL1}"
${EndIf}
FunctionEnd

Function UnRegPortableDLL
${If} ${RunningX64}
${AndIf} ${FileExists} "${PORTABLEDLL1_64}"
	${dll::UnRegPortable} "${PORTABLEDLL1_64}"
${Else}
	${dll::UnRegPortable} "${PORTABLEDLL1}"
${EndIf}
FunctionEnd

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

