;Copyright (C) 2004-2008 John T. Haller

;Website: http://PortableApps.com/PokerTHPortable

;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 PORTABLEAPPNAME "PokerTH Portable"
!define NAME "PokerTHPortable"
!define APPNAME "PokerTH"
!define VER "1.5.1.0"
!define WEBSITE "PortableApps.com/PokerTHPortable"
!define DEFAULTEXE "PokerTH.exe"
!define DEFAULTAPPDIR "pokerth"
!define DEFAULTSETTINGSDIR "settings"

;=== Program Details
Name "${PORTABLEAPPNAME}"
OutFile "..\..\${NAME}.exe"
Caption "${PORTABLEAPPNAME} | PortableApps.com"
VIProductVersion "${VER}"
VIAddVersionKey ProductName "${PORTABLEAPPNAME}"
VIAddVersionKey Comments "Allows ${APPNAME} to be run from a removable drive.  For additional details, visit ${WEBSITE}"
VIAddVersionKey CompanyName "PortableApps.com"
VIAddVersionKey LegalCopyright "John T. Haller"
VIAddVersionKey FileDescription "${PORTABLEAPPNAME}"
VIAddVersionKey FileVersion "${VER}"
VIAddVersionKey ProductVersion "${VER}"
VIAddVersionKey InternalName "${PORTABLEAPPNAME}"
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

; Best Compression
SetCompress Auto
SetCompressor /SOLID lzma
SetCompressorDictSize 32
SetDatablockOptimize On

;=== Include
!include "GetParameters.nsh"
!include "TextFunc.nsh"
!insertmacro ConfigRead
!insertmacro ConfigWrite
;!include "WordFunc.nsh"
;!insertmacro StrFilter
!include "FileFunc.nsh"
!insertmacro GetRoot
!include "MUI.nsh"

;=== Program Icon
Icon "..\..\App\AppInfo\appicon.ico"

;=== Icon & Stye ===
!define MUI_ICON "..\..\App\AppInfo\appicon.ico"

;=== Languages
!insertmacro MUI_LANGUAGE "English"

LangString LauncherFileNotFound ${LANG_ENGLISH} "${PORTABLEAPPNAME} cannot be started. You may wish to re-install to fix this issue. (ERROR: $MISSINGFILEORPATH could not be found)"
LangString LauncherAlreadyRunning ${LANG_ENGLISH} "Another instance of ${APPNAME} is already running. Please close other instances of ${APPNAME} before launching ${PORTABLEAPPNAME}."
LangString LauncherAskCopyLocal ${LANG_ENGLISH} "${PORTABLEAPPNAME} 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$\nPrivacy Note: If you say Yes, your personal data within ${PORTABLEAPPNAME} will be temporarily copied to a local drive. Although this copy of your data will be deleted when you close ${PORTABLEAPPNAME}, it may be possible for someone else to access your data later."
LangString LauncherNoReadOnly ${LANG_ENGLISH} "${PORTABLEAPPNAME} can not run directly from a read-only location and will now close."


