// registry.inc

//
//	Attempts to open existing key, if no key exists attempts to create key
//
//		$0 = Registry key root
//		$1 = Registry key data
//
//		$R0 = Registry key [KEY_NULL when unsuccessfull]
//
[MakeRegKey]

	set KeyRoot = $($0)
	set Data = $($1)
	_Whisper("MakeRegKey", _Show(KeyRoot)_Show(Data))

	set KeyName = _RegKeyName($(Data))

	_OpenRegKey($(KeyRoot), $(KeyName), KeyResult)
	_IfNotValidKey($(KeyResult))
		_CreateRegKey($(KeyRoot), $(Data), KeyResult)
	endif

	_Whisper("MakeRegKey", _Show(KeyResult))
	Return $(KeyResult)

//
//	Sets value in given key.
//
//		$0 = Type (REG_VT_*)
//		$1 = Key
//		$2 = Value Name
//		$3 = Value Data
//
//		$R0 = Boolean value indicating success.
//
[SetRegValueByKey]

	set Type = $($0)
	set Key = $($1)
	set ValueName = $($2)
	set Value = $($3)
	_Whisper("SetRegValueByKey", _Show(Type)_Show(Key)_Show(ValueName)_Show(Value))

	set Success = FALSE

	set ValueData = _MakeValueData($(ValueName), $(Type), $(Value))

	SetRegValue $(Key) $(ValueData)
	ifint $(RegLastError) == 0
		set Success = TRUE
	endif

	_Whisper("SetRegValueByKey", _Show(Success))
	Return $(Success)

//
//	Gets value in given key.
//
//		$0 = Type (REG_VT_*) [set to $(TYPECHECK_NONE) for any type]
//		$1 = Key
//		$2 = Value Name
//
//		$R0 = Boolean value indicating success.
//		$R1 = Value Data
//
[GetRegValueByKey]
	read-syms GlobalConstants

	set Type = $($0)
	set Key = $($1)
	set ValueName = $($2)
	_Whisper("GetRegValueByKey", _Show(Type)_Show(Key)_Show(ValueName))
		
	set Success = FALSE
	set Value = EMPTY_STR

	GetRegValue $(Key) $(ValueName) ValueData
	ifint $(RegLastError) != 0
		goto Exit
	endif

	set Value = _RegValueValue($(ValueData))

	ifstr(i) $(Type) != $(TYPECHECK_NONE)
		set TypeRead = _RegValueType($(ValueData))
		ifstr(i) $(TypeRead) != $(Type)
			_Error("Type check failed", _Show(TypeRead)_Show(Type))
			goto Exit
		endif
	endif
 
	set Success = TRUE

Exit = +

	_Whisper("GetRegValueByKey", _Show(Success)_Show(Value))
	Return $(Success) $(Value)

//
//	Gets value in given key returning default on error.
//
//		$0 = Type (REG_VT_*) [set to $(TYPECHECK_NONE) for any type]
//		$1 = Key
//		$2 = Value name
//		$3 = Default value data.
//
//		$R0 = Value data
//
[DefaultGetRegValueByKey]

	set Type = $($0)
	set Key = $($1)
	set ValueName = $($2)
	set Default = $($3)
	_Whisper("DefaultGetRegValueByKey", _Show(Type)_Show(Key)_Show(ValueName)_Show(Default))

	set Value = $(Default)

	_LocalShell GetRegValueByKey $(Type) $(Key) $(ValueName)
	_If($($R0))
		set Value = $($R1)
	endif

	_Whisper("DefaultGetRegValueByKey", _Show(Value))
	Return $(Value)

//
//	Sets value in given key path (in HKEY_LOCAL_MACHINE).
//
//		$0 = Type (REG_VT_*)
//		$1 = Key path
//		$2 = Value Name
//		$3 = Value Data
//
//		$R0 = Boolean value indicating success.
//
[SetRegValueByName]

	set Type = $($0)
	set Path = $($1)
	set ValueName = $($2)
	set Value = $($3)
	_Whisper("SetRegValueByName", _Show(Type)_Show(Path)_Show(ValueName)_Show(Value))

	set Success = FALSE

	_MakeStdRegKey($(!REG_H_LOCAL), $(Path), Key)
	_IfNotValidKey($(Key))
      _AssertGoto(FALSE, Exit)
	endif

	_LocalShell SetRegValueByKey $(Type) $(Key) $(ValueName) $(Value)
   _AssertGoto($($R0), Exit)

	set Success = TRUE

	CloseRegKey $(Key)

Exit = +

	_Whisper("SetRegValueByName", _Show(Success))
	Return $(Success)

