;Copyright (C) 2004-2007 John T. Haller of PortableApps.com

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

;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 "On-ScreenKeyboardPortable"
!define FULLNAME "On-Screen Keyboard Portable"
!define APP "On-Screen Keyboard"
!define VER "1.4.1.0"
!define WEBSITE "PortableApps.com/OSKPortable"

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

;=== Includes
!include "Registry.nsh"

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

Var DISABLESPLASHSCREEN
Var FAILEDTORESTOREKEY

Section "Main"
	;=== Check if already running
	System::Call 'kernel32::CreateMutexA(i 0, i 0, t "${NAME}") i .r1 ?e'
	Pop $0
	StrCmp $0 0 CheckForEXE
		MessageBox MB_OK|MB_ICONEXCLAMATION `${FULLNAME} is already running.`
		Abort

	CheckForEXE:
		IfFileExists "$WINDIR\system32\osk.exe" FoundProgramEXE
		IfFileExists "$EXEDIR\App\osk\On-ScreenKeyboard.exe" FoundProgramEXE

	;NoProgramEXE:
		;=== Program executable not where expected
		MessageBox MB_OK|MB_ICONEXCLAMATION `$EXEDIR\App\On-ScreenKeyboard.exe was not found.  Please check your configuration`
		Abort
		
	FoundProgramEXE:
		ReadINIStr $DISABLESPLASHSCREEN "$EXEDIR\${NAME}.ini" "${NAME}" "DisableSplashScreen"
		ClearErrors
		;=== Check if running
		FindProcDLL::FindProc "osk.exe"
		StrCmp $R0 "1" WarnAnotherInstance DisplaySplash

	WarnAnotherInstance:
		MessageBox MB_OK|MB_ICONINFORMATION `Another instance of ${APP} is already running.  Please close other instances of ${APP} before launching ${FULLNAME}.`
		Abort
	
	DisplaySplash:
		StrCmp $DISABLESPLASHSCREEN "true" SettingsDirectory
			;=== Show the splash screen while processing registry entries
			InitPluginsDir
			File /oname=$PLUGINSDIR\splash.jpg "${NAME}.jpg"
			newadvsplash::show /NOUNLOAD 1300 200 0 -1 /L $PLUGINSDIR\splash.jpg
	
	SettingsDirectory:
		;=== Set the settings directory if we have a path
		IfFileExists "$EXEDIR\Data\setttings\*.*" "" OSKRegistryBackup
			CreateDirectory "$EXEDIR\Data\setttings"
	
	OSKRegistryBackup:
		IfFileExists "$WINDIR\system32\osk.exe" "" Windows9xLaunch
		;=== Backup the registry
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\Microsoft\Osk-BackupByOSKPortable" $R0
		StrCmp $R0 "0" MSSwitchRegistryBackup
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\Microsoft\Osk" $R0
		StrCmp $R0 "-1" MSSwitchRegistryBackup
		${registry::MoveKey} "HKEY_CURRENT_USER\Software\Microsoft\Osk" "HKEY_CURRENT_USER\Software\Microsoft\Osk-BackupByOSKPortable" $R0
	
	MSSwitchRegistryBackup:
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\Microsoft\MS Switch-BackupByOSKPortable" $R0
		StrCmp $R0 "0" RestoreTheKey
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\Microsoft\MS Switch" $R0
		StrCmp $R0 "-1" RestoreTheKey
		${registry::MoveKey} "HKEY_CURRENT_USER\Software\Microsoft\MS Switch" "HKEY_CURRENT_USER\Software\Microsoft\MS Switch-BackupByOSKPortable" $R0
		Sleep 200

	RestoreTheKey:
		IfFileExists "$EXEDIR\Data\settings\osk.reg" "" LaunchOsk
		${registry::RestoreKey} "$EXEDIR\Data\settings\osk.reg" $R0
		StrCmp $R0 '0' LaunchOsk ;successfully restored key
		nsExec::ExecToStack `"$WINDIR\system32\reg.exe" import "$EXEDIR\Data\settings\osk.reg"`
		Pop $R0
		StrCmp $R0 '0' LaunchOsk ;successfully restored key
		StrCpy $FAILEDTORESTOREKEY "true"
	
	LaunchOsk:
		ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "CurrentVersion"
		StrCmp $R0 '6.0' LaunchOskVista
		Sleep 200
		ExecWait `$WINDIR\system32\osk.exe`
		Goto CleanupTime

	LaunchOskVista:
		Sleep 200
		nsExec::Exec `$EXEDIR\App\osk\osk.bat`
	
	CleanupTime:
		StrCmp $FAILEDTORESTOREKEY "true" SetOriginalKeyBack
		Sleep 200
		${registry::SaveKey} "HKEY_CURRENT_USER\Software\Microsoft\Osk" "$EXEDIR\Data\settings\osk.reg" "" $0
		Sleep 200
	
	SetOriginalKeyBack:
		${registry::DeleteKey} "HKEY_CURRENT_USER\Software\Microsoft\Osk" $R0
		Sleep 200
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\Microsoft\Osk-BackupByOSKPortable" $R0
		StrCmp $R0 "-1" SetSecondKeyBack
		${registry::MoveKey} "HKEY_CURRENT_USER\Software\Microsoft\Osk-BackupByOSKPortable" "HKEY_CURRENT_USER\Software\Microsoft\Osk" $R0
		Sleep 200

	SetSecondKeyBack:
		${registry::DeleteKey} "HKEY_CURRENT_USER\Software\Microsoft\MS Switch" $R0
		Sleep 200
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\Microsoft\MS Switch-BackupByOSKPortable" $R0
		StrCmp $R0 "-1" TheEnd
		${registry::MoveKey} "HKEY_CURRENT_USER\Software\Microsoft\MS Switch-BackupByOSKPortable" "HKEY_CURRENT_USER\Software\Microsoft\MS Switch" $R0
		Goto TheEnd
	
	Windows9xLaunch:
		ExecWait "$EXEDIR\App\osk\On-ScreenKeyboard.exe"

	TheEnd:
		${registry::Unload}
		newadvsplash::wait
SectionEnd