; ----------------------------------------------------
; Portable Freemind
; ----------------------------------------------------
; Par Cyrille Largillier en utilisant le script de Sarkos adapté par Tuxmouraille
; Utilisant comme base de travail le script de John T. Haller
; License : GPL
; Ce script permet de créer Portable Freemind
; Ce script est à compiler avec NSIS de Nullsoft (http://www.nullsoft.com).
; Il nécessite le plugin "ipicsplash" (affichage splashscreen) pour être compilé.

; ----------------------------------------------------
; Général

!define NAME "PortableFreemind"
!define APP "Freemind"
!define VER "0.2.0.0"
!define WEBSITE "http://www.framakey.org"
!define DEFAULTEXE "Freemind.exe"
!define DEFAULTAPPDIR "freemind"
!define DEFAULTPROFILE "profile"


; Nom de l'exécutable
  Name "${NAME}"
; Etiquette
  Caption "${NAME} - Portable and Proud"
; 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

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

  Var IniPath
  Var DataDir
  Var AppDirectory
  Var AppExecutable
  Var AdditionalParameters
  Var ProfileDir
  Var DisableSplashScreen
  Var ExecString
  Var Drive
  Var AppScripts

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

  VIProductVersion "${VER}"
  VIAddVersionKey FileDescription "${NAME} for Windows"
  VIAddVersionKey LegalCopyright "GPL"
  VIAddVersionKey Comments "Allows ${APP} to be run from a removable drive.  For additional details, visit ${WEBSITE}"
  VIAddVersionKey CompanyName "by Framasoft"
  VIAddVersionKey OriginalFilename "${NAME}.exe"
  VIAddVersionKey FileVersion "${VER}"

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

Section "Main"

; Trouver le fichier INI, s'il y en a un
  IfFileExists "$EXEDIR\${NAME}.ini" "" CheckDataINI
    StrCpy "$IniPath" "$EXEDIR"
    Goto ReadINI

  CheckDataINI:
    Push "$EXEDIR"
    Call GetRoot
    Pop $0
    StrCpy "$DataDir" "$0\Data\AppsData"
    IfFileExists "$DataDir\${APP}\${NAME}.ini" ""  NoINI
      StrCpy "$IniPath" "$DataDir\${APP}"
      Goto ReadINI

  ReadINI:
; Lit les paramètres du fichier INI
    ReadINIStr $0 "$IniPath\${NAME}.ini" "${NAME}" "${APP}Directory"
      StrCpy "$AppDirectory" "$EXEDIR\$0"

; Vérifie que les paramètres requis ci-dessus sont présents
    IfErrors NoINI

    ReadINIStr $0 "$IniPath\${NAME}.ini" "${NAME}" "AdditionalParameters"
      StrCpy "$AdditionalParameters" $0
    ReadINIStr $0 "$IniPath\${NAME}.ini" "${NAME}" "${APP}Executable"
      StrCpy "$AppExecutable" $0
    ReadINIStr $0 "$IniPath\${NAME}.ini" "${NAME}" "${APP}Scripts"
      StrCpy "$AppScripts" $0
    ReadINIStr $0 "$IniPath\${NAME}.ini" "${NAME}" "${APP}Profile"
      StrCpy "$ProfileDir" $0
    ReadINIStr $0 "$IniPath\${NAME}.ini" "SplashScreen" "DisableSplashScreen"
      StrCpy "$DisableSplashScreen" $0

; Pour n'importe quelle paramètre du fichier INI non requis contenant une chaine vide, ignorer les erreurs associées
    ClearErrors

; Correct PROGRAMEXECUTABLE si paramètre vide
    StrCmp $AppExecutable "" "" EndINI
    StrCpy "$AppExecutable" "${DEFAULTEXE}"
    Goto EndINI

  NoINI:
; Pas de fichier INI, nous utiliserons les paramètres par défaut
    StrCpy "$AdditionalParameters" ""
    StrCpy "$AppExecutable" "${DEFAULTEXE}"
 ;   StrCpy "$AppScripts" "$EXEDIR\${DEFAULTSCRIPT}"
    StrCpy "$ProfileDir" "$EXEDIR\${DEFAULTPROFILE}"
    StrCpy "$DisableSplashScreen" "false"
   

    IfFileExists "$EXEDIR\${DEFAULTAPPDIR}\${DEFAULTEXE}" "" NoProgramEXE
      StrCpy "$AppDirectory" "$EXEDIR\${DEFAULTAPPDIR}"
      Goto EndINI

  EndINI:
    IfFileExists "$AppDirectory\$AppExecutable" FoundProgramEXE

  NoProgramEXE:
; Si le chemin pointant vers l'exécutable est invalide
    MessageBox MB_OK|MB_ICONEXCLAMATION `$AppDirectory\$AppExecutable was not found.  Please check your configuration`
    Abort

  FoundProgramEXE:
    StrCmp $DisableSplashScreen "true" GetAppParameters
; Affiche le splash screen
    InitPluginsDir
    File /oname=$PLUGINSDIR\splash.jpg "${NAME}.jpg"
    ipicsplash::show 3000 600 400 0x04025C "$PLUGINSDIR\splash.jpg"

; Check des paramètres
  GetAppParameters:
    Call GetParameters
    Pop $0

; Routine si pas de paramètres

  StrCmp "'$0'" "''" "" ExecWithParameters
  StrCpy $ExecString `"$AppDirectory\$AppExecutable"`
  Goto AdditionalParameters

; Routine si paramètres
  ExecWithParameters:
    StrCpy $ExecString `"$AppDirectory\$AppExecutable" $0`
    Goto ExecApp

  AdditionalParameters:
    StrCmp $AdditionalParameters "" ProfileEnvironment

; Envoi des Paramètres additionnels
    StrCpy $ExecString `$ExecString $AdditionalParameters`

; Fake home directory
  ProfileEnvironment:
; Set the %HOMEDRIVE%%HOMEPATH% directory if we have a path
    StrCmp $ProfileDir "" ExecApp
    IfFileExists "$ProfileDir\*.*" "" ExecApp
	Push "$EXEDIR"
	Call GetRoot
	Pop "$Drive"
	
	StrCpy "$ProfileDir" "$ProfileDir" "" 2
	
	System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("HOMEDRIVE", "$Drive").r0'
	
	System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("HOMEPATH", "$ProfileDir").r0'

; Lancement Application
  ExecApp:
	SetOutPath "$EXEDIR\freemind"
	Exec $ExecString

SectionEnd

Section .onInit
  Call Mutex
SectionEnd

; -------------------------------------------
; Obtenir les paramètres
; -------------------------------------------
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

; -------------------------------------------
; Obtenir le chemin root
; Auteur : KiCHiK
; Source : http://nsis.sourceforge.net/Get_the_Root_Directory
; -------------------------------------------
Function GetRoot
  Exch $0
  Push $1
  Push $2
  Push $3
  Push $4

  StrCpy $1 $0 2
  StrCmp $1 "\\" UNC
    StrCpy $0 $1
    Goto done

UNC:
  StrCpy $2 3
  StrLen $3 $0
  loop:
    IntCmp $2 $3 "" "" loopend
    StrCpy $1 $0 1 $2
    IntOp $2 $2 + 1
    StrCmp $1 "\" loopend loop
  loopend:
    StrCmp $4 "1" +3
      StrCpy $4 1
      Goto loop
    IntOp $2 $2 - 1
    StrCpy $0 $0 $2

done:
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Exch $0
FunctionEnd

; -------------------------------------------
; Prévenir plusieurs Instances
; Sources : http://www.veekee.net/wikinsis/docs/AppendiceC.html#C.11
; -------------------------------------------
  Function Mutex
  System::Call 'kernel32::CreateMutexA(i 0, i 0, t "myMutex") i .r1 ?e'
  Pop $R0

  StrCmp $R0 0 +3
   MessageBox MB_OK|MB_ICONEXCLAMATION "${NAME} est déjà lancé."
   Abort
  FunctionEnd

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

; Fin du script