//
//	Gets value in given key path (in HKEY_LOCAL_MACHINE).
//
//		$0 = Type (REG_VT_*) [set to $(TYPECHECK_NONE) for any type]
//		$1 = Key path
//		$2 = Value name
//
//		$R0 = Boolean value indicating success.
//		$R1 = Value Data
//
[GetRegValueByName]

	set Type = $($0)
	set Path = $($1)
	set ValueName = $($2)
	_Whisper("GetRegValueByName", _Show(Type)_Show(Path)_Show(ValueName))

	set Success = FALSE
	set Value = EMPTY_STR

	_OpenRegKey($(!REG_H_LOCAL), $(Path), Key)
	_IfNotValidKey($(Key))
      goto Exit
	endif

	_LocalShell GetRegValueByKey $(Type) $(Key) $(ValueName)
	_IfNot($($R0))
      goto Exit
	endif

	set Success = TRUE
	set Value = $($R1)

	CloseRegKey $(Key)

Exit = +

	_Whisper("GetRegValueByName", _Show(Success)_Show(Value))
	Return $(Success) $(Value)

//
// Set values from the given list with error checking performed.
//
//	$0 = Key
//	$1 = Value list.
//
//	$R0 = Boolean indicating success.
//
[SetValuesFromList]

	set Key = $($0)
	set List = $($1)
	_Whisper("SetValuesFromList", _Show(Key)_Show(List))

	set Success = FALSE

	_UtilityShell AddValueList $(Key) $(List)
   _OnShellErrorGoto(Exit)
   _OnUtilityShellErrorGoto($($R0), Exit)

	set Success = TRUE

Exit = +

	_Whisper("SetValuesFromList", _Show(Success))
	Return $(Success)

//
//	Close registry key (if open)
//
//		$0 = Key to close
//
[CloseOpenRegKey]

	set Key = $($0)

	_IfValidKey($(Key))
		CloseRegKey $(Key)
	endif

	Return

//
//	Close registry keys in list (if open)
//
//		$0 = List of keys to close
//
[CloseOpenRegKeyList]

	set KeyList = $($0)

	ForListDo $(KeyList)
		_CloseRegKey($($))
	EndForListDo

	Return


//
//	Copies registry values from one key to another.
//
//		$0 = Source key
//		$1 = Dest key
//
//		$R0 = Boolean result indicating success
//
[CopyRegValues]

	set KeySource = $($0)
	set KeyDestination = $($1)
	_Whisper("CopyRegValues", _Show(KeySource)_Show(KeyDestination))

	set Success = FALSE

	_IfNotValidKey($(KeySource))
		goto Error
	endif

	_IfNotValidKey($(KeyDestination))
		goto Error
	endif

	EnumRegValue $(KeySource) ValueList

	_LocalShell SetValuesFromList $(KeyDestination) $(ValueList)
   _AssertGoto($($R0), Error)

Exit = +
	set Success = TRUE

Error = +
	_Whisper("CopyRegValues", _Show(Success))
	Return $(Success)

//
//	Copies a registry tree from one key to another.
//
//		$0 = Source key
//		$1 = Dest key
//
//		$R0 = Boolean result indicating success
//
[CopyRegTree]

	set KeySource = $($0)
	set KeyDestination = $($1)
	_Whisper("CopyRegTree", _Show(KeySource)_Show(KeyDestination))

	set Success = FALSE

	_LocalShell CopyRegValues $(KeySource) $(KeyDestination)
   _AssertGoto($($R0), Error)

	EnumRegKey $(KeySource) SourceSubKeyList
	QueryListSize NumSubKeys $(SourceSubKeyList)
	ifint $(NumSubKeys) == 0
		goto Exit
	endif

	ForListDo $(SourceSubKeyList)
		set SubKeyName = _RegKeyName($($))

      _Trace("Recursing to ", _Show(SubKeyName))

		_OpenRegKey($(KeySource), $(SubKeyName), KeySourceSubKey)
	   _IfNotValidKey($(KeySourceSubKey))
         _AssertMsg(FALSE, "Unable to open subkey!");
      endif

		_MakeStdRegKey($(KeyDestination), $(SubKeyName), KeyDestinationSubKey)

		_LocalShell CopyRegTree $(KeySourceSubKey) $(KeyDestinationSubKey)
      _AssertGoto($($R0), Error)

		_CloseRegKey($(KeySourceSubKey))
		_CloseRegKey($(KeyDestinationSubKey))
	EndForListDo

Exit = +
	set Success = TRUE

Error = +
	_Whisper("CopyRegTree", _Show(Success))
	Return $(Success)
	
//
//	Open the "Parameters" key for the given service name.
//
//		$0 = Service name.
//
//		$R0 = Parameters key [KEY_NULL on error]
//
[OpenServiceParamKey]

	set ServiceName = $($0)
	_Whisper("OpenServiceParamKey", _Show(ServiceName))

	set KeyParameters = KEY_NULL

	_OpenRegKey_HKLM($(!NTN_ServiceBase)"\\"$(ServiceName)"\\Parameters", KeyParameters)

	_Whisper("OpenServiceParamKey", _Show(KeyParameters))
	Return $(KeyParameters)
