// bind.inc - binding mode

//
//	Get bindings for a given key
//
//		$0 = Key
//
//		$R0 = Bind list.
//		$R1 = Route list.
//		$R2 = Export list.
//
[GetBindings]
	set Key = $($0)
	_Whisper("GetBindings", _Show(Key))

	_DefaultGetRegMultiSzByKey $(Key) "Bind" EMPTY_LIST
	set BindList = $($R0)
	_DefaultGetRegMultiSzByKey $(Key) "Route" EMPTY_LIST
	set RouteList = $($R0)
	_DefaultGetRegMultiSzByKey $(Key) "Export" EMPTY_LIST
	set ExportList = $($R0)

Exit = +

	_Whisper("GetBindings", _Show(BindList)_Show(RouteList)_Show(ExportList))
	Return $(BindList) $(RouteList) $(ExportList)

//
//	Binds an adapter.
//
//		$0 = Option
//		$1 = Source Dir
//		$2 = $(AddCopy)
//		$3 = $(DoCopy)
//		$4 = $(DoConfig)
//
//		$R0 = Status (STATUS_*)
//
[BindMode]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants

	set Option = $($0)
	set SrcDir = $($1)
	set AddCopy = $($2)
	set DoCopy = $($3)
	set DoConfig = $($4)
	_Whisper("BindMode", _Show(Option)_Show(SrcDir)_Show(AddCopy)_Show(DoCopy)_Show(DoConfig))

	read-syms DriverConstants-$(Option)

	// Add SNMP registry entries
	_LocalShell InstallSNMP

   ifstr(i) $(!NTVER) == "3.51"
	   LoadLibrary "x" $(!STF_WINDOWSSYSPATH)"\\DigiInf.dll" DIGIHANDLE
	   _Call("LibraryProcedure Update", _Show(!STF_HWND)_Show(Option))
      LibraryProcedure Result $(DIGIHANDLE) AddModemSettingsToRAS $(!STF_HWND) $(Option)
	   _Return("LibraryProcedure Update", _Show(Result))
	   FreeLibrary $(DIGIHANDLE)
   endif

   set OptionsThatNeedSpecialBindings = { }
	Ifcontains $(Option) not-in $(OptionsThatNeedSpecialBindings)
	   goto Exit
	endif

	set Status = "STATUS_FAILED"

	StartWait

	// Binding review looks at all Digi virtual adapters (i.e. FR or X.25 PVC's) for
	//  their upper level protocol usage (i.e. TCPIP, IPX, NetBEUI, etc.) and
	//  enables/disable the upper level protocol bindings to the virtual adapter.
	// X.25 only supports IP.
	// What to enable and disable comes from the GUI config.

	set TCPIPUsageList = EMPTY_LIST
	set IPXUsageList = EMPTY_LIST
	set EthernetUsageList = EMPTY_LIST

	_LocalShell EnumAdapters { "FR", "X25L" } FALSE
	set AdapterDataList = $($R0)

	ForListDo $(AdapterDataList)
		set ServiceName = *($($), 1)
		_OpenServiceParamKey($(ServiceName), KeyService)

		_LocalShell GetRegValueByKey $(TYPECHECK_NONE) $(KeyService) "TCPIP"
      _Assert($($R0))
		set TCPIPUsageList = >($(TCPIPUsageList), $($R1))

		_LocalShell GetRegValueByKey $(TYPECHECK_NONE) $(KeyService) "IPX"
      _Assert($($R0))
		set IPXUsageList = >($(IPXUsageList), $($R1))

		_LocalShell GetRegValueByKey $(TYPECHECK_NONE) $(KeyService) "Ethernet"
      _Assert($($R0))
		set EthernetUsageList = >($(EthernetUsageList), $($R1))
	EndForListDo
	CloseRegKey $(KeyService)

	set TypeList = { "TCPIP", "IPX", "Ethernet" }
	set TCPIPServiceList = { "Tcpip", "NetBT" }
	set IPXServiceList = { "NwlnkIpx" }
	set EthernetServiceList = { "Nbf", "AppleTalk", "DLC" } 

	_OpenRegKey_HKLM($(!NTN_ServiceBase), KeyServiceRoot)

	EnumRegKey $(KeyServiceRoot) ServicesList
	ForListDo $(ServicesList)
		set Service = _RegKeyName($($))

		set KeyBind = KEY_NULL
		set KeyDisabledBind = KEY_NULL

		_Whisper("Working on ", _Show1(Service))
		Ifcontains(i) $(Service) in $(HardwareList)
			goto NextService
		endif

		_OpenRegKey($(KeyServiceRoot), $(Service)"\\Linkage", KeyBind)
		_IfNotValidKey($(KeyBind))
			goto NextService
		endif

		_OpenRegKey($(KeyBind), "Disabled", KeyDisabledBind)
		_IfNotValidKey($(KeyDisabledBind))
			goto NextService
		endif

		_LocalShell GetBindings $(KeyBind)
		set BindList = $($R0)
		set RouteList = $($R1)
		set ExportList = $($R2)

		_LocalShell GetBindings $(KeyDisabledBind)
		set DisabledBindList = $($R0)
		set DisabledRouteList = $($R1)
		set DisabledExportList = $($R2)

		_IfEmptyList($(BindList))
			_IfEmptyList($(DisabledBindList))
				goto NextService
			endif
		endif

		set NewBindList = EMPTY_LIST
		set NewRouteList = EMPTY_LIST
		set NewExportList = EMPTY_LIST
		set NewDisabledBindList = EMPTY_LIST
		set NewDisabledRouteList = EMPTY_LIST
		set NewDisabledExportList = EMPTY_LIST

		ForListDo $(BindList)
			set Activate = TRUE
			Split-String $($) "_\\" BindComponentList

			ForListDo $(AdapterDataList)
				set AdapterName = *($($), 1)
				set AdapterIndex = $(#)

				Ifcontains(i) $(AdapterName) in $(BindComponentList)
					ForListDo $(TypeList)
						set Protocol = $($)
						set Usage = *($($(Protocol)UsageList), $(AdapterIndex))

						ifint $(Usage) == 0
							Ifcontains(i) $(Service) in $($(Protocol)ServiceList) 
								set Activate = FALSE
							else
								ForListDo $($(Protocol)ServiceList)
									Ifcontains(i) $($) in $(BindComponentList)
										set Activate = FALSE
									endif
								EndForListDo	
							Endif
						endif
					EndForListDo
				endif
			EndForListDo

			set BindValue = *($(BindList), $(#))
			set RouteValue = *($(BindList), $(#))
			set ExportValue = *($(BindList), $(#))

			_If($(Activate))
				set NewBindList = >($(NewBindList), $(BindValue))
				set NewRouteList = >($(NewRouteList), $(RouteValue))
				set NewExportList = >($(NewExportList), $(ExportValue))
			else
				set NewDisabledBindList = >($(NewDisabledBindList), $(BindValue))
				set NewDisabledRouteList = >($(NewDisabledRouteList), $(RouteValue))
				set NewDisabledExportList = >($(NewDisabledExportList), $(ExportValue))
			endif
		EndForListDo

		ForListDo $(DisabledBindList)
			set Activate = FALSE
			Split-String $($) "_\\" BindComponentList

			ForListDo $(AdapterDataList)
				set AdapterName = *($($), 1)
				set AdapterIndex = $(#)

				Ifcontains(i) $(AdapterName) in $(BindComponentList)
					ForListDo $(TypeList)
						set Protocol = $($)
						set Usage = *($($(Protocol)UsageList), $(AdapterIndex))

						ifint $(Usage) > 0
							Ifcontains(i) $(Service) in $($(Protocol)ServiceList) 
								set Activate = TRUE
							else
								ForListDo $($(Protocol)ServiceList)
									Ifcontains(i) $($) in $(BindComponentList)
										set Activate = TRUE
									endif
								EndForListDo	
							Endif
						endif
					EndForListDo
				endif
			EndForListDo

			set BindValue = *($(DisabledBindList), $(#))
			set RouteValue = *($(DisabledRouteList), $(#))
			set ExportValue = *($(DisabledExportList), $(#))

			_If($(Activate))
				set NewBindList = >($(NewBindList), $(BindValue))
				set NewRouteList = >($(NewRouteList), $(BindValue))
				set NewExportList = >($(NewExportList), $(ExportValue))
			else
				set NewDisabledBindList = >($(NewDisabledBindList), $(BindValue))
				set NewDisabledRouteList = >($(NewDisabledRouteList), $(RouteValue))
				set NewDisabledExportList = >($(NewDisabledExportList), $(ExportValue))
			endif
		EndForListDo

		set BindValueList = { +
				_MakeMultiSzData("Bind", $(NewBindList)), +
				_MakeMultiSzData("Route", $(NewRouteList)), +
				_MakeMultiSzData("Export", $(NewExportList)) }
		_LocalShell SetValuesFromList $(KeyBind) $(BindValueList)
      _Assert($($R0))

		set DisabledBindValueList = { +
				_MakeMultiSzData("Bind", $(NewDisabledBindList)), +
				_MakeMultiSzData("Route", $(NewDisabledRouteList)), +
				_MakeMultiSzData("Export", $(NewDisabledExportList)) }
		_LocalShell SetValuesFromList $(KeyDisabledBind) $(DisabledBindValueList)
      _Assert($($R0))

NextService = +

		_CloseRegKey($(KeyDisabledBind))
		_CloseRegKey($(KeyBind))

	EndForListDo
	
	_CloseRegKey($(KeyServiceRoot))
	
	EndWait

Exit = +

	set Status = "STATUS_SUCCESSFUL"

	_Whisper("BindMode", _Show(Status))
	Return $(Status)
