REM $Id$
const archive_dest = "\\advwb1.opcs-online.info\archivelogs"
const logship_user = "logship"
const logship_pwd = "logship"
const archive_source = "\\advwb2.opcs-online.info\archivelogs"
const strFrom = "webmip@AdvanticaGroup.com"
const strTo = "Andrew.Hardy@AdvanticaGroup.com"
const strSub = "MIP: Logship"
const strSubError = "MIP: Logship Error"
strBody = "Logship Completed Successfully"
const strSMTP = "relay01.sleek.net"


CopyArchives archive_source,archive_dest, logship_user, logship_pwd

Sub CopyArchives(strSource, strDest, strUser, strPwd)
	Dim strLocalDrive
	strLocalDrive = "L:"

	Dim strRemoteDrive
	strRemoteDrive = "R:"

	'error handling
	On Error Resume Next

	Set objNetwork = CreateObject("WScript.Network")
	
	objNetwork.MapNetworkDrive strLocalDrive, strSource, false, strUser, strPwd

	'handle error
	If Err.Number <> 0 Then
		SendEmail strFrom, StrTo, strSubError, "Error Mapping " & strSource & ":" & Err.Description, StrSMTP
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If

	objNetwork.MapNetworkDrive strRemoteDrive, strDest, false, strUser, strPwd


	'handle error
	If Err.Number <> 0 Then
		SendEmail strFrom, StrTo, strSubError, "Error Mapping " & strDest & ":" & Err.Description, StrSMTP
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If

	set fileSystem = createobject("Scripting.FileSystemObject")

        WScript.Echo "Getting fldrSource"
	set fldrSource = fileSystem.GetFolder(strLocalDrive)

	'handle error
	If Err.Number <> 0 Then
		SendEmail strFrom, StrTo, strSubError, "Error Getting Local Folder " & strLocalDrive & ":" & Err.Description, StrSMTP
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If


        WScript.Echo "Getting fldrDest"
	set fldrDest = fileSystem.GetFolder(strRemoteDrive)

	'handle error
	If Err.Number <> 0 Then
		SendEmail strFrom, StrTo, strSubError, "Error Getting Remote Folder " & strRemoteDrive & ":" & Err.Description, StrSMTP
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If


        WScript.Echo "Looking at directory " & strSource
	for each file in fldrSource.Files
		if not filesystem.FileExists(strDest & "\" & file.Name) then
			WScript.Echo "Copying " & file.Name & " to " & strDest & "\" & file.Name
                        file.Copy (strDest & "\" & file.Name)
		end if
        next

	'handle error
	If Err.Number <> 0 Then
		SendEmail strFrom, StrTo, strSubError, "Error During File Copy:" & Err.Description, StrSMTP
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If

	objNetwork.RemoveNetworkDrive strRemoteDrive
	'handle error
	If Err.Number <> 0 Then
		SendEmail strFrom, StrTo, strSubError, "Error Removing Remote Folder " & strRemoteDrive & ":" & Err.Description, StrSMTP
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If

	objNetwork.RemoveNetworkDrive strLocalDrive
	'handle error
	If Err.Number <> 0 Then
		SendEmail strFrom, StrTo, strSubError, "Error Removing Local Folder " & strLocalDrive & ":" & Err.Description, StrSMTP
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If

	SendEmail strFrom, StrTo, strSub, strBody, StrSMTP
	
End Sub

' From the book "Windows Server Cookbook" by Robbie Allen
' ISBN: 0-596-00633-0

Sub SendEmail(strFrom, StrTo, strSub, strBody, StrSMTP)
	set objEmail = CreateObject("CDO.Message")
	objEmail.From = strFrom
	objEmail.To = strTo
	objEmail.Subject = strSub
	objEmail.Textbody = strBody
	objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTP
	objEmail.Configuration.Fields.Update
	objEmail.Send
	If Err.Number <> 0 Then
		Wscript.Echo "Error number " & Err.Number & " occured: " & Err.Description
		Wscript.Echo ""
		Wscript.Quit Err.Number
	End If
	WScript.Echo "Email sent"
End Sub