'
' Example script for 2BrightSparks SyncBackPro that lets you create your own
' incrementing variable. You can also configure if it should be a full backup
' once it goes over a certain number.
'
' SBLang=VBScript
'
' http://www.2BrightSparks.com/
'
'-----------------------------------------------------------------------------
'-DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING !-
'-----------------------------------------------------------------------------

' This is set to TRUE if it should be a full backup because the variables
' value went over MaxVarValue and FullBackupWhenOver is TRUE 
Dim DidReset

'
' This is a runtime script and a configuration script
'
' It must be a configuration script otherwise the variable will not be used
' when you modify the script
'
Function Description(ByRef ScriptType)
  Description = "Custom incrementing variable"
  
  ScriptType = 1 + 2
End Function

'
' Remind user to edit the profiles settings before use
'
Function Install(Interactive)
  If Interactive Then
    MsgBox "Please remember to change the settings as appropriate for those profiles using this script."
  End If
End Function

'
' This subroutine is called when Pro needs to know if it should be a
' full backup or not.
'
Sub RunDoFullBackup(LeftDir, RightDir, ByRef FullBackup)
  if DidReset then
    SBRunning.DebugOut "Full backup", "", 5
    FullBackup = TRUE
  end if
End Sub

'
' This subroutine is called when the variables are initialised. This is either
' when the profile is run, or when the variables are being checked during profile
' configuration. The check is made to make sure the variable actually exists.

' Note that you need to be careful as many things haven not been initialised or
' created when this is called, e.g. no locations. It is called very early during
' the profile run.
'
Sub InitialiseVars(Checking)
  DidReset = FALSE

  MyVarName = SBVariables.GetProperty("IncVarName", "CustomIncVar", FALSE)
  MinVarValue = Int(SBVariables.GetProperty("IncVarMin", 1, FALSE))
  MaxVarValue = Int(SBVariables.GetProperty("IncVarMax", 12, FALSE))

  ' We store our variable as a property in the profile. This ensures it keeps
  ' its value between runs of Pro and profile runs, i.e. it has state.
  OurVar = CInt(SBVariables.GetProperty(MyVarName, "0", FALSE))
  If Checking then
    DoIncVar = FALSE
  elseif SBRunning.Restore or SBRunning.Simulated then
    DoIncVar = FALSE
  else
    DoIncVar = TRUE
  End If

  If not DoIncVar then
    ' If its a restore or simulated run then we do not increment the value. If
    ' the custom variable has not even been set yet then we use the default
    if OurVar < MinVarValue then
      OurVar = MinVarValue
    elseif OurVar > MaxVarValue then
      OurVar = MaxVarValue
    end if
  else
    ' Increment the value
    SBRunning.DebugOut "Incrementing var value", "", 5

    ' Gone over or under the limit?
    if OurVar < MinVarValue then
      OurVar = MinVarValue
    else
      OurVar = OurVar + 1
      if OurVar > MaxVarValue then
        OurVar = MinVarValue
        if SBVariables.GetProperty("IncVarFullOnMax", "Y", FALSE) = "Y" then
	        DidReset = TRUE
	    end if
      End If
    End If

    ' Store the new value
    SBVariables.SetProperty MyVarName, CStr(OurVar)
    SBRunning.DebugOut MyVarName, CStr(OurVar), 5
  End If

  ' Set the variable value
  SBVariables.SetVar MyVarName, CStr(OurVar)
End Sub

'
' 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.SetProperty "IncVarName", "CustomIncVar"
  SBVariables.SetProperty "IncVarMin", 1
  SBVariables.SetProperty "IncVarMax", 12
  SBVariables.SetProperty "IncVarFullOver", "Y"
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 = "Custom IncVar"
End Function

' Create setup window display
Sub ConfigSetupDisplay()
  SBProfile.AddEdit "The name of the variable you want. It should be unique.", 64, FALSE, FALSE, 1
  SBProfile.AddEdit "The minimum value of the variable", 16, TRUE, FALSE, 2
  SBProfile.AddEdit "The maximum value of the variable", 16, TRUE, FALSE, 3
  SBProfile.AddCheckbox "Should it be a full backup when it goes over the maximum value?", 4
End Sub

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

  SBProfile.SetEdit SBVariables.GetProperty("IncVarName", "CustomIncVar", FALSE), 1
  SBProfile.SetEdit SBVariables.GetProperty("IncVarMin", 1, FALSE), 2
  SBProfile.SetEdit SBVariables.GetProperty("IncVarMax", 12, FALSE), 3
  
  if SBVariables.GetProperty("IncVarFullOnMax", "Y", FALSE) = "Y" then
	SBProfile.SetCheckbox TRUE, 4
  else
    SBProfile.SetCheckbox FALSE, 4
  end if
End Function

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

  if Int(SBProfile.GetEdit(2)) > Int(SBProfile.GetEdit(3)) then
    ConfigSaveSettings = "The minimum value is greater than the maximum value!"
    Exit Function
  end if

  SBVariables.SetProperty "IncVarName", SBProfile.GetEdit(1)
  SBVariables.SetProperty "IncVarMin", SBProfile.GetEdit(2)
  SBVariables.SetProperty "IncVarMax", SBProfile.GetEdit(3)
  
  if SBProfile.GetCheckbox(4) then
    SBVariables.SetProperty "IncVarFullOnMax", "Y"
  else
    SBVariables.SetProperty "IncVarFullOnMax", "N"
  end if
End Function

' The settings have changed
Sub ConfigUpdateConditionals()
  if Int(SBProfile.GetEdit(2)) > Int(SBProfile.GetEdit(3)) then
    SBProfile.SetEdit SBProfile.GetEdit(3), 2
  end if
End Sub
