//
// Example script for 2BrightSparks SyncBackPro that stops a profile
// from running if a specific file does NOT exist. The file can then
// be optionally deleted automatically if it does exist (to stop the
// profile being run again until the file is re-created).
//
// This can be useful when files are being copied to a folder and
// you do not want to copy them until they have all been copied.
// This assume you are using a special "marker" file that will
// always be the last file to be copied/created.
//
// SBLang=Pascal
//
// http://www.2BrightSparks.com/
//

//
// This is a configuration script and runtime script
//
function Description(var ScriptType);
begin
  Result:='The profile will not run unless a specific file exists';
  ScriptType:=SCRIPTTYPE_RUN + SCRIPTTYPE_CONFIG;
end;

function RunDisabledCheck(NoLog);
var
  markerfilename, deletemarker, FSO;
begin
  NoLog:=TRUE;
  Result:='';

  markerfilename:=SBVariables.GetProperty('MarkerFilename', '', FALSE);
  if (markerfilename <> '') then begin
    FSO:=CreateOleObject('Scripting.FileSystemObject');
    if not FSO.FileExists(markerfilename) then
      Result:='File does not exist: ' + markerfilename
    else if (SBVariables.GetProperty('MarkerDelete', '', FALSE) = 'Y') then
      FSO.DeleteFile(markerfilename, True);
    FSO:=Unassigned;
  end;
end;

//
// Configuration part of the script
//

// Not for group profiles, and only display setup page if the profile
// is using the script.
function ConfigWantSetupNode(IsGroup);
begin
  Result:=not IsGroup and SBProfile.UsesScript;
end;

// Reset to factory defaults
procedure ConfigFactoryDefaults;
begin
  SBVariables.DeleteProperty('MarkerFilename');
  SBVariables.DeleteProperty('MarkerDelete');
end;

// Can revert to factory settings
function ConfigCanRevert;
begin
  Result:=TRUE;
end;

// Always allow the setup page to close
function ConfigCanClose;
begin
  Result:=TRUE;
end;

// Setup node caption
function ConfigNodeCaption;
begin
  Result:='File Exists Check';
end;

// Create setup window display
procedure ConfigSetupDisplay;
begin
  SBProfile.AddEdit('Full filename of the file that must exist', 256, FALSE, FALSE, 1);
  SBProfile.AddCheckbox('Delete file automatically', 2);
end;

// Load settings. Return error message on failure.
function ConfigLoadSettings;
begin
  Result:='';

  SBProfile.SetEdit(SBVariables.GetProperty('MarkerFilename', '', FALSE), 1);
  SBProfile.SetCheckbox(SBVariables.GetProperty('MarkerDelete', '', FALSE) = 'Y', 2);
end;

// Save settings. Return error message on failure.
function ConfigSaveSettings(Silent);
begin
  Result:='';

  SBVariables.SetProperty('MarkerFilename', SBProfile.GetEdit(1));
  if SBProfile.GetCheckbox(2) then
    SBVariables.SetProperty('MarkerDelete', 'Y')
  else
    SBVariables.SetProperty('MarkerDelete', 'N');
end;

// The settings have changed
procedure ConfigUpdateConditionals;
begin
  SBProfile.EnableControl(SBProfile.GetEdit(1) <> '', 2);
end;
