' Filename: backup.vbs
' Author  : Kevin Weilbacher (kweilbac@gte.net)
'           Copyright ©2001, 2002 Kevin R. Weilbacher
'
' Version 1.0e 07.30.2002 Backup files can now be stored in a different drive or
'                         directory than the .tlx file itself
' Version 1.0d 07.28.2002 Create/maintain logfile of backups; 
'                         Allow user to cancel/exit script
' Version 1.0c 07.25.2002 Add parameter for # of prior versions
' Version 1.0b 06.30.2002 Include an opening prompt
' Version 1.0a 12.29.2001 Modified to work with CuteSITE Builder
' Version 1.0  07.08.2001 Initial version
' ----------------------------------------------------------------------------
On Error Resume Next

Dim strFileName, strLogName, strDir, StrProgram, strBackupDir

' INSTRUCTIONS:
'
' There are 6 variables that you can edit:
'
'   Max           How many prior versions of your .tlx file to retain
'   strFileName   Actual name of your CSB .tlx file
'   strDir        Full disk & directory name where your CSB .tlx file exists
'   strLogName    ActualThis is the name of the log file to create and update
'   strBackupDir  Full disk & directory name where the backup files will be stored
'   strProgram    This is the full name name of the CSB 3.0 executable program
'
' These variables are listed in the section marked: *** START CUSTOMIZATION AREA ***
' immediately below.
'
' *** START CUSTOMIZATION AREA ***
'
Max = 5
strFileName = "SampleCSB.tlx"
strDir = "C:\My Documents\"
strLogName  = "CSB_Backup.log"   
strBackupDir = "E:\Backup\"
strProgram = "C:\Program Files\Globalscape\CuteSITE Builder\program\csb.exe"
'
' *** END CUSTOMIZATION AREA ***
'
' *** DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING! ***
'
  Dim fs, f, WSHShell, strToday, strResponse, strTitle
  strToday = Date & " " & Time
  strVersion = "Version 1.0e  7.30.2002"
  strTitle = "CSB Backup Script"
  strFilePrefix = Split(strFileName, ".")(0)
  strTlxFileName = strDir & strFileName
  strBackupPrefix = strBackupDir & strFilePrefix & "_Backup_"
  strLogFileName = strDir & strLogName
  strSpace = " "

  xx = ReportFolderStatus(strDir) 'Verify that file directory exists
  xx = FileExists(strTlxFileName, N) 'Verify that CSB .tlx file exists
  xx = ReportFolderStatus(strBackupDir) 'Verify that backup directory exists

' Get info on .tlx file (Date last modified, file size)
  strModified = FileLastModified(strTlxFileName)
  strFileSize = GetFileSize(strTlxFileName)

  strLine1 = "Today is " & strToday & vbCrLf & vbCrLF
  strLine2 = "Ready to backup file: " & strFileName & " " & vbCrLf & vbCrLf
  strLine3 = "File location: " & strDir & "     " & vbCrLf
  strLine4 = "Last modified: " & strModified & "  File size: " & strFileSize & vbCrLf &vbCrLf
  strLine5 = "Prior versions to maintain = " & Max & vbCrLf
  strLine6 = "Backup location: " & strBackupDir & "     " & vbCrLf & vbCrLf
  strLine7 = "Copyright ©2001,2002 Kevin R. Weilbacher" & vbCrLf & strVersion & vbCrLf & vbCrLf
  
  strMsg = strLine1 & strLine2 & strLine3 & strLine4 & strLine5 & strLine6 & strLine7
  strResponse = MsgBox (strMsg, 1, strTitle)
  If strResponse = vbCancel Then
	WScript.Quit(0)
  End If
    
