' Ecosystem tray boot — sin WMI, sin Sleep, sin wscript anidado.
' Escribe log al instante y lanza node en cada carpeta.

On Error Resume Next

Dim sh, fso, logFile, node, f
Set sh = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

logFile = "C:\xampp\htdocs\JobsWorld\data\ecosystem-boot.log"
If Not fso.FolderExists("C:\xampp\htdocs\JobsWorld\data") Then
  fso.CreateFolder "C:\xampp\htdocs\JobsWorld\data"
End If

Sub LogLine(msg)
  On Error Resume Next
  Dim tf
  Set tf = fso.OpenTextFile(logFile, 8, True)
  tf.WriteLine Now & " " & msg
  tf.Close
End Sub

LogLine "boot begin"

node = "C:\Program Files\nodejs\node.exe"
If Not fso.FileExists(node) Then node = "C:\Program Files (x86)\nodejs\node.exe"
If Not fso.FileExists(node) Then
  LogLine "ERROR no node.exe"
  WScript.Quit 1
End If
LogLine "node=" & node

Sub StartTray(workDir, scriptName)
  On Error Resume Next
  If Not fso.FolderExists(workDir) Then
    LogLine "ERROR no dir " & workDir
    Exit Sub
  End If
  If Not fso.FileExists(workDir & "\" & scriptName) Then
    LogLine "ERROR no file " & scriptName
    Exit Sub
  End If
  sh.CurrentDirectory = workDir
  Err.Clear
  sh.Run """" & node & """ " & scriptName, 0, False
  If Err.Number <> 0 Then
    LogLine "FAIL " & scriptName & " err=" & Err.Number & " " & Err.Description
    Err.Clear
  Else
    LogLine "START " & scriptName
  End If
End Sub

StartTray "C:\xampp\htdocs\JobsWorld\scripts\tray", "jobsworld_tray.js"
StartTray "C:\xampp\htdocs\captajaus\pm2-tray", "captajaus_tray.js"
StartTray "C:\xampp\htdocs\ALimpiezas\tools\tray", "alimpiezas_tray.js"
StartTray "C:\Users\Paterna\Desktop\Royal Broker BERTO\indicadorFenix\RoyalBrokerBot\tray", "royalbroker_tray.js"

LogLine "boot end"
WScript.Quit 0
