; ----------------------------------------------------
; ExecutePortable
; ----------------------------------------------------
; Par fat115
; 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 ExecutePortable.
; Execute: NSIS (http://www.nullsoft.com).
; Require plugins: newadvsplash
; $id=ExecutePortable.nsi $date=2010-03-25
; ----------------------------------------------------
;Copyright (C) 2005-2010 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
; Définition variables entête
!define APPLANG "French"
!define NAME "ExecutePortable"
!define FULLNAME "ExecutePortable"
!define APP "Execute"
!define VER "0.2.0.0" ;Version de l'application
!define LAUNCHVER "0.9.8.0" ;Version 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"

; Etiquette
Caption "${FULLNAME} - Exécution portable de scripts AutoHotkey/AutoIt3"

; ----------------------------------------------------

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

; Icone
Icon "${NAME}.ico"
WindowIcon Off

; Nom du fichier à créer
OutFile "..\${NAME}.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 LogicLib.nsh
!include FileFunc.nsh
!include StrFunc.nsh
${StrRep}

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

; Inclusion langue française (ajout de sarkos)
LoadLanguageFile "${NSISDIR}\Contrib\Language Files\${APPLANG}.nlf"

; ----------------------------------------------------
; 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 "${NAME}.exe"
VIAddVersionKey InternalName "${NAME}-${VER}-r${LAUNCHVER}"
VIAddVersionKey FileVersion "${SCRIPTVER}"
	
; ----------------------------------------------------
; Variables

Var ScriptFullName
Var ScriptName
Var ScriptFileName
Var ScriptType
Var ScriptDirectory
Var BaseDirectory
Var ExecString

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

Section "Main"

; Récupérer les infos du script passé en paramètre
	${GetParameters} $ScriptFullName
	${StrRep} $ScriptFullName $ScriptFullName "$\"" ""
	${GetFileExt} $ScriptFullName $ScriptType
	${GetFileName} $ScriptFullName $ScriptFileName
	${GetBaseName} $ScriptFullName $ScriptName
	${GetParent} $ScriptFullName $ScriptDirectory
	${GetParent} "$EXEDIR" $BaseDirectory

; Test du type de script
	${If} "$ScriptType" == "ahk"
		Goto ExecuteAhk
	${ElseIf} "$ScriptType" == "au3"
		Goto TestLocalAu3
	${EndIf}
	
; Pas un script AutoHotkey ou AutoIt3
	MessageBox MB_OK|MB_ICONEXCLAMATION `$ScriptFileName :${WRONGTYPE}`
		Abort

	ExecuteAhk:
		StrCpy $ExecString '"$BaseDirectory\AutoHotkeyPortable\AutoHotkeyPortable.exe" "$ScriptFullName"'
		Exec '$ExecString'
		Goto TheEnd
	
; Test si AutoIT3 est installé en local (utilisation en priorité)
	TestLocalAu3:
		IfFileExists "$PROGRAMFILES\AutoIt3\AutoIt3.exe" "" TestPortableAu3
			StrCpy $ExecString '"$PROGRAMFILES\AutoIt3\AutoIt3.exe" "$ScriptFullName"'
			Goto ExecuteAu3

; Test si AutoIT3 est disponible en version "portable"
	TestPortableAu3:
		IfFileExists "$BaseDirectory\AutoIt3Portable\App\AutoIt3\AutoIt3.exe" "" NoAutoIt3
			StrCpy $ExecString '"$BaseDirectory\AutoIt3Portable\App\AutoIt3\AutoIt3.exe" "$ScriptFullName"'
			Goto ExecuteAu3

; Proposition de décompression du lanceur  + téléchargement AutoIt3
	NoAutoIt3:
		MessageBox MB_YESNO|MB_ICONSTOP `${NOAU3}` IDNO ExitApp
			ExecWait '"$EXEDIR\AutoIT3Portable.exe" -y -ao -o"$BaseDirectory"'
			ExecShell "open" "http://www.autoitscript.com/autoit3/downloads.shtml"
			Sleep 2000
		
	ExitApp:
		Abort
		
; On lance le script au3
	ExecuteAu3:
		Exec $ExecString

	TheEnd:
		
SectionEnd

; -------------------------------------------
; Fin du script
