;Copyright (C) 2004-2007 John T. Haller of PortableApps.com
;Additional Ideas from tracon and mai9

;Website: http://portableapps.com/MirandaPortable

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

!define NAME "MirandaPortable"
!define FULLNAME "Miranda Portable"
!define APP "Miranda"
!define VER "1.3.1.0"
!define WEBSITE "PortableApps.com/MirandaPortable"
!define DEFAULTEXE "miranda32.exe"
!define DEFAULTAPPDIR "miranda"
!define DEFAULTPROFILESDIR "profiles"

;=== Program Details
Name "${FULLNAME}"
OutFile "${NAME}.exe"
Caption "${FULLNAME} | PortableApps.com"
VIProductVersion "${VER}"
VIAddVersionKey ProductName "${FULLNAME}"
VIAddVersionKey Comments "Allows ${APP} to be run from a removable drive.  For additional details, visit ${WEBSITE}"
VIAddVersionKey CompanyName "PortableApps.com (John T. Haller)"
VIAddVersionKey LegalCopyright "John T. Haller"
VIAddVersionKey FileDescription "${FULLNAME}"
VIAddVersionKey FileVersion "${VER}"
VIAddVersionKey ProductVersion "${VER}"
VIAddVersionKey InternalName "${FULLNAME}"
VIAddVersionKey LegalTrademarks "PortableApps.com is a Trademark of Rare Ideas, LLC."
VIAddVersionKey OriginalFilename "${NAME}.exe"
;VIAddVersionKey PrivateBuild ""
;VIAddVersionKey SpecialBuild ""

;=== Runtime Switches
CRCCheck On
WindowIcon Off
SilentInstall Silent
AutoCloseWindow True
RequestExecutionLevel user

;=== Include
!include "Attrib.nsh"
!include "GetParameters.nsh"
!include "Registry.nsh"

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

Var PROGRAMDIRECTORY
Var PROFILESDIRECTORY
Var ADDITIONALPARAMETERS
Var EXECSTRING
Var PROGRAMEXECUTABLE
Var INIPATH
Var DISABLESPLASHSCREEN
Var ISDEFAULTDIRECTORY
Var RUNLOCALLY
Var USERTYPE

