'
' Example script for 2BrightSparks SyncBackPro V8 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=Basic
'
' http://www.2BrightSparks.com/
'

' The name of the variable you want. It should be unique.
const MyVarName = "CustomIncVar"

' Set MinVarValue to what the variables minimum value should be
const MinVarValue = 1

' Set MaxVarValue to what the maximum variables value should be
const MaxVarValue = 12

' Should it be a full backup when it goes over the maximum value?
const FullBackupWhenOver = TRUE

'-----------------------------------------------------------------------------
'-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 " & MyVarName & ". Values from " & CStr(MinVarValue) & " to " & CStr(MaxVarValue)
  If FullBackupWhenOver then
    Description = Description & ". Does full backup." 
  End If
  
  ScriptType = SCRIPTTYPE_RUN + SCRIPTTYPE_CONFIG
End Function

'
' Remind user to edit this script before use
'
Function Install(Interactive)
  If Interactive Then
    MsgBox("Please edit the script file " & SBSystem.ScriptFilename & " before you use it.")
  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

  ' 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
        DidReset = FullBackupWhenOver
      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
