'
' Example script for 2BrightSparks SyncBackPro V8 that waits for a program to
' finish before running the profile. If its a simulated run, or a restore,
' then it can be run regardless.
'
' SBLang=Basic
'
' http://www.2BrightSparks.com/
'

' The number of seconds to wait before re-checking if it is still running
const RetrySecs = 5

'-----------------------------------------------------------------------------
'-DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING !-
'-----------------------------------------------------------------------------

'
' This is a profile configuration and runtime script 
'
Function Description(ByRef ScriptType)
  Description = "Wait for a program to finish before starting profile"
  ScriptType = SCRIPTTYPE_RUN + SCRIPTTYPE_CONFIG
End Function

'
' Do nothing
'
Function Install(Interactive)
  Install = ""
End Function

'
'
Function RunDisabledCheck(NoLog)
  ' Running simulated or as a restore?
  If SBRunning.Restore or SBRunning.Simulated then
    RunDisabledCheck = ""
    NoLog = FALSE
    Exit Function
  End If

  SBSystem.UpdateFileStatus("Waiting for " & CStr(SBVariables.GetProperty("WFProcessName", "Notepad.exe", FALSE)) & "...")

  ' Create object to tell us what processes are running
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objShell = CreateObject("WScript.Shell")

  Do
    ' Is the process running? Could be more than one process.
    Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = '" & CStr(SBVariables.GetProperty("WFProcessName", "Notepad.exe", FALSE)) & "'")
    RunProfile = (colProcessList.Count < 1)

    If RunProfile Then
      ' The program is not running
      RunDisabledCheck = ""
      NoLog = FALSE
      Exit Do
    Else
      ' The program is (still) running
      RunDisabledCheck = "The program is still running"
      NoLog = TRUE
    End If

    ' Have we been told to abort?
    if SBRunning.Abort then
      ' Let SyncBack see that the user aborted
      RunDisabledCheck = ""
      NoLog = FALSE
      Exit Do
    End If

    ' Wait RetrySecs seconds and try again
    if SBRunning.Sleep(Int(SBVariables.GetProperty("WFRetrySecs", 5, FALSE))) then
      ' User has aborted profile
      RunDisabledCheck = ""
      NoLog = FALSE
      Exit Do
    end if
  Loop While 1=1

  Set objShell = nothing
End Function

'
' Configuration part of the script
'

' Not for group profiles, and only display setup page if the profile
' is using the script.
Function ConfigWantSetupNode(IsGroup)
  ConfigWantSetupNode = not IsGroup and SBProfile.UsesScript
End Function

' Reset to factory defaults
Sub ConfigFactoryDefaults
  SBVariables.DeleteProperty("WFProcessName")
  SBVariables.DeleteProperty("WFRetrySecs")
End Sub

' Always allow the setup page to close
Function ConfigCanClose
  ConfigCanClose = TRUE
End Function

' Can revert to factory settings
Function ConfigCanRevert
  ConfigCanRevert = TRUE
End Function

' Setup node caption
Function ConfigNodeCaption
  ConfigNodeCaption = "Wait For Finish"
End Function

' Create setup window display
Sub ConfigSetupDisplay
  SBProfile.AddEdit("The name of the process you want to have finished running", 128, FALSE, FALSE, 1)
  SBProfile.AddEdit("The number of seconds to wait before re-checking if it is still running", 10, TRUE, FALSE, 2)
End Sub

' Load settings. Return error message on failure.
Function ConfigLoadSettings
  ConfigLoadSettings = ""

  SBProfile.SetEdit(SBVariables.GetProperty("WFProcessName", "Notepad.exe", FALSE), 1)
  SBProfile.SetEdit(SBVariables.GetProperty("WFRetrySecs", 5, FALSE), 2)
End Function

' Save settings. Return error message on failure.
Function ConfigSaveSettings(Silent)
  ConfigSaveSettings = ""
 
  if SBProfile.GetEdit(1) = "" then
    ConfigSaveSettings = "The process name cannot be empty!"
    Exit Function
  end if

  if Int(SBProfile.GetEdit(2) < 1) then
    ConfigSaveSettings = "The retry secionds cannot be less than 1!"
    Exit Function
  end if

  SBVariables.SetProperty("WFProcessName", SBProfile.GetEdit(1))
  SBVariables.SetProperty("WFRetrySecs", SBProfile.GetEdit(2))
End Function

' The settings have changed
Sub ConfigUpdateConditionals
End Sub