Section "Main"
	;=== Find the INI file, if there is one
		IfFileExists "$EXEDIR\${NAME}.ini" "" CheckSubINI
			StrCpy "$INIPATH" "$EXEDIR"
			Goto ReadINI

	CheckSubINI:
		IfFileExists "$EXEDIR\${NAME}\${NAME}.ini" "" NoINI
			StrCpy "$INIPATH" "$EXEDIR\${NAME}"
			Goto ReadINI

	ReadINI:
		;=== Read the parameters from the INI file
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "${APP}Directory"
		StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\$0"
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "ProfilesDirectory"
		StrCpy "$PROFILESDIRECTORY" "$EXEDIR\$0"
	

		;=== Check that the above required parameters are present
		IfErrors NoINI

		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "AdditionalParameters"
		StrCpy "$ADDITIONALPARAMETERS" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "DisableSplashScreen"
		StrCpy "$DISABLESPLASHSCREEN" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "RunLocally"
		StrCpy "$RUNLOCALLY" $0
		StrCmp "$RUNLOCALLY" "true" "" CleanUpAnyErrors
		
	CleanUpAnyErrors:
		;=== Any missing unrequired INI entries will be an empty string, ignore associated errors
		ClearErrors

		;=== Correct PROGRAMEXECUTABLE if blank
		StrCmp $PROGRAMEXECUTABLE "" "" EndINI
			StrCpy "$PROGRAMEXECUTABLE" "${DEFAULTEXE}"
			Goto EndINI

	NoINI:
		;=== No INI file, so we'll use the defaults
		StrCpy "$ADDITIONALPARAMETERS" ""
		StrCpy "$PROGRAMEXECUTABLE" "${DEFAULTEXE}"

		IfFileExists "$EXEDIR\App\${DEFAULTAPPDIR}\${DEFAULTEXE}" "" CheckPortableProgramDIR
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\App\${DEFAULTAPPDIR}"
			StrCpy "$PROFILESDIRECTORY" "$EXEDIR\Data\${DEFAULTPROFILESDIR}"
			StrCpy "$ISDEFAULTDIRECTORY" "true"
			GoTo EndINI

		CheckPortableProgramDIR:
			IfFileExists "$EXEDIR\${NAME}\App\${DEFAULTAPPDIR}\${DEFAULTEXE}" "" NoProgramEXE
			StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\${NAME}\App\${DEFAULTAPPDIR}"
			StrCpy "$PROFILESDIRECTORY" "$EXEDIR\${NAME}\Data\${DEFAULTPROFILESDIR}"
			GoTo EndINI

	EndINI:
		IfFileExists "$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" FoundProgramEXE

	NoProgramEXE:
		;=== Program executable not where expected
		MessageBox MB_OK|MB_ICONEXCLAMATION `$PROGRAMEXECUTABLE was not found.  Please check your configuration`
		Abort
		
	FoundProgramEXE:
		StrCmp $RUNLOCALLY "true" DisplaySplash
		ClearErrors
		FileOpen $R0 "$PROFILESDIRECTORY\writetest.temp" w
		IfErrors "" WriteSuccessful
			;== Write failed, so we're read-only
			MessageBox MB_YESNO|MB_ICONQUESTION `${FULLNAME} appears to be running from a location that is read-only.  Would you like to temporarily copy it to the local hard drive and run it from there?$\n$\n(Privacy Note: Any personal data within ${FULLNAME} will be copied to a local drive. Although it will be deleted on exit, it may be possible to recover this data later.)` IDYES SwitchToRunLocally
			MessageBox MB_OK|MB_ICONINFORMATION `${FULLNAME} can not run directly from a read-only location and will now close.`
			Abort
			
	SwitchToRunLocally:
		StrCpy $RUNLOCALLY "true"
		StrCpy "$ISDEFAULTDIRECTORY" "false"
		Goto DisplaySplash
	
	WriteSuccessful:
		FileClose $R0
		Delete "$PROFILESDIRECTORY\writetest.temp"
		
	DisplaySplash:
		StrCmp $DISABLESPLASHSCREEN "true" SkipSplashScreen
			;=== Show the splash screen before processing the files
			InitPluginsDir
			File /oname=$PLUGINSDIR\splash.jpg "${NAME}.jpg"
			newadvsplash::show /NOUNLOAD 1300 200 0 -1 /L $PLUGINSDIR\splash.jpg
	
	SkipSplashScreen:
		;=== Run locally if needed (aka Live)
		StrCmp $RUNLOCALLY "true" "" GetAnyPassParameters
		RMDir /r "$TEMP\${NAME}\"
		CreateDirectory $TEMP\${NAME}\profiles
		CreateDirectory $TEMP\${NAME}\program
		CopyFiles /SILENT $PROFILESDIRECTORY\*.* $TEMP\${NAME}\profiles
		StrCpy $PROFILESDIRECTORY $TEMP\${NAME}\profiles
		CopyFiles /SILENT $PROGRAMDIRECTORY\*.* $TEMP\${NAME}\program
		StrCpy $PROGRAMDIRECTORY $TEMP\${NAME}\program
		Push $TEMP\${NAME}
		Call Attrib
	
	GetAnyPassParameters:
		;=== Get any passed parameters
		Call GetParameters
		Pop $0
		StrCmp "'$0'" "''" "" LaunchProgramParameters

		;=== No parameters
		StrCpy $EXECSTRING `"$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE"`
		Goto AdditionalParameters

	LaunchProgramParameters:
		StrCpy $EXECSTRING `"$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" $0`

	AdditionalParameters:
		StrCmp $ADDITIONALPARAMETERS "" ProfilesDirectory

		;=== Additional Parameters
		StrCpy $EXECSTRING `$EXECSTRING $ADDITIONALPARAMETERS`
	
	ProfilesDirectory:
		;=== Set the settings directory if we have a path
		IfFileExists "$PROFILESDIRECTORY\*.*" "" ProfilesDirectoryNotFound
		StrCmp $ISDEFAULTDIRECTORY "true" SettingsDefault
		WriteINIStr `$PROGRAMDIRECTORY\mirandaboot.ini` "Database" "ProfileDir" "$PROFILESDIRECTORY"
		GoTo LaunchNow
		
	SettingsDefault:
		ReadINIStr $0 "$PROGRAMDIRECTORY\mirandaboot.ini" "Database" "ProfileDir"
		StrCmp $0 "..\..\Data\profiles" RegistryBackup
		WriteINIStr `$PROGRAMDIRECTORY\mirandaboot.ini` "Database" "ProfileDir" "..\..\Data\profiles"

	ProfilesDirectoryNotFound:
		MessageBox MB_OK|MB_ICONEXCLAMATION `$PROFILESDIRECTORY was not found.  Please check your configuration`
		Abort
	
	RegistryBackup:
		;=== Check for registry permissions
		UserInfo::GetAccountType
		Pop $0
		StrCpy $USERTYPE $0
		StrCmp $USERTYPE "Guest" LaunchNow
		StrCmp $USERTYPE "User" LaunchNow
		${registry::KeyExists} "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda-BackupByMirandaPortable" $R0
		StrCmp $R0 "-1" LaunchNow
		${registry::KeyExists} "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda" $R0
		StrCmp $R0 "-1" LaunchNow
		${registry::MoveKey} "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda" "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda-BackupByMirandaPortable" $R0
	
	LaunchNow:	
		ExecWait $EXECSTRING
		
		StrCmp $USERTYPE "Guest" StopSplash
		StrCmp $USERTYPE "User" StopSplash
		${registry::DeleteKey} "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda" $R0
		Sleep 100
		${registry::KeyExists} "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda-BackupByMirandaPortable" $R0
		StrCmp $R0 "-1" StopSplash
		${registry::MoveKey} "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda-BackupByMirandaPortable" "HKEY_LOCAL_MACHINE\SOFTWARE\Miranda" $R0
		Sleep 100
		
	StopSplash:
		RMDir /r "$TEMP\${NAME}"
		newadvsplash::stop
		${registry::Unload}
SectionEnd