function readFromRegistry (RootKey)
 Dim WSHShell, value 
 Set WSHShell = CreateObject("WScript.Shell")
 
 On Error Resume Next
 value = WSHShell.RegRead(RootKey)
 
 if Err <> 0 then
   readFromRegistry = strDefault
   WScript.Echo "Incredibuild Coordinator not installed"   
 else
  readFromRegistry = value
 end if
 
 set WSHShell = nothing
end function

function openURLInDefaultBrowser(url)
 Dim WSHShell
 Set WSHShell = CreateObject("WScript.Shell")
 WSHShell.Run url
 
 set WSHShell = nothing
end function

function getComputerName
 'Try to get full computer name
 
 Dim WSHShell, objWMIService, colItems, objItem
 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

 Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration")
 
 For Each objItem in colItems
	if not objItem.DNSHostName = "" then
	  getComputerName = objItem.DNSHostName
	  Exit Function
	end if
 Next
 ' if previous approach did not work, try to get NetBIOS name 
 
 Set WSHShell = CreateObject("WScript.Network")
 getComputerName = WSHShell.ComputerName  
 set WSHShell = nothing 
  
end function

Const RegCoordinator = "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Xoreax\Incredibuild\Coordinator\"
Primary = readfromRegistry(RegCoordinator + "Primary")

if Primary = 1 then
  'primary coordinator
	
  computerName = getCOmputerName
  port = readfromRegistry(RegCoordinator + "IncredibuildManagerPort")
	
	if (port <> strDefault) then
    openURLInDefaultBrowser("https://" & computerName & ":" & port)
	end if	
else 
  'backup coordinator 
	
	WebUrl = readfromRegistry(RegCoordinator + "CoordDashboardURL")
	openURLInDefaultBrowser(WebUrl)
end if	
