; ----------------------------------------------------
; CompilerPortable
; ----------------------------------------------------
; 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 CompilerPortable.
; Compiler: NSIS (http://www.nullsoft.com).
; Require plugins: newadvsplash
; $id=CompilerPortable.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 "CompilerPortable"
!define FULLNAME "CompilerPortable"
!define APP "Compiler"
!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} - Compilateur NSIS/AutoHotkey/AutoIt3 (Lanceur sélectif)"

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

; 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
Var LogFile
Var IconFile
Var LogMsg

; ----------------------------------------------------
; 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

; On lit le fichier INI
	ReadINIStr $LogMsg "$EXEDIR\${NAME}.ini" "Options" "CompilEndMsg"
	
	
; Test du type de script
	${If} "$ScriptType" == "nsi"
		Goto CompileNSI
	${ElseIf} "$ScriptType" == "ahk"
		Goto CompileAhk
	${ElseIf} "$ScriptType" == "au3"
		Goto TestLocalAu3
	${EndIf}
	
; Pas un script NSIS ou AutoHotkey ou AutoIt3
	MessageBox MB_OK|MB_ICONEXCLAMATION `$ScriptFileName :${WRONGTYPE}`
		Abort

	CompileNSI:
; Compilation basique, le résultat apparait dans la fenêtre de compilation (CMD)
		StrCpy $ExecString '"$BaseDirectory\NSISPortable\App\NSIS\makensis.exe" /PAUSE "$ScriptFullName"'
		Exec $ExecString
		Goto TheEnd
		
	CompileAhk:
; Grosse saleté en ligne de commande, obligation de créer un fichier log ... qui ne contient pas grand chose
		CreateDirectory "$BaseDirectory\AutoHotkeyPortable\App\AutoHotkey\Logs"
		${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6
		StrCpy $LogFile "$ScriptName_$2$1$0_$4$5$6.log"
		SetOutPath "$BaseDirectory\AutoHotkeyPortable\App\AutoHotkey\Compiler"

; On utilise une icône perso si disponible
		IfFileExists "$ScriptDirectory\$ScriptName.ico" "" FramakeyIcon
			StrCpy $IconFile "$ScriptDirectory\$ScriptName.ico"
			Goto CompileNow
		
; On définit une icône par défaut
	FramakeyIcon:
		StrCpy $IconFile "$EXEDIR\Framakey.ico"
		
; On compile en vérifiant la création du fichier exe
	CompileNow:
		StrCpy $ExecString '"Ahk2exe.exe" /in "$ScriptFullName" /out "$ScriptDirectory\$ScriptName.exe" /icon "$IconFile"'
		ExecWait 'CMD /C "$ExecString" >..\Logs\$LogFile'
		IfFileExists "$ScriptDirectory\$ScriptName.exe" TheEnd ""
			MessageBox MB_YESNO|MB_ICONQUESTION `$ScriptFileName :${ERRORCOMPILE}` IDNO TheEnd
			IfFileExists "..\Logs\$LogFile" "" Nolog
				Exec '"$WINDIR\notepad.exe" "..\Logs\$LogFile"'
			Goto TheEnd
	
; Test si AutoIT3 est installé en local (utilisation en priorité)
	TestLocalAu3:
		IfFileExists "$PROGRAMFILES\AutoIt3\Aut2exe\Aut2Exe.exe" "" TestPortableAu3
			StrCpy $ExecString '"$PROGRAMFILES\AutoIt3\Aut2Exe\Aut2Exe.exe" /in "$ScriptFullName"'
			Goto CompileAu3

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

; 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 compile le script au3 sans vérification
	CompileAu3:
		Exec $ExecString
		Goto TheEnd
	
	Nolog:
		MessageBox MB_OK|MB_ICONSTOP `$ScriptFileName :${NOLOG}`
		
	TheEnd:
		StrCmp $LogMsg "true" "" TheRealEnd
		MessageBox MB_OK|MB_TOPMOST|MB_ICONINFORMATION `$ScriptFileName :${ENDCOMPILE}`
		
	TheRealEnd:
		
SectionEnd

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