// configure.inc - configuation mode


//
//	Converts a line/port number to 4 digit name
//
//		$0 = Line/port number
//
//		$R0 = Line/port name
//
[NumberToName]

	set Num = $($0)
	_Enter("NumberToName", _Show(Num))

   set Name = EMPTY_STR

#if defined(SUPPORT_4DIGITNAME)
   ifint $(Num) > 9999
      _AssertMsg(FALSE, "Out of range number "$(Num))
   endif

   ifint $(Num) < 1000
      set Name = $(Name)"0"
   endif
   ifint $(Num) < 100
      set Name = $(Name)"0"
   endif
   ifint $(Num) < 10
      set Name = $(Name)"0"
   endif
#endif

   set Name = $(Name)$(Num)

Exit = +

	_Exit("NumberToName", _Show(Name))
	Return $(Name)

//
//	Common configure routine for all adapters.
//
//		$0 = Source directory
//		$1 = Option
//		$2 = Parameters Key
//		$3 = AdapterServiceName
//    $4 = Config Dll Path
//    $5 = Config Dll Entry Point
//
//		$R0 = Status 
//    $R1 = Bindings need to be reviewed
//
[ConfigureAdapter]

	set SrcDir = $($0)
	set Option = $($1)
	set KeyParameters = $($2)
	set AdapterServiceName = $($3)
   set CfgDllPath = $($4)
   set CfgDllEntry = $($5)
	_Enter("ConfigureAdapter", _Show(SrcDir)_Show(Option)_Show(KeyParameters)_Show(AdapterServiceName)_Show(CfgDllPath)_Show(CfgDllEntry))

	set Status = "STATUS_FAILED"
	set NeedBindingReview = FALSE

   set OldLineConfigList = EMPTY_LIST     // { {"LineName", { Services } } }

	_GetRegDwordByKey $(KeyParameters) DXB_REGVAL_NUMBEROFLINES
   _Assert($($R0))
	set NumberOfLines = $($R1)

   set LineNumber = 0
	_Do(GetOldLineConfigs)

		_LocalShell NumberToName $(LineNumber)
      set LineName = $($R0)

	   set OldLineServices = EMPTY_LIST

	   _OpenRegKey($(KeyParameters), $(LineName), KeyLine)
	   _IfNotValidKey($(KeyLine))
		   _MakeStdRegKey($(KeyParameters), $(LineName), KeyLine)
		   set OldLineServices = { "None" }
	   else
		   _GetRegMultiSzByKey $(KeyLine) DXB_REGVAL_LINE_SERVICES
         _Assert($($R0))
		   set OldLineServices = $($R1)
	      ForListDo $(OldLineServices)
            set Service = $($)
		      _LocalShell "SetUp-"$(Service) $(LineName) $(KeyLine) $(AdapterServiceName)
	      EndForListDo
	   endif

		CloseRegKey $(KeyLine)

      set OldLineConfigList = >($(OldLineConfigList), { $(LineName), $(OldLineServices) })

		set-add LineNumber = $(LineNumber), 1
	_While(GetOldLineConfigs, ifint $(LineNumber) < $(NumberOfLines))

	_SubroutineShell PopBillboard
	LoadLibrary "x" $(CfgDllPath) DIGIHANDLE
	_Call("LibraryProcedure "$(CfgDllEntry), _Show(!STF_HWND)_Show(Option)_Show(AdapterServiceName))
   LibraryProcedure Result $(DIGIHANDLE) $(CfgDllEntry) $(!STF_HWND) $(Option) $(AdapterServiceName)
	_Return("LibraryProcedure "$(CfgDllEntry), _Show(Result))
	FreeLibrary $(DIGIHANDLE)

	ifstr(i) $(Result) != "OK"
		read-syms InstallMessages-$(!STF_LANGUAGE)
		ifstr(i) $(Result) == "Cancel"
			set Status = "STATUS_USERCANCEL"
         ifstr(i) $(!NTN_InstallMode) == "Install"
            _LocalShell SetupMessage "FATAL" $(UserCanceledInstall)
         endif
		else
         _LocalShell SetupMessage "FATAL" $(CfgDllFailed)
		endif
		goto Exit
	endif

   // Ok, this may look kinda weird, but when we come back from the config
   // there may be more ports/lines than before we ran the cfg dll (eg. user
   // added a daughter board to a Houston).

	_GetRegDwordByKey $(KeyParameters) DXB_REGVAL_NUMBEROFLINES
   _Assert($($R0))
	set NewNumberOfLines = $($R1)

   ifint $(NewNumberOfLines) > $(NumberOfLines)
      _Trace("Resetting number of lines", _Show(NewNumberOfLines)_Show(NumberOfLines))
      set NumberOfLines = $(NewNumberOfLines)
   endif

   set LineNumber = 0
	_Do(GetNewLineConfigs)

		_LocalShell NumberToName $(LineNumber)
      set LineName = $($R0)

		set OldLineServices = { "None" }
	   ForListDo $(OldLineConfigList)
         set OldLineName = *($($), 1)
         ifstr(i) $(LineName) == $(OldLineName)
            set OldLineServices = *($($), 2)
         endif
	   EndForListDo

	   _OpenRegKey($(KeyParameters), $(LineName), KeyLine)
		_GetRegMultiSzByKey $(KeyLine) DXB_REGVAL_LINE_SERVICES
      _Assert($($R0))
		set NewLineServices = $($R1)

		_Trace("Compare", _Show(LineName)_Show(OldLineServices)_Show(NewLineServices))

      // 1st add any new services
	   ForListDo $(NewLineServices)
         set Service = $($)
			Ifcontains(i) $(Service) not-in $(OldLineServices)
		      _Trace("Add", _Show(LineName)_Show(Service))
			   _LocalShell "Add-"$(Service) $(LineName) $(KeyLine) $(AdapterServiceName) $(LineNumber) $(SrcDir) $(Option)
            _Assert($($R0))
	         set NeedBindingReview = TRUE
         endif
	   EndForListDo

      // 2nd remove any old services not used any more
	   ForListDo $(OldLineServices)
         set Service = $($)
			Ifcontains(i) $(Service) not-in $(NewLineServices)
		      _Trace("Remove", _Show(LineName)_Show(Service))
			   _LocalShell "Remove-"$(Service) $(LineName) $(KeyLine) $(AdapterServiceName)
	         set NeedBindingReview = TRUE
         endif
	   EndForListDo

      // 3rd config & cleanup new services
	   ForListDo $(NewLineServices)
         set Service = $($)
		   _Trace("Config", _Show(LineName)_Show(Service))
		   _LocalShell "Config-"$(Service) $(LineName) $(KeyLine) $(AdapterServiceName) $(Option)
         _Assert($($R0))
         _If($($R1))
            // Don't reset to FALSE!
	         set NeedBindingReview = TRUE
         endif
		   _Trace("CleanUp", _Show(LineName)_Show(Service))
	      _LocalShell "CleanUp-"$(Service) $(LineName) $(KeyLine) $(AdapterServiceName) $(Result)
	   EndForListDo

      // 4th cleanup old services
	   ForListDo $(OldLineServices)
         set Service = $($)
		   _Trace("CleanUp", _Show(LineName)_Show(Service))
	      _LocalShell "CleanUp-"$(Service) $(LineName) $(KeyLine) $(AdapterServiceName) $(Result)
	   EndForListDo

		CloseRegKey $(KeyLine)

		set-add LineNumber = $(LineNumber), 1
	_While(GetNewLineConfigs, ifint $(LineNumber) < $(NumberOfLines))

   set Status = "STATUS_SUCCESSFUL"

