; ----------------------------------------------------
; PortableVLC
; ----------------------------------------------------
; Par John T. Haller
; Adaptation: sarkos pour Framakey
; License: GPL
; Ce script permet de créer PortableVLC.
; Compiler: NSIS de Nullsoft (http://www.nullsoft.com).
; Require plugins: "newadvsplash"
; $id=PortableVLC.nsi $date=2006-04-29
; ----------------------------------------------------
; Copyright (C) 2004-2006 John T. Haller
; Copyright (C) 2005-2006 Framakey Team

; Website : http://www.portableapps.com
; Website: http://www.framakey.org

; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation; either version 2
; of the License, or (at your option) any later version.

; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.

; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
; ----------------------------------------------------

;pyg 2008/08 - Niveau d'execution pour Windows Vista
RequestExecutionLevel user

; Définition variables chemins
!define DEFAULTEXE "vlc.exe" ;Nom de l'exécutable par défaut
!define DEFAULTAPPDIR "vlc" ;Répertoire par défaut de l'application
!define DEFAULTSETTINGSDIR "settings" ;Répertoire utilisateur par défaut

; Définition variables entête
!define APPLANG "multilingue"
!define NAME "PortableVLC" ;Nom du lanceur (forme abrégée)
!define FULLNAME "PortableVLC Media Player" ;Nom complet du lanceur
!define APP "VLC" ;Nom de l'application
!define VER "1.1.4.1" ;Version du lanceur
!define COMPANY "Framakey" ;Nom du groupe développant le projet
!define DEVELOP "John T. Haller, adaptation sarkos" ;Noms des développeurs
!define WEBSITE "http://www.framakey.org" ;Site Internet

; ----------------------------------------------------
; Traduction des informations de version

!define FILEDESCRIPTIONTEXT "${FULLNAME} pour Windows"
!define LEGALCOPYRIGHTTEXT "GPL"
!define COMMENTSTEXT "Permet de lancer ${APP} (${APPLANG}) depuis un disque amovible. Pour plus de détails, visitez ${WEBSITE}"
!define CAPTIONTEXT "${FULLNAME} - Portable et discret"

; ----------------------------------------------------
; Traduction des messages de boite de dialogue

!define NOEXEMSG `"$EXEDIR\${DEFAULTAPPDIR}\$PROGRAMEXECUTABLE" est introuvable. Merci de vérifier votre configuration.`

; ----------------------------------------------------
; Informations de compilation

; Nom de l'exécutable
  Name "${NAME}"
; Etiquette
  Caption "${CAPTIONTEXT}"
; Icone
  Icon "${NAME}.ico"
  WindowIcon Off
; Nom du fichier à créer
  OutFile "${NAME}.exe"

; Runtime Switches
  SetDateSave on
  SetDatablockOptimize on
  CRCCheck On
  SilentInstall Silent
  AutoCloseWindow True

; ----------------------------------------------------
; Version Informations

VIProductVersion "${VER}"
VIAddVersionKey FileDescription "${FILEDESCRIPTIONTEXT}"
VIAddVersionKey LegalCopyright "${LEGALCOPYRIGHTTEXT}"
VIAddVersionKey Comments "${COMMENTSTEXT}"
VIAddVersionKey CompanyName "${COMPANY}"
VIAddVersionKey OriginalFilename "${NAME}.exe"
VIAddVersionKey FileVersion "${VER}"

; ----------------------------------------------------
; Variables

Var PROGRAMDIRECTORY
Var SETTINGSDIRECTORY
Var ADDITIONALPARAMETERS
Var EXECSTRING
Var WAITFORPROGRAM
Var PROGRAMEXECUTABLE
Var INIPATH
Var DISABLESPLASHSCREEN

Section "Main"
	;=== Find the INI file, if there is one
		IfFileExists "$EXEDIR\${NAME}.ini" "" CheckSubINI
			StrCpy "$INIPATH" "$EXEDIR\"
			Goto ReadINI

	CheckSubINI:
		IfFileExists "$EXEDIR\${NAME}\${NAME}.ini" "" CheckSubSubINI
			StrCpy "$INIPATH" "$EXEDIR\${NAME}\"
			Goto ReadINI

	CheckSubSubINI:
		IfFileExists "$EXEDIR\PortableApps\${NAME}\${NAME}.ini" "" CheckPortableAppsINI
			StrCpy "$INIPATH" "$EXEDIR\PortableApps\${NAME}\"
			Goto ReadINI

	CheckPortableAppsINI:
		IfFileExists "$EXEDIR\Data\${NAME}\${NAME}.ini" ""  NoINI
			StrCpy "$INIPATH" "$EXEDIR\Data\${NAME}\"
			Goto ReadINI

	ReadINI:
		;=== Read the parameters from the INI file
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "${APP}Directory"
		StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\$0"
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "SettingsDirectory"
		StrCpy "$SETTINGSDIRECTORY" "$EXEDIR\$0"

		;=== Check that the above required parameters are present
		IfErrors NoINI

		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "AdditionalParameters"
		StrCpy "$ADDITIONALPARAMETERS" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "WaitFor${APP}"
		StrCpy "$WAITFORPROGRAM" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "${APP}Executable"
		StrCpy "$PROGRAMEXECUTABLE" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "DisableSplashScreen"
		StrCpy "$DISABLESPLASHSCREEN" $0

		;=== Any missing unrequired INI entries will be an empty string, ignore associated errors
		ClearErrors

		;=== Correct PROGRAMEXECUTABLE if blank
		StrCmp $PROGRAMEXECUTABLE "" "" EndINI
			StrCpy "$PROGRAMEXECUTABLE" "${DEFAULTEXE}"
			Goto EndINI

	NoINI:
		;=== No INI file, so we'll use the defaults
		StrCpy "$ADDITIONALPARAMETERS" ""
		StrCpy "$WAITFORPROGRAM" "false"
		StrCpy "$PROGRAMEXECUTABLE" "${DEFAULTEXE}"
		StrCpy "$DISABLESPLASHSCREEN" "false"

		IfFileExists "$EXEDIR\${DEFAULTAPPDIR}\${DEFAULTEXE}" "" CheckPortableProgramDIR
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\${DEFAULTAPPDIR}"
			StrCpy "$SETTINGSDIRECTORY" "$EXEDIR\${DEFAULTSETTINGSDIR}"
			GoTo EndINI

		CheckPortableProgramDIR:
			IfFileExists "$EXEDIR\${NAME}\${DEFAULTAPPDIR}\${DEFAULTEXE}" "" CheckPortableAppsDIR
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\${NAME}\${DEFAULTAPPDIR}"
			StrCpy "$SETTINGSDIRECTORY" "$EXEDIR\${NAME}\${DEFAULTSETTINGSDIR}"
			GoTo EndINI

		CheckPortableAppsDIR:
			IfFileExists "$EXEDIR\PortableApps\${NAME}\${DEFAULTAPPDIR}\${DEFAULTEXE}" "" CheckPortableAppsSplitDIR
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\PortableApps\${NAME}\${DEFAULTAPPDIR}"
			StrCpy "$SETTINGSDIRECTORY" "$EXEDIR\PortableApps\${NAME}\${DEFAULTSETTINGSDIR}"
			GoTo EndINI

		CheckPortableAppsSplitDIR:
			IfFileExists "$EXEDIR\Apps\${NAME}\${DEFAULTAPPDIR}\${DEFAULTEXE}" "" NoProgramEXE
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\Apps\${NAME}\${DEFAULTAPPDIR}"
			StrCpy "$SETTINGSDIRECTORY" "$EXEDIR\Data\${NAME}\${DEFAULTSETTINGSDIR}"
	
	EndINI:
		IfFileExists "$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" FoundProgramEXE

	NoProgramEXE:
		;=== Program executable not where expected
		MessageBox MB_OK|MB_ICONEXCLAMATION `${NOEXEMSG}`
		Abort
		
	FoundProgramEXE:
		;=== Get any passed parameters
		Call GetParameters
		Pop $0
		StrCmp "'$0'" "''" "" LaunchProgramParameters

		;=== No parameters
		StrCpy $EXECSTRING `"$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" --vlm-conf "$SETTINGSDIRECTORY\vlcrc" --no-plugins-cache --config "$SETTINGSDIRECTORY\vlcrc"`
		Goto SplashScreen

	LaunchProgramParameters:
		StrCpy $EXECSTRING `"$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" $0 --vlm-conf "$SETTINGSDIRECTORY\vlcrc" --no-plugins-cache --config "$SETTINGSDIRECTORY\vlcrc"`

	SplashScreen:
		StrCmp $DISABLESPLASHSCREEN "true" LaunchNow
		;=== Show the splash screen before processing the files
		InitPluginsDir
		File /oname=$PLUGINSDIR\splash.jpg "${NAME}_splash.jpg"
		
		newadvsplash::show /NOUNLOAD 2000 400 0 -1 /L $PLUGINSDIR\splash.jpg
		
	LaunchNow:
		StrCmp $WAITFORPROGRAM "true" LaunchAndWait LaunchAndClose

	LaunchAndWait:
		ExecWait $EXECSTRING
		GoTo TheEnd

	LaunchAndClose:
		Exec $EXECSTRING

	TheEnd:
		newadvsplash::stop /WAIT
SectionEnd

Function "GetParameters"
  Push $R0
  Push $R1
  Push $R2
  StrCpy $R0 $CMDLINE 1
  StrCpy $R1 '"'
  StrCpy $R2 1
  StrCmp $R0 '"' loop
    StrCpy $R1 ' ' ; we're scanning for a space instead of a quote
  loop:
    StrCpy $R0 $CMDLINE 1 $R2
    StrCmp $R0 $R1 loop2
    StrCmp $R0 "\" "" "nofile"
      IntOp $2 $R2 + 1
    nofile:
    ;MessageBox MB_OK "r0: $R0"
    StrCmp $R0 "" loop2
    IntOp $R2 $R2 + 1
    Goto loop
  loop2:
    IntOp $R0 $R2 - $2
    IntOp $R0 $R0 - 4
    ;MessageBox MB_OK "$R2 - $2 = $R0"
    StrCpy $R7 $CMDLINE $R0 $2 ; we save the filename
    ;MessageBox MB_OK "$2"
  loop2b:
    IntOp $R2 $R2 + 1
    StrCpy $R0 $CMDLINE 1 $R2
    ;MessageBox MB_OK "rr0: $R0"
    StrCmp $R0 " " loop2b
  StrCpy $R0 $CMDLINE "" $R2
  Pop $R2
  Pop $R1
  Exch $R0
FunctionEnd