; ----------------------------------------------------
; Portable Sunbird
; ----------------------------------------------------
; Par sarkos
; Utilisant comme base de travail le script de John T. Haller
; License : GPL
; Ce script permet de créer Portable Sunbird.
; Ce script est à compiler avec NSIS de Nullsoft (http://www.nullsoft.com).
; Il nécessite le plugin "ipicsplash" (affichage splashscreen) pour être compilé.
; $id=PortableSunbird.nsi $date=2006-02-18
; ----------------------------------------------------


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

; Général

!define NAME "PortableSunbird"
!define APP "Sunbird"
!define VER "1.1.0"
!define WEBSITE "johnhaller.com/jh/mozilla/portable_sunbird"
!define DEFAULTEXE "sunbird.exe"
!define DEFAULTAPPDIR "sunbird"

;=== Program Details
Name "${NAME}"
OutFile "${NAME}.exe"
Caption "${NAME} - Portable and Proud"
VIProductVersion "${VER}.0"
VIAddVersionKey FileDescription "${NAME} Launcher runs ${APP} from a removable drive"
VIAddVersionKey LegalCopyright "GPL"
VIAddVersionKey Comments "This launcher allows ${APP} to be run from a removable drive.  For additional details, visit ${WEBSITE}"
VIAddVersionKey CompanyName "by John T. Haller et al"
VIAddVersionKey OriginalFilename "${NAME}.exe"
VIAddVersionKey FileVersion "${VER}"

;=== Runtime switches
CRCCheck On
WindowIcon Off
SilentInstall Silent
AutoCloseWindow True

;=== Program Icon
Icon "${APP}.ico"

;=== Path variables
Var PROGRAMDIRECTORY
Var PROFILEDIRECTORY
Var PLUGINSDIRECTORY
Var ADDITIONALPARAMETERS
Var ALLOWMULTIPLEINSTANCES
Var SKIPCHROMEFIX
Var SKIPCOMPREGFIX
Var EXECSTRING
Var WAITFORPROGRAM
Var PROGRAMEXECUTABLE
Var INIPATH
Var DISABLESPLASHSCREEN


Section "Main"
	;=== Get the path information from the INI file
	IfFileExists "$EXEDIR\${NAME}.ini" FoundStandardINI CheckSubINI

	FoundStandardINI:
		StrCpy "$INIPATH" "$EXEDIR\"
		Goto ReadINI

	CheckSubINI:
		IfFileExists "$EXEDIR\${NAME}\${NAME}.ini" FoundSubINI CheckSubSubINI

	FoundSubINI:
		StrCpy "$INIPATH" "$EXEDIR\${NAME}\"
		Goto ReadINI

	CheckSubSubINI:
		IfFileExists "$EXEDIR\PortableApps\${NAME}\${NAME}.ini" FoundSubSubINI CheckPortableAppsINI

	FoundSubSubINI:
		StrCpy "$INIPATH" "$EXEDIR\PortableApps\${NAME}\"
		Goto ReadINI

	CheckPortableAppsINI:
		IfFileExists "$EXEDIR\Data\${NAME}\${NAME}.ini" FoundPortableAppsINI NoINI

	FoundPortableAppsINI:
		StrCpy "$INIPATH" "$EXEDIR\Data\${NAME}\"
		Goto ReadINI

	ReadINI:
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "${APP}Directory"
		StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\$0"
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "ProfileDirectory"
		StrCpy "$PROFILEDIRECTORY" "$EXEDIR\$0"
		;=== check if a required INI parameter is missing
		IfErrors NoINI

		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "PluginsDirectory"
		StrCpy "$PLUGINSDIRECTORY" "$EXEDIR\$0"
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "AdditionalParameters"
		StrCpy "$ADDITIONALPARAMETERS" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "AllowMultipleInstances"
		StrCpy "$ALLOWMULTIPLEINSTANCES" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "SkipChromeFix"
		StrCpy "$SKIPCHROMEFIX" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "SkipCompregFix"
		StrCpy "$SKIPCOMPREGFIX" $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" "SplashScreen" "DisableSplashScreen"
                StrCpy "$DISABLESPLASHSCREEN" $0

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

		;=== Correct PROGRAMEXECUTABLE if blank
		StrCmp $PROGRAMEXECUTABLE "" NoProgramExecutable GoodProgramExecutable

			NoProgramExecutable:
				StrCpy "$PROGRAMEXECUTABLE" "${DEFAULTEXE}"

			GoodProgramExecutable:
		Goto EndINI

	NoINI:
		;=== No INI file, so we'll use the defaults

		;=== defaults common to all file locations
		StrCpy "$ADDITIONALPARAMETERS" ""
		StrCpy "$ALLOWMULTIPLEINSTANCES" "false"
		StrCpy "$SKIPCHROMEFIX" "false"
		StrCpy "$SKIPCOMPREGFIX" "false"
		StrCpy "$WAITFORPROGRAM" "false"
		StrCpy "$PROGRAMEXECUTABLE" "${DEFAULTEXE}"
		StrCpy "$DISABLESPLASHSCREEN" "false"

		IfFileExists "$EXEDIR\${DEFAULTAPPDIR}\${DEFAULTEXE}" FoundProgramDIR CheckPortableProgramDIR

		FoundProgramDIR:
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\${DEFAULTAPPDIR}"
			StrCpy "$PROFILEDIRECTORY" "$EXEDIR\profile"
			StrCpy "$PLUGINSDIRECTORY" "$EXEDIR\plugins"
			GoTo EndINI

		CheckPortableProgramDIR:
			IfFileExists "$EXEDIR\${NAME}\${DEFAULTAPPDIR}\${DEFAULTEXE}" FoundPortableProgramDIR CheckPortableAppsDIR

		FoundPortableProgramDIR:
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\${NAME}\${DEFAULTAPPDIR}"
			StrCpy "$PROFILEDIRECTORY" "$EXEDIR\${NAME}\profile"
			StrCpy "$PLUGINSDIRECTORY" "$EXEDIR\${NAME}\plugins"
			GoTo EndINI

		CheckPortableAppsDIR:
			IfFileExists "$EXEDIR\PortableApps\${NAME}\${DEFAULTAPPDIR}\${DEFAULTEXE}" FoundPortableAppsDIR CheckPortableAppsSplitDIR

		FoundPortableAppsDIR:
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\PortableApps\${NAME}\${DEFAULTAPPDIR}"
			StrCpy "$PROFILEDIRECTORY" "$EXEDIR\PortableApps\${NAME}\profile"
			StrCpy "$PLUGINSDIRECTORY" "$EXEDIR\PortableApps\${NAME}\plugins"
			GoTo EndINI

		CheckPortableAppsSplitDIR:
			IfFileExists "$EXEDIR\Apps\${NAME}\${DEFAULTAPPDIR}\${DEFAULTEXE}" FoundPortableAppsSplitDIR NoProgramEXE

		FoundPortableAppsSplitDIR:
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\Apps\${NAME}\${DEFAULTAPPDIR}"
			StrCpy "$PROFILEDIRECTORY" "$EXEDIR\Data\${NAME}\profile"
			StrCpy "$PLUGINSDIRECTORY" "$EXEDIR\Apps\${NAME}\plugins"
			GoTo EndINI	

	EndINI:
		IfFileExists "$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" FoundProgramEXE

	NoProgramEXE:
		;=== exe was not found
		MessageBox MB_OK|MB_ICONEXCLAMATION `$PROGRAMEXECUTABLE was not found.  Please check your configuration`
		Abort
		
	FoundProgramEXE:

	;=== Check for an existing profile
	IfFileExists "$PROFILEDIRECTORY\prefs.js" ProfileFound
	
	;=== No profile was found
	MessageBox MB_YESNO|MB_ICONQUESTION `No profile was found. Would you like to create a new profile here? $\n$PROFILEDIRECTORY\` IDYES CreateProfile
	MessageBox MB_OK|MB_ICONINFORMATION `${NAME} can not run without a profile directory.  Please check your configuration.`
	Abort
	
	CreateProfile:
		IfFileExists "$PROFILEDIRECTORY\*.*" ProfileFound LaunchProgram
		;=== create a new profile directory
		;CreateDirectory "$PROFILEDIRECTORY\"

	ProfileFound:
		IfFileExists "$PROFILEDIRECTORY\chrome\chrome.rdf" ChromeFound FixCalendarManager
	
	ChromeFound:
		StrCmp $SKIPCHROMEFIX "true" RunProgram
		FileOpen $0 "$PROFILEDIRECTORY\chrome\chrome.rdf" r
		FileOpen $R0 "$PROFILEDIRECTORY\chrome\chrome.rdf.new" w
		ClearErrors ; if there's an error, we're done with the file
	
	NextLine:
		FileWrite $R0 $4
		FileRead $0 $4
		IfErrors NoMoreLines ;== we've reached the end of the file
		StrCpy $5 $4 34
		StrCmp $5 `                   c:baseURL="jar:` "" NextLine
		StrCpy $R4 40
	
	NotYet:
		IntOp $R4 $R4 + 1
		StrCpy $7 $4 10 $R4 ; looking for the point to strip the extension path
		StrCmp $7 "extensions" PathFound NotYet
	
	PathFound:
		StrCpy $5 $4 "" $R4
		StrCpy $4 `                   c:baseURL="jar:file:///$PROFILEDIRECTORY/$5` ; the path made ABSOLUTE
		GoTo NextLine

	NoMoreLines:
		FileClose $0
		FileClose $R0
		;=== Backup the chrome.rdf just in case
		CopyFiles "$PROFILEDIRECTORY\chrome\chrome.rdf" "$PROFILEDIRECTORY\chrome\chrome.rdf.old" 		;=== Save the new chrome.rdf
		CopyFiles "$PROFILEDIRECTORY\chrome\chrome.rdf.new" "$PROFILEDIRECTORY\chrome\chrome.rdf" 

	FixCalendarManager:
		IfFileExists "$PROFILEDIRECTORY\Calendar\CalendarManager.rdf" CalendarManagerFound RunProgram

	CalendarManagerFound:
		FileOpen $0 "$PROFILEDIRECTORY\Calendar\CalendarManager.rdf" r
		FileOpen $R0 "$PROFILEDIRECTORY\Calendar\CalendarManager.rdf.new" w
		ClearErrors ; if there's an error, we're done with the file
	
	CMNextLine:
		FileWrite $R0 $4
		FileRead $0 $4
		IfErrors CMNoMoreLines ;== we've reached the end of the file
		StrCpy $5 $4 28
		StrCmp $5 `                   NC:path="` "" CMNextLine
		StrCpy $R4 40
	
	CMNotYet:
		IntOp $R4 $R4 + 1
		StrCpy $7 $4 9 $R4 ; looking for the point to strip the calendar path
		StrCmp $7 "Calendar\" CMPathFound CMNotYet
	
	CMPathFound:
		StrCpy $5 $4 "" $R4
		StrCpy $4 `                   NC:path="$PROFILEDIRECTORY\$5` ; the path made ABSOLUTE
		GoTo CMNextLine

	CMNoMoreLines:
		FileClose $0
		FileClose $R0
		;=== Backup the Calendar\CalendarManager.rdf just in case
		CopyFiles "$PROFILEDIRECTORY\Calendar\CalendarManager.rdf" "$PROFILEDIRECTORY\Calendar\CalendarManager.rdf.old"
		CopyFiles "$PROFILEDIRECTORY\Calendar\CalendarManager.rdf.new" "$PROFILEDIRECTORY\Calendar\CalendarManager.rdf" 

	RunProgram:
		StrCmp $SKIPCOMPREGFIX "true" GetPassedParameters

		;=== Delete component registry to ensure compatibility with all extensions
		Delete $PROFILEDIRECTORY\compreg.dat

	GetPassedParameters:
		;=== Get any passed parameters
		Call GetParameters
		Pop $0
		StrCmp "'$0'" "''" LaunchProgram LaunchProgramParameters

	LaunchProgram:
		StrCpy $EXECSTRING `"$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" -profile "$PROFILEDIRECTORY"`
		Goto CheckMultipleInstances

	LaunchProgramParameters:	
		StrCpy $EXECSTRING `"$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" -profile "$PROFILEDIRECTORY" $0`

	CheckMultipleInstances:
		StrCmp $ALLOWMULTIPLEINSTANCES "true" MultipleInstances NoMultiple

	MultipleInstances:
		System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("MOZ_NO_REMOTE", "1").r0'

	NoMultiple:
		StrCmp $ADDITIONALPARAMETERS "" NoParameters AdditionalParameters

	AdditionalParameters:
		StrCpy $EXECSTRING `$EXECSTRING $ADDITIONALPARAMETERS`

	NoParameters:
		;=== Set the plugins directory if we have a path
		StrCmp $PLUGINSDIRECTORY "" DisplaySplashScreen
		System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("MOZ_PLUGIN_PATH", "$PLUGINSDIRECTORY").r0'
; -------------------------------------------------
; Ajouté par nos soins pour l'affichage d'un splash
  DisplaySplashScreen:
    StrCmp $DisableSplashScreen "true" LaunchNow
; Affiche le splash screen
    InitPluginsDir
    File /oname=$PLUGINSDIR\splash.jpg "${NAME}.jpg"
    ipicsplash::show 3000 600 400 0x04025C "$PLUGINSDIR\splash.jpg"
; -------------------------------------------------

	LaunchNow:
		StrCmp $WAITFORPROGRAM "true" LaunchAndWait LaunchAndClose

	LaunchAndWait:
		ExecWait $EXECSTRING
		GoTo TheEnd

	LaunchAndClose:
		Exec $EXECSTRING

	TheEnd:

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