Exit = +

	_Exit("ConfigureAdapter", _Show(Status)_Show(NeedBindingReview))
	Return $(Status) $(NeedBindingReview)

//
//	Configures an adapter.
//
//		$0 = Option
//		$1 = Source Dir
//		$2 = $(AddCopy)
//		$3 = $(DoCopy)
//		$4 = $(DoConfig)
//
//		$R0 = Status (STATUS_*)
//
[ConfigureMode]

	set Option = $($0)
	set SrcDir = $($1)
	set AddCopy = $($2)
	set DoCopy = $($3)
	set DoConfig = $($4)
	_Enter("ConfigureMode", _Show(Option)_Show(SrcDir)_Show(AddCopy)_Show(DoCopy)_Show(DoConfig)_Show(!NTN_RegBase))

	set Status = "STATUS_FAILED"

   set KeyParameters = KEY_NULL
   set AdapterServiceName = EMPTY_STR

   ifstr(i) $(Option) == "PAMA"
      // PAMA is special, it is a service NOT an adapter
      // but still needs configuring, so don't try opening
      // its NetworkCards key, just jump to configuring it.
      goto Configure
   endif

   // $(!NTN_RegBase) == HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\#
	_OpenRegKey_HKLM($(!NTN_RegBase), KeySoftware)
	_IfNotValidKey($(KeySoftware))
		goto Error_CannotConfig
	endif

	_GetRegSzByKey $(KeySoftware) DXB_REGVAL_SERVICENAME
   _Assert($($R0))
	set AdapterServiceName = $($R1)
	CloseRegKey $(KeySoftware)
	
	_OpenServiceParamKey($(AdapterServiceName), KeyParameters)
	_IfNotValidKey($(KeyParameters))
		goto Error_CannotConfig
	endif

Configure = +

	_LocalShell ConfigureAdapter-$(Option) $(SrcDir) $(Option) $(KeyParameters) $(AdapterServiceName)
   set Status = $($R0)

	CloseRegKey $(KeyParameters)

   goto Exit

Error_CannotConfig = +

   // Force a failure message to be displayed
   _OnUtilityShellErrorGoto("CANNOT_CONFIGURE_SOFTWARE", Exit)
	set Status = "STATUS_USERCANCEL"

Exit = +

	_Exit("ConfigureMode", _Show(Status))
	Return $(Status)
