// snmp.inc

[SNMPConstants]
SNMPAgentNameList = ^(Files-SNMP, 0)
SNMPDllNameList   = ^(Files-SNMP, 2)

[LocalSNMPRegistryConstants]
KEY_SNMPROOT = $(!NTN_ServiceBase)"\\SNMP"
KEY_PARAMETERS = $(KEY_SNMPROOT)"\\Parameters"
KEY_AGENTLIST = $(KEY_PARAMETERS)"\\ExtensionAgents"

//	********************************************************************************
//	Local Shells
//	********************************************************************************

//	Determines if SNMP is installed
//
//		$R0	= Boolean result indicating installed.
//
[IsSNMPServiceInstalled]
	read-syms LocalSNMPRegistryConstants

	_Enter("IsSNMPServiceInstalled", "")
	set Installed = FALSE

	_OpenRegKey_HKLM($(KEY_SNMPROOT), KeySNMPRoot)
	_IfNotValidKey($(KeySNMPRoot))
		goto Exit
	endif
	CloseRegKey $(KeySNMPRoot)
	set Installed = TRUE

Exit = +
	
	_Exit("IsSNMPServiceInstalled", _Show(Installed))
	Return $(Installed)

//
//	Point to agent's DLL and make sure agent key exists under "Parameters"
//
//		$0	= Human-readable agent name suffix.
//		$1	= File path to DLL.
//
//		$R0	= Boolean result indicating success.
//
[InstallAgent]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms LocalSNMPRegistryConstants

	set AgentSuffix = $($0)
	set AgentPath = $($1)
	_Enter("InstallAgent", _Show(AgentSuffix)_Show(AgentPath))

	set Result = FALSE

   // 1st create the key under HKLM\Software\Digi\SNMP\????\CurrentVersion
   set KEY_AGENTSOFTWARE = $(KEY_DIGIROOT)"\\SNMP\\"$(AgentSuffix)"\\CurrentVersion"
   _Trace("Installing agent", _Show(KEY_AGENTSOFTWARE))

	_MakeStdRegKey($(!REG_H_LOCAL), $(KEY_AGENTSOFTWARE), KeyCurrentVersion)
	_IfNotValidKey($(KeyCurrentVersion))
      _AssertGoto(FALSE, Exit)
	endif

   // 2nd write the path to the dll
	_SetRegSzByKey $(KeyCurrentVersion) "PathName" $(AgentPath)
   _Assert($($R0))

	CloseRegKey $(KeyCurrentVersion)

   // 3rd point SNMP back to the stuff we just wrote above
	_OpenRegKey_HKLM($(KEY_PARAMETERS), KeyParams)
	_IfNotValidKey($(KeyParams))
      _AssertGoto(FALSE, Exit)
	endif

	_MakeStdRegKey($(KeyParams), "ExtensionAgents", KeyAgent)
	_IfNotValidKey($(KeyAgent))
      _AssertGoto(FALSE, Exit)
	endif

	_SetRegSzByKey $(KeyAgent) "Digi-"$(AgentSuffix) $(KEY_AGENTSOFTWARE)
   _Assert($($R0))
  
	_CloseRegKey($(KeyAgent))
	_CloseRegKey($(KeyParams))

	set Result = TRUE

Exit = +

	_Exit("InstallAgent", _Show(Result))
	Return $(Result)

//
//	Undoes actions of [InstallAgent]
//
//		$0	= Human-readable agent name suffix.
//
[RemoveAgent]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms LocalSNMPRegistryConstants

	set AgentSuffix = $($0)
	_Enter("RemoveAgent", _Show(AgentSuffix))

	_OpenRegKey_HKLM($(KEY_DIGIROOT)"\\SNMP", KeyRoot)
	_IfValidKey($(KeyRoot))
      _Trace("Whacking reg key", _Show(AgentSuffix))
	   DeleteRegTree $(KeyRoot) $(AgentSuffix)
	   CloseRegKey $(KeyRoot)
   endif

	_OpenRegKey_HKLM($(KEY_AGENTLIST), KeyAgents)
	_IfValidKey($(KeyAgents))
      _Trace("Whacking reg value", _Show(AgentSuffix))
      DeleteRegValue $(KeyAgents) "Digi-"$(AgentSuffix)
	   CloseRegKey $(KeyAgents)
   endif

	_Exit("RemoveAgent", _Show(AgentSuffix))
	Return

//	********************************************************************************
//	Global Shells
//	********************************************************************************

//
//	Installs all the SNMP extension agents
//
//		$R0 = Boolean indicating successfull install.
//
[InstallSNMP]
	read-syms GlobalConstants
	read-syms GlobalPathConstants
	read-syms SNMPConstants
	
	_Enter("InstallSNMP", "")

	set Success = FALSE

	_LocalShell IsSNMPServiceInstalled
	_IfNot($($R0))
		goto Exit
	endif

	ForListDo $(SNMPAgentNameList)
		set AgentSuffix = $($)
		set AgentDll = *($(SNMPDllNameList), $(#))
		set AgentPath = $(!STF_WINDOWSSYSPATH)"\\"$(AgentDll)

      _Trace("About to install", _Show(AgentSuffix)_Show(AgentDll)_Show(AgentPath))
		_LocalShell InstallAgent $(AgentSuffix) $(AgentPath)
		set Success = $($R0)

		_IfNot($(Success))
			read-syms SNMPMessages-$(!STF_LANGUAGE)
			_LocalShell StatusMessage $(InstallError)
			goto Exit
		endif

	EndForListDo

Exit = +

	_Exit("InstallSNMP", _Show(Success))
	Return $(Success)

//
//	Removes all the SNMP extension agents
//
//
[RemoveSNMP]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms SNMPConstants

	_Enter("RemoveSNMP", "")

	ForListDo $(SNMPAgentNameList)
      _Trace("About to remove", _Show($))
		_LocalShell RemoveAgent $($)
	EndForListDo

	_OpenRegKey_HKLM($(KEY_DIGIROOT)"\\SNMP", KeyRoot)
	_IfValidKey($(KeyRoot))

      // get count of reg keys under SNMP
		EnumRegKey $(KeyRoot) OtherSnmpsList
		QueryListSize NumOtherSnmps $(OtherSnmpsList)
      _Trace("", _Show(NumOtherSnmps))
   	CloseRegKey $(KeyRoot)

		ifint $(NumOtherSnmps) == 0
         _Trace($(KEY_DIGIROOT)"\\SNMP is empty, whacking the reg key", "")
      	_OpenRegKey_HKLM($(KEY_DIGIROOT), KeyRoot)
      	DeleteRegTree $(KeyRoot) "SNMP"
      	CloseRegKey $(KeyRoot)
		endif
	endif

Exit = +

	_Exit("RemoveSNMP", "")
	Return