' This is the section where we make the backup copies of .tlx file
  For XNew = Max to 1 step -1
    Old = XNew - 1
    If Old = 0 Then
	strSource = strTlxFileName
    Else
	strSource = strBackupPrefix & Old & ".tlx"
    End If
    strTarget = strBackupPrefix & XNew & ".tlx"
    Set fs = CreateObject("Scripting.FileSystemObject")
    fs.CopyFile strSource, strTarget
  Next
  Set fs = Nothing

' Update Log File
  xx = UpdateLogFile(strLogFileName)

  strLine1 = vbCrLf & "File " & strFileName & ".tlx successfully backed up  " 
  strLine2 = vbCrLf & "to directory: " & strBackupDir  & vbCrLf & vbCrLf
  strLine3 = "Click OK to start CuteSITE Builder" & vbCrLf
  strMsg = strLine1 & strLine2
  strResponse = MsgBox (strMsg, 1, strTitle)
  If strResponse = vbCancel Then
    WScript.Quit(0)
  End If

' Start CuteSITE Builder program (open normal and don't wait) using .tlx file

  Set WSHShell = WScript.CreateObject("WScript.Shell")
  WshShell.Run """" & strProgram & """" & strSpace & """" & strTlxFileName & """", 1, True
  Set WSHShell = Nothing
  WScript.Quit(0)
'
' *** End of Program ***
'
' *** FUNCTIONS ***
'
'Return the date/time a file was last modified
Function FileLastModified(Fname)

  FileLastModified = ""
  Set fs = CreateObject("Scripting.FileSystemObject")

  If fs.FileExists(Fname) = True Then 
    Set f = fs.GetFile(Fname)
    FileLastModified = f.DateLastModified
  End If 
  
  Set f = Nothing
  Set fs = Nothing
End Function

'Return 0 if a file exists else -1
Function FileExists(Fname,sCreatefile)

  Set fs = CreateObject("Scripting.FileSystemObject")
  
  if fs.FileExists(Fname) = False then
    FileExists = -1
    If sCreateFile = "Y" Then
    	fs.CreateTextFile(Fname)
    	strMsg = "Log file created:" & vbCrLf & vbCrLf & Fname & "     " & vbCrLf & vbCrLf
    	MsgBox strMsg,64,strTitle
    Else
    	strMsg = "File not found:" & vbCrLF & vbCrLf & strTlxFileName & "   " & vbCrLf & vbCrLf
	MsgBox strMsg,48,strTitle
	WScript.Quit(0)
    End If
  Else 
    FileExists = 0
  End If
Set fs = Nothing
End Function

'Return 0 if directory exists else -1
Function ReportFolderStatus(fldr)

   Set fs = CreateObject("Scripting.FileSystemObject")
   If (fs.FolderExists(fldr)) Then
        ReportFolderStatus = 0
   Else
        ReportFolderStatus = -1
    	strMsg = "Directory " & fldr & " doesn't exist!     " & vbCrLf & vbCrLf
	MsgBox strMsg,48,strTitle
	WScript.Quit(0)
   End If
Set fs = Nothing
End Function

'Return the length of a file or -1 if it does not exist
Function GetFileSize(Fname)

  GetFileSize = -1
  Set fs = CreateObject("Scripting.FileSystemObject")

  if fs.FileExists(Fname) = True then
    set f = fs.GetFile(Fname)
    GetFileSize = f.size
  end if
  if fs.FolderExists(Fname) = True then
    set f = fs.GetFolder(Fname)
    GetFileSize = f.size
  end if

  Set f = Nothing
  Set fs = Nothing
End Function

'Update log file
Function UpdateLogFile(Fname)

  xx = FileExists(Fname, "Y")

  Set fs = CreateObject("Scripting.FileSystemObject")
  Set f = fs.GetFile(Fname)
  Set ts = f.OpenAsTextStream(8,-2)
  ts.write(strToday & ": Backup file: " & strTlxFileName & " File size: " & strFileSize & vbCrLf)
  ts.close

  Set f = Nothing
  Set fs = Nothing
  Set ts = Nothing
End Function
