; ----------------------------------------------------
; CAFEPortable
; ----------------------------------------------------
; Par pyg
; Utilisant comme base de travail les scripts de John T. Haller et Sarkos et pyg
; License : GPL
; Ce script permet de créer le lanceur de CAFEPortable.
; Compiler: NSIS (http://www.nullsoft.com).
; Require plugins: newadvsplash
; $id=CAFEPortable.nsi $date=2010-04-16
; ----------------------------------------------------
;Copyright (C) 2005-2009 Framakey

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

;This software is OSI Certified Open Source Software.
;OSI Certified is a certification mark of the Open Source Initiative.

;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.
; ----------------------------------------------------
; Général
; ---- Modifiez les valeurs souhaitées ----
; Définition variables entête
!define APPLANG "French"
!define FULLNAME "CAFEPortable"
!define APPNAME "CAFE"
!define VER "1.6.8.0" ;Version de l'application
!define LAUNCHVER "01" ;Version du lanceur
!define SCRIPTVER "0.9.9.2" ;Version du script du lanceur
!define PROJECT "Framakey" ;Nom du projet
!define COMPANY "Framakey.org" ;Nom du groupe développant le projet
!define WEBSITE "http://www.framakey.org"
!define DEFAULTEXE "cafe.exe"
!define DEFAULTAPPDIR "App\cafe"
!define DEFAULTDATADIR "Data\settings"
!define MUTEXNAME "${FULLNAME}_${VER}"
; Etiquette
Caption "${FULLNAME} - Ouvrez vos fichiers avec vos applications Framakey"

; ----------------------------------------------------
; ---- à partir de là, vous n'avez normalement rien à modifier ---

; Nom de l'exécutable
Name "${FULLNAME}"

; Icone
Icon "..\..\App\AppInfo\appicon.ico"
WindowIcon Off

; Nom du fichier à créer
OutFile "..\..\${FULLNAME}.exe"

; Options de Compilation
CRCCheck On
SilentInstall Silent
AutoCloseWindow True
RequestExecutionLevel user

; Compression Optimale
SetCompress Auto
SetCompressor /SOLID lzma
SetCompressorDictSize 32
SetDatablockOptimize On

; ----------------------------------------------------
; Liste des includes
;(Standard NSIS)
!include FileFunc.nsh

; Personnalisées
!include "${FULLNAME}Lang_${APPLANG}.nsh"

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

VIProductVersion "${VER}"
VIAddVersionKey ProductName "${FULLNAME}"
VIAddVersionKey FileDescription "${FILEDESCRIPTIONTEXT}"
VIAddVersionKey LegalTrademarks "${LEGALTRADEMARKSTEXT}"
VIAddVersionKey LegalCopyright "${LEGALCOPYRIGHTTEXT}"
VIAddVersionKey Comments "${COMMENTSTEXT}"
VIAddVersionKey CompanyName "${COMPANY}"
VIAddVersionKey OriginalFilename "${FULLNAME}.exe"
VIAddVersionKey InternalName "${FULLNAME}-${VER}-r${LAUNCHVER}"
VIAddVersionKey FileVersion "${SCRIPTVER}"
	
; ----------------------------------------------------
; Variables

Var AppDirectory
Var AppExecutable
Var DataDir
Var Parameters
Var AdditionalParameters
Var DisableSplashScreen
Var ExecString

; ----------------------------------------------------
; Sections

Section "Main"

	; Définition des valeurs par défaut des paramètres
	StrCpy "$DisableSplashScreen" "false"
	StrCpy "$AppDirectory" "$EXEDIR\${DEFAULTAPPDIR}"
	StrCpy "$AppExecutable" "${DEFAULTEXE}"
	StrCpy "$DataDir" "$EXEDIR\${DEFAULTDATADIR}"
	StrCpy "$AdditionalParameters" ""

	; Vérifie s'il existe un fichier INI
	IfFileExists "$EXEDIR\${FULLNAME}.ini" "" CheckExec
		; Lit les paramètres du fichier INI et conserve la valeur par défaut si non défini
		ReadINIStr $0 "$EXEDIR\${FULLNAME}.ini" "${FULLNAME}" "DisableSplashScreen"
			StrCmp $0 "" +2 0
				StrCpy "$DisableSplashScreen" "$0"
		ReadINIStr $0 "$EXEDIR\${FULLNAME}.ini" "${FULLNAME}" "${APPNAME}Directory"
			StrCmp $0 "" +2 0
				StrCpy "$AppDirectory" "$EXEDIR\$0"
		ReadINIStr $0 "$EXEDIR\${FULLNAME}.ini" "${FULLNAME}" "${APPNAME}Executable"
			StrCmp $0 "" +2 0
				StrCpy "$AppExecutable" "$0"
		ReadINIStr $0 "$EXEDIR\${FULLNAME}.ini" "${FULLNAME}" "SettingsDirectory"
			StrCmp $0 "" +2 0
				StrCpy "$DataDir" "$EXEDIR\$0"
		ReadINIStr $0 "$EXEDIR\${FULLNAME}.ini" "${FULLNAME}" "AdditionalParameters"
			StrCmp $0 "" +2 0
				StrCpy "$AdditionalParameters" "$0"
	ClearErrors

	CheckExec:
		IfFileExists "$AppDirectory\$AppExecutable" CreateLaunchCmd
			; Si le chemin pointant vers l'exécutable est invalide
			MessageBox MB_OK|MB_ICONEXCLAMATION `${NOEXEMSG}`
			Abort
	
	CreateLaunchCmd:
		; Vérification des paramètres passés au lanceur (par exemple par CAFE)
		${GetParameters} $Parameters
		StrCpy $ExecString `"$AppDirectory\$AppExecutable" $Parameters $AdditionalParameters`

	CheckPortableInstance:
		; Si la version portable est déjà lancée : on lance directement
		System::Call 'kernel32::OpenMutexA(i 0x00020000, i 0, t "${MUTEXNAME}") i .r1 ?e'
		StrCmp $1 0 CheckLocalInstance
			Exec "$ExecString"
			Quit

	CheckLocalInstance:
		; Si l'application est déjà lancée (non portable)
		FindProcDLL::FindProc "$AppExecutable"
		StrCmp $R0 "1" WarningInstances ShowSplash
		
	WarningInstances:
			MessageBox MB_OK|MB_ICONEXCLAMATION `${FOUNDPROCESSMSG}`
			Abort

	ShowSplash:
		StrCmp $DisableSplashScreen "true" LaunchAndWait
			; Affiche le splash screen
			InitPluginsDir
			File /oname=$PLUGINSDIR\splash.jpg "${FULLNAME}_splash.jpg"
			newadvsplash::show /NOUNLOAD 2000 200 200 -1 /L $PLUGINSDIR\splash.jpg
			Sleep 500

	LaunchAndWait:
		; Première instance portable : on crée un mutex
		System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${MUTEXNAME}") i .r1 ?e'
		; Lancement de l'application
		SetOutPath "$AppDirectory"
		ExecWait "$ExecString"
		; Déchargement du splashscreen
		newadvsplash::stop /WAIT

SectionEnd
