'
' Example script for 2BrightSparks SyncBackPro V8 that stops a profile from
' running if too many files will be copied or moved. Does nothing if it
' is a restore.
'
' SBLang=Basic
'
' http://www.2BrightSparks.com/
'

'-----------------------------------------------------------------------------
'-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 = "Stops a profile from running if too many files will be copied or moved"
  ScriptType = SCRIPTTYPE_RUN + SCRIPTTYPE_CONFIG
End Function

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

'
' Called before any copying etc. is done
'
Function RunPreCopyCheck
  ' Restoring?
  If SBRunning.Restore then
    RunPreCopyCheck = ""
    Exit Function
  End If

  ' Too many files being copied?
  if SBVariables.GetProperty("PCMaxCopyEnabled", "N", FALSE) = "Y" then
    MaxCopy = Int(SBVariables.GetProperty("PCMaxCopy", 0, FALSE))

    ' 17 = files to be copied to destination
    ' 18 = files to be copied to source
    BeingCopied = SBRunning.GetRuntimeValue(17) + SBRunning.GetRuntimeValue(18)

    if BeingCopied > MaxCopy then
      RunPreCopyCheck = "Too many files (" + CStr(BeingCopied) + ") are being copied (max is " + CStr(MaxCopy) + ")"
      Exit Function
    End if
  End If

  ' Too many files being moved?
  if SBVariables.GetProperty("PCMaxMoveEnabled", "N", FALSE) = "Y" then
    MaxMove = Int(SBVariables.GetProperty("PCMaxMove", 0, FALSE))

    ' 19 = files to be moved to destination
    ' 20 = files to be moved to source
    BeingMoved = SBRunning.GetRuntimeValue(19) + SBRunning.GetRuntimeValue(20)

    if BeingMoved > MaxMove then
      RunPreCopyCheck = "Too many files (" + CStr(BeingMoved) + ") are being moved (max is " + CStr(MaxMove) + ")"
      Exit Function
    End if
  End If
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("PCMaxCopyEnabled")
  SBVariables.DeleteProperty("PCMaxCopy")
  SBVariables.DeleteProperty("PCMaxMoveEnabled")
  SBVariables.DeleteProperty("PCMaxMove")
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 = "Stop Changes"
End Function

' Create setup window display
Sub ConfigSetupDisplay
  SBProfile.AddCheckbox("Limit the number of files that can be copied", 1)
  SBProfile.AddEdit("The maximum number of files that can be copied", 16, TRUE, FALSE, 2)
  SBProfile.AddCheckbox("Limit the number of files that can be moved", 3)
  SBProfile.AddEdit("The maximum number of files that can be moved", 16, TRUE, FALSE, 4)
End Sub

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

  if SBVariables.GetProperty("PCMaxCopyEnabled", "N", FALSE) = "Y" then
    SBProfile.SetCheckbox(TRUE, 1)
  else
    SBProfile.SetCheckbox(FALSE, 1)
  end if
  SBProfile.SetEdit(SBVariables.GetProperty("PCMaxCopy", 0, FALSE), 2)

  if SBVariables.GetProperty("PCMaxMoveEnabled", "N", FALSE) = "Y" then
    SBProfile.SetCheckbox(TRUE, 3)
  else
    SBProfile.SetCheckbox(FALSE, 3)
  end if
  SBProfile.SetEdit(SBVariables.GetProperty("PCMaxMove", 0, FALSE), 4)
End Function

' Save settings. Return error message on failure.
Function ConfigSaveSettings(Silent)
  ConfigSaveSettings = ""

  If SBProfile.GetCheckbox(1) Then
    ' copy-max is selected - check value
    if Int(SBProfile.GetEdit(2) < 1) then
      ConfigSaveSettings = "The max copy value cannot be less than 1!"
      Exit Function
    end if

    SBVariables.SetProperty("PCMaxCopyEnabled", "Y")
    SBVariables.SetProperty("PCMaxCopy", SBProfile.GetEdit(2))
  else
    SBVariables.SetProperty("PCMaxCopyEnabled", "N")
  end if

  If SBProfile.GetCheckbox(3) Then 
    ' move-max is selected - check value
	if Int(SBProfile.GetEdit(4) < 1) then
      ConfigSaveSettings = "The max move value cannot be less than 1!"
      Exit Function
    end if

    SBVariables.SetProperty("PCMaxMoveEnabled", "Y")
    SBVariables.SetProperty("PCMaxMove", SBProfile.GetEdit(4))
  Else  
    SBVariables.SetProperty("PCMaxMoveEnabled", "N")
  End If
End Function

' The settings have changed
Sub ConfigUpdateConditionals
  SBProfile.EnableControl(SBProfile.GetCheckbox(1), 2)
  SBProfile.EnableControl(SBProfile.GetCheckbox(3), 4)
End Sub
