; Script
DECLARE_GLOBAL_MACRO(PHal_PDA_FOLDER,"%PROGRAMFILES_FOLDER%\Dolphin\Hal")
DECLARE_GLOBAL_MACRO(PHal_NAME, "Pocket Hal")
DECLARE_GLOBAL_MACRO(PHal_FOLDER, "%THIS_FOLDER%")

DECLARE_GLOBAL_MACRO(PHal_LANG_INSTALL_DESC, "Select this group to install language support for %LANG_00044_DESC%. This Includes default speech and Braille verbosity settings and hot key settings and the default graphics database.")

DECLARE_GROUP("PHal")
{
	SET_DISPLAY_NAME("%PHal_NAME%")
	INSTALL_ALWAYS;

	; If we are repairing then Pocket Hal must be stopped
	if(REPAIRING)
		RUNREMOTEPROCESS(%PHal_PDA_FOLDER%\PHal.exe, -q, TRUE);
		; The interceptor cannot be stopped (except on reboot)
		; so dolpvid.dll may be in use until a reboot happens
		; however the system allows a rename of the in-use file so that a new one can be copied!
		; NOTE: FALSE below means that any dolpvid.dl~ will be deleted before the rename happens
		REN(%WINDOWS_FOLDER%\dolpvid.dll,%WINDOWS_FOLDER%\dolpvid.dl~,FALSE);
		REN(%WINDOWS_FOLDER%\MyToday.dll,%WINDOWS_FOLDER%\MyToday.dl~,FALSE);
	endif

	; Technically this should not be needed, However do it just in case!
	if(INSTALLING)
		RUNREMOTEPROCESS(%PHal_PDA_FOLDER%\PHal.exe, -q, TRUE);
		; The interceptor cannot be stopped (except on reboot)
		; so dolpvid.dll may be in use until a reboot happens
		; however the system allows a rename of the in-use file so that a new one can be copied!
		; NOTE: FALSE below means that any dolpvid.dl~ will be deleted before the rename happens
		REN(%WINDOWS_FOLDER%\dolpvid.dll,%WINDOWS_FOLDER%\dolpvid.dl~,FALSE);
		REN(%WINDOWS_FOLDER%\MyToday.dll,%WINDOWS_FOLDER%\MyToday.dl~,FALSE);
	endif

	; core components
	MKDIR(%PHal_PDA_FOLDER%);
	MCOPY(%THIS_FOLDER%\PPC2003\ARMv4\*.*, %PHal_PDA_FOLDER%, ALWAYS_OVERWRITE, FALSE);
	COPY(%THIS_FOLDER%\setup.msi, %PHal_PDA_FOLDER%\setup.msi, ALWAYS_OVERWRITE);

	; copy interceptor
	COPY(%THIS_FOLDER%\PPC2003\ARMv4\Windows\dolpvid.dll, %WINDOWS_FOLDER%\dolpvid.dll, ALWAYS_OVERWRITE);
	
	if(INSTALLING)
		; back up the old default input method
		RENREGEX(HKEY_CURRENT_USER, ControlPanel\Sip, DefaultIm, DefaultImBackup, TRUE);
		; make this the default input method
		MKREG(HKEY_CURRENT_USER, ControlPanel\Sip\DefaultIm, REG_SZ, {C8311F61-12DF-4107-B5EA-B0B0D55CEC50});
	endif
	if(REPAIRING)
		MKREG(HKEY_CURRENT_USER, ControlPanel\Sip\DefaultIm, REG_SZ, {C8311F61-12DF-4107-B5EA-B0B0D55CEC50});
	endif


	; copy MyToday component
	COPY(%THIS_FOLDER%\PPC2003\ARMv4\Windows\MyToday.dll, %WINDOWS_FOLDER%\MyToday.dll, ALWAYS_OVERWRITE);
	; and create necessary registry entries
	MKREGKEY(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin");
	MKREG(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin"\Type, REG_DWORD, 4);
	MKREG(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin"\Enabled, REG_DWORD, 1); 
	MKREG(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin"\Options, REG_DWORD, 0);
	MKREG(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin"\Order, REG_DWORD, 12);
	MKREG(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin"\Flags, REG_DWORD, 3);
	MKREG(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin"\Selectability, REG_DWORD, 1);
	MKREG(HKEY_LOCAL_MACHINE, Software\Microsoft\Today\Items\"Dolphin"\DLL, REG_SZ, %WINDOWS_FOLDER%\MyToday.dll);

	; copy non-language specific defaults

	if(INSTALLING)
		; Special fix for Build 534 Beta 15 to change default verbosity to Medium
		MKDIR(%PHal_PDA_FOLDER%\settings);
		COPY(%THIS_FOLDER%\settings\%INSTALL_LANG%\controls.ucp, %PHal_PDA_FOLDER%\settings\controls.ucp, ALWAYS_OVERWRITE);
	endif

	; Things to go in the "defaults" directory
	MKDIR(%PHal_PDA_FOLDER%\defaults);
	MCOPY(%THIS_FOLDER%\defaults\*.atl, %PHal_PDA_FOLDER%\defaults, ALWAYS_OVERWRITE, FALSE);
	MCOPY(%THIS_FOLDER%\defaults\*.bk1, %PHal_PDA_FOLDER%\defaults, ALWAYS_OVERWRITE, FALSE);
	COPY(%THIS_FOLDER%\defaults\system.ucp, %PHal_PDA_FOLDER%\defaults\system.ucp, ALWAYS_OVERWRITE);
	COPY(%THIS_FOLDER%\defaults\app_keys.ini, %PHal_PDA_FOLDER%\defaults\app_keys.ini, ALWAYS_OVERWRITE);
	MCOPY(%THIS_FOLDER%\defaults\*.dtp, %PHal_PDA_FOLDER%\defaults, ALWAYS_OVERWRITE, FALSE);

	; Things to go in the "language" directory that are abolutely required
	MKDIR(%PHal_PDA_FOLDER%\language);
	MCOPY(%THIS_FOLDER%\language\Brltab??.ini, %PHal_PDA_FOLDER%\language, ALWAYS_OVERWRITE, FALSE);
	MCOPY(%THIS_FOLDER%\language\Brltab??.ctt, %PHal_PDA_FOLDER%\language, ALWAYS_OVERWRITE, FALSE);
	MCOPY(%THIS_FOLDER%\language\Aut?????.ini, %PHal_PDA_FOLDER%\language, ALWAYS_OVERWRITE, FALSE);
	
	; PDA Quick Reference Guide:
	MKDIR(%PHal_PDA_FOLDER%\docs);
	COPY(%PHal_FOLDER%\docs\qrg%INSTALL_LANG%.psw, %PHal_PDA_FOLDER%\docs\qrg%INSTALL_LANG%.psw, ALWAYS_OVERWRITE);
	MKSHORTCUT(%START_MENU_PROGRAMS%\Dolphin\Quick Reference Guide.lnk, %PHal_PDA_FOLDER%\docs\qrg%INSTALL_LANG%.psw);

	; create shortcuts
	MKSHORTCUT(%START_MENU_PROGRAMS%\Dolphin\Pocket Hal.lnk, %PHal_PDA_FOLDER%\PHal.exe);
	MKSHORTCUT(%STARTUP%\PHal.lnk, %PHal_PDA_FOLDER%\PHal.exe);

	; create registry entries
	MKREGKEY(HKEY_CURRENT_USER, Software\Dolphin\Hal);
	MKREG(HKEY_CURRENT_USER, Software\Dolphin\Hal\LCD, REG_SZ, off);
	CREATEPIIK(HCC, HKEY_CURRENT_USER, Software\Dolphin\Hal, %PHal_PDA_FOLDER%);

	; remove before uninstalling (NOTE: ******* UNINSTALL IS DONE IN REVERSE ORDER *******)
	if(UNINSTALLING)
		; The interceptor cannot be stopped (except on reboot)
		; so dolpvid.dll may be in use until a reboot happens
		; however the system allows a rename of the in-use file so that it will not be used on reboot
		; NOTE: FALSE below means that any dolpvid.dl~ will be deleted before the rename happens
		REN(%WINDOWS_FOLDER%\dolpvid.dll,%WINDOWS_FOLDER%\dolpvid.dl~,FALSE);
		; restore the default input method
		RENREG(HKEY_CURRENT_USER, ControlPanel\Sip, DefaultImBackup, DefaultIm);
		RMREG(HKEY_CURRENT_USER, ControlPanel\Sip, DefaultIm);
		REN(%WINDOWS_FOLDER%\MyToday.dll,%WINDOWS_FOLDER%\MyToday.dl~,FALSE);
		RUNREMOTEPROCESS(%PHal_PDA_FOLDER%\dolpremv.exe, -i, TRUE);
		RUNREMOTEPROCESS(%PHal_PDA_FOLDER%\PHal.exe, -q, TRUE);
	endif
	; Install the interceptor...
	if(INSTALLING)
		DEL(%WINDOWS_FOLDER%\dolpvid.dl~);
		DEL(%WINDOWS_FOLDER%\MyToday.dl~);
		RUNREMOTEPROCESS(%PHal_PDA_FOLDER%\dolpinst.exe, -i, TRUE);
	endif
	; If we are repairing then make sure the interceptor is installed
	if(REPAIRING)
		DEL(%WINDOWS_FOLDER%\dolpvid.dl~);
		DEL(%WINDOWS_FOLDER%\MyToday.dl~);
		RUNREMOTEPROCESS(%PHal_PDA_FOLDER%\dolpinst.exe, -i, TRUE);
	endif
}
