'
' Example script for 2BrightSparks SyncBackPro V8 that shows how
' to have a "Message Of The Day" which is retrieved from a
' web server and displayed (if it is new).
'
' How this works:
'
' - Put a message of the day file on your web server. It would
'   be a good idea to make it a small file otherwise there
'   will be a delay when starting SyncBackPro.
'
' - Put the full URL of that file into the MOTD setting below
'
' - Change the MOTDEXT setting below to the extension of that
'   Message Of The Day file.
'
' - Install the script into SyncBackPro, enable it, and then
'   restart SyncBackPro
'
' How to improve this script:
'
' Instead of downloading the entire message of the day file
' each time, you could instead have two files on your web
' server: a counter file, and the message of the day file. The
' counter file would be a small file that you change each time
' the message of the day file is changed. So instead of this
' script downloading the message file and comparing it to the
' local message file, it would instead download the counter
' file and use that for comparison. If different, then download
' the message of the day file.
'
' Even better: use a PHP script that calculates the CRC32 hash
' value of the message of the day file on the server, and then
' compare that with the local file. If they are different then
' download the new message of the day file.
'
'
' SBLang=VBScript
'
' http://www.2BrightSparks.com/
'

' The full URL of the Message Of The Day file
const MOTD = "http://www.2brightsparks.com/syncback/sbpromotd.txt"

' The extension of the Message of The Day File. It could be a text file,
' a Word document, a picture, MP3, etc.
const MOTDEXT = ".txt"

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

'
' This is a main interface script
'
Function Description(ByRef ScriptType)
  Description = "Message of the day"
  ScriptType = SCRIPTTYPE_MAIN
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

'
' Display the MOTD unless we've already read it.
'
Sub MainStarted(Unattended)
  ' If unattended then exit now
  If Unattended Then
    Exit Sub
  End If

  ' Where should we store our MOTD files?
  MOTDFile = SBVariables.GetVar("%USERPROFILE%")
  If (MOTDFile = "%USERPROFILE%") or (MOTDFile = "") then
    MOTDFile = SBVariables.GetVar("%TMP%")
    If (MOTDFile = "%TMP%") or (MOTDFile = "") then
      MOTDFile = SBVariables.GetVar("%TEMP%")
      If (MOTDFile = "%TEMP%") or (MOTDFile = "") then
        ' Give up!
        Exit Sub
      End If
    End If
  End If
  If not SBSystem.IsFolder(MOTDFile) Then
    MOTDFile = MOTDFile & "\"
  End If
  MOTDFile = MOTDFile & "motd" & MOTDEXT
  NewMOTDFile = MOTDFile & ".new"
 
  ' Download the file
  Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")
  objXMLHTTP.open("GET", MOTD, false)
  objXMLHTTP.send
  If objXMLHTTP.Status = 200 Then
    Set objADOStream = CreateObject("ADODB.Stream")
    objADOStream.Open
    objADOStream.Type = 1 'adTypeBinary
 
    objADOStream.Write(objXMLHTTP.ResponseBody)
    objADOStream.Position = 0
 
    Set objFSO = Createobject("Scripting.FileSystemObject")
    If objFSO.Fileexists(NewMOTDFile) Then objFSO.DeleteFile(NewMOTDFile)
    Set objFSO = Nothing
 
    objADOStream.SaveToFile(NewMOTDFile)
    objADOStream.Close

    Set objADOStream = Nothing
    Set objXMLHTTP = Nothing
  Else
    Exit Sub
  End if

  ' Is it different from the previous file?
  If SBSystem.CRC32(MOTDFile) <> SBSystem.CRC32(NewMOTDFile) Then
    ' Replace the existing MOTD file
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.Fileexists(MOTDFile) Then objFSO.DeleteFile(MOTDFile)
    objFSO.MoveFile(NewMOTDFile, MOTDFile)
    Set objFSO = Nothing

    ' Display the message
    SBSystem.OpenFile(MOTDFile)
  Else
    ' MOTD has not changed
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.Fileexists(NewMOTDFile) Then objFSO.DeleteFile(NewMOTDFile)
    Set objFSO = Nothing
  End If
End Sub