Var PROGRAMDIRECTORY
Var SETTINGSDIRECTORY
Var ADDITIONALPARAMETERS
Var EXECSTRING
Var WAITFORPROGRAM
Var PROGRAMEXECUTABLE
Var INIPATH
Var DISABLESPLASHSCREEN
Var ISDEFAULTDIRECTORY
Var DRIVEROOT
Var MISSINGFILEORPATH
Var APPLANGUAGE


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}" "${APPNAME}Directory"
		StrCpy "$PROGRAMDIRECTORY" "$EXEDIR\$0"
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "SettingsDirectory"
		StrCpy "$SETTINGSDIRECTORY" "$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}" "WaitFor${APPNAME}"
		StrCpy "$WAITFORPROGRAM" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "${APPNAME}Executable"
		StrCpy "$PROGRAMEXECUTABLE" $0
		ReadINIStr $0 "$INIPATH\${NAME}.ini" "${NAME}" "DisableSplashScreen"
		StrCpy "$DISABLESPLASHSCREEN" $0
	
	;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 "$WAITFORPROGRAM" "false"
		StrCpy "$PROGRAMEXECUTABLE" "${DEFAULTEXE}"
		StrCpy "$DISABLESPLASHSCREEN" "false"

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

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

	EndINI:
		IfFileExists "$PROGRAMDIRECTORY\$PROGRAMEXECUTABLE" FoundProgramEXE

	NoProgramEXE:
		;=== Program executable not where expected
		StrCpy $MISSINGFILEORPATH $PROGRAMEXECUTABLE
		MessageBox MB_OK|MB_ICONEXCLAMATION `$(LauncherFileNotFound)`
		Abort
		
	FoundProgramEXE:
		IfFileExists "$SETTINGSDIRECTORY\pokerth\config.xml" SettingsFound
			;=== No profile was found
			StrCmp $ISDEFAULTDIRECTORY "true" CopyDefaultSettings
			StrCpy $MISSINGFILEORPATH $SETTINGSDIRECTORY
			MessageBox MB_OK|MB_ICONEXCLAMATION `$(LauncherFileNotFound)`
			Abort
	
	CopyDefaultSettings:
		CreateDirectory "$EXEDIR\Data\${DEFAULTSETTINGSDIR}"
		CreateDirectory "$EXEDIR\Data\${DEFAULTSETTINGSDIR}\pokerth"
		CreateDirectory "$EXEDIR\Data\${DEFAULTSETTINGSDIR}\pokerth\log-files\"
		CreateDirectory "$EXEDIR\Data\${DEFAULTSETTINGSDIR}\pokerth\data\"
		CreateDirectory "$EXEDIR\Data\${DEFAULTSETTINGSDIR}\pokerth\cache\"
		StrCmp "$ISDEFAULTDIRECTORY" "true" "" SettingsFound
			CopyFiles "$EXEDIR\App\DefaultData\settings\pokerth\config.xml" "$EXEDIR\Data\${DEFAULTSETTINGSDIR}\pokerth"

	SettingsFound:
		StrCmp $DISABLESPLASHSCREEN "true" AdjustSettings
			;=== Show the splash screen before processing the files
			InitPluginsDir
			File /oname=$PLUGINSDIR\splash.jpg "${NAME}.jpg"
			newadvsplash::show /NOUNLOAD 1200 0 0 -1 /L $PLUGINSDIR\splash.jpg
	
	AdjustSettings:
		IfFileExists "$SETTINGSDIRECTORY\pokerth\config.xml" "" GetPassedParameters
			${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <LogDir value="` `$SETTINGSDIRECTORY\pokerth\log-files\" />` $R0
			${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <CacheDir value="` `$SETTINGSDIRECTORY\pokerth\cache\" />` $R0
			
			;GetAppLanguage:
				ReadEnvStr $APPLANGUAGE "PortableApps.comLocaleCode2"
				StrCmp $APPLANGUAGE "" AdjustDataDir ;if not set, move on
				;${StrFilter} $APPLANGUAGE "" "" "-" $0 ;remove dash if present
				;StrCpy $APPLANGUAGE $0
				${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Language value="` $0
				StrCpy $1 $0 2 ;get the first 2 characters
				StrCmp `$APPLANGUAGE` $1 AdjustDataDir ;if the same, move on
				StrCmp $APPLANGUAGE "en" DefaultToEnglish ;english is the default and not an individual file, just set it
				IfFileExists "$PROGRAMDIRECTORY\data\translations\pokerth_$APPLANGUAGE*.qm" SetAppLanguage AdjustDataDir

			SetAppLanguage:
				StrCmp $APPLANGUAGE "pt" "" CheckForChinese
				StrCpy $APPLANGUAGE "ptbr"
				Goto WriteAppLanguageToConfig
			CheckForChinese:
				StrCmp $APPLANGUAGE "zh" "" WriteAppLanguageToConfig
				StrCpy $APPLANGUAGE "zhcn"
				Goto WriteAppLanguageToConfig

			DefaultToEnglish:
				StrCpy $APPLANGUAGE "en_US"
				
			WriteAppLanguageToConfig:
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Language value="` `$APPLANGUAGE" />` $R0
			
			AdjustDataDir:
			${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <UserDataDir value="` `$SETTINGSDIRECTORY\pokerth\data\" />` $R0
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <AppDataDir value="` $0
			StrCmp $0 "" OldDataDir
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <AppDataDir value="` `$SETTINGSDIRECTORY\pokerth\data\" />` $R0
				Goto AdjustMyAvatar

			OldDataDir:
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <DataDir value="` `$SETTINGSDIRECTORY\pokerth\data\" />` $R0
				Goto AdjustMyAvatar				

			AdjustMyAvatar:
			${GetRoot} $EXEDIR $DRIVEROOT
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <MyAvatar value="` $0
			StrCmp $0 `" />` AdjustAvatar1
				StrCpy $1 $0 1
				StrCmp $1 ":" AdjustAvatar1
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <MyAvatar value="` `$0` $R0
		AdjustAvatar1:
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent1Avatar value="` $0
			StrCmp $0 `" />` AdjustAvatar2
				StrCpy $1 $0 1
				StrCmp $1 ":" AdjustAvatar2
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent1Avatar value="` `$0` $R0
		AdjustAvatar2:
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent2Avatar value="` $0
			StrCmp $0 `" />` AdjustAvatar3
				StrCpy $1 $0 1
				StrCmp $1 ":" AdjustAvatar3
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent2Avatar value="` `$0` $R0
		AdjustAvatar3:
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent3Avatar value="` $0
			StrCmp $0 `" />` AdjustAvatar4
				StrCpy $1 $0 1
				StrCmp $1 ":" AdjustAvatar4
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent3Avatar value="` `$0` $R0
		AdjustAvatar4:
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent4Avatar value="` $0
			StrCmp $0 `" />` AdjustAvatar5
				StrCpy $1 $0 1
				StrCmp $1 ":" AdjustAvatar5
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent4Avatar value="` `$0` $R0
		AdjustAvatar5:
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent5Avatar value="` $0
			StrCmp $0 `" />` AdjustAvatar6
				StrCpy $1 $0 1
				StrCmp $1 ":" AdjustAvatar6
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent5Avatar value="` `$0` $R0
		AdjustAvatar6:
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent6Avatar value="` $0
			StrCmp $0 `" />` FlipSideFile
				StrCpy $1 $0 1
				StrCmp $1 ":" FlipSideFile
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <Opponent6Avatar value="` `$0` $R0
		FlipSideFile:
			${ConfigRead} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <FlipsideOwnFile value="` $0
			StrCmp $0 `" />` GetPassedParameters
				StrCpy $1 $0 1
				StrCmp $1 ":" GetPassedParameters
				StrCpy $1 $0 "" 2
				StrCpy $0 `$DRIVEROOT$1`
				${ConfigWrite} `$SETTINGSDIRECTORY\pokerth\config.xml` `        <FlipsideOwnFile value="` `$0` $R0
	
	GetPassedParameters:
		;=== 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 "" UserProfileEnvironment

		;=== Additional Parameters
		StrCpy $EXECSTRING `$EXECSTRING $ADDITIONALPARAMETERS`

	UserProfileEnvironment:
		;=== Set the %USERPROFILE% directory if we have a path
		;System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("USERPROFILE", "$SETTINGSDIRECTORY").r0'
		System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("APPDATA", "$SETTINGSDIRECTORY").r0'

	;LaunchNow:
		StrCmp $WAITFORPROGRAM "true" LaunchAndWait LaunchAndClose

	LaunchAndWait:
		ExecWait $EXECSTRING
		Goto TheEnd

	LaunchAndClose:
		Exec $EXECSTRING

	TheEnd:
		newadvsplash::STOP /WAIT
SectionEnd