' docopy.vbs
' This script (re)calculates the build number, creates a directory on the target machine
' with that build number as a directory name, then copies all the files identified in the 
' filecopy.dat file to the destinations noted there.  

BUILDDEST = "\\b11nlbuilds\sapi5"
BUILDER = "spgbld"
BUILDMAIL = "spgmake"

on error resume next

' VerifyPath takes the destination from the filecopy.dat, as modified to include BUILDDEST
' and the build number, and looks to see if the destination actually exists.  It starts from
' the root of the name, BUILDDEST, and checks at each node to see if that folder exists; if
' not, it creates that folder, then continues down the destination name.  

sub VerifyPath(pathname)
	if right(pathname,1) <> "\"	then	'strip off filename, if any
		beginfname = InStrRev(pathname, "\")
		pathname = left(pathname,len(pathname) - beginfname)
	end if
	lasttry = instr(pathname, BUILDDEST) + len(BUILDDEST)	' offset for known directory
	do while lasttry < (len(pathname) - 1)
		ptr = InStr(lasttry + 2, pathname, "\") - 1	'end of path component
		trial = left(pathname, ptr)
		if not fso.FolderExists(trial) then
			fso.CreateFolder(trial)
		end if
		lasttry = ptr
	loop
end sub

set wso = CreateObject("WScript.Shell")
USER = wso.ExpandEnvironmentStrings("%USERNAME%")
SAPIROOT = wso.ExpandEnvironmentStrings("%SAPIROOT%")

' The following if..then ensures that only the official builder will do
' the copy functions through the buildall command file.  If the username
' in the environment is not that of the official builder, the loop will
' not be executed.  

if USER = BUILDER then

	set fso = CreateObject("Scripting.FileSystemObject")
	' The following retrieves the build number from the 
	' currver.inc file that is generated by the build process
	set verfile = fso.GetFile(SAPIROOT & "\build\currver.inc")
	set fstream = verfile.OpenAsTextStream
	do while fstream.AtEndOfStream <> True
		linein = fstream.ReadLine
		if left(linein, 15) = "#define VERSION" then
			BuildNum = left(right(linein, 5), 4)
		end if
	loop
	fstream.Close()
	' Now read the data file to find out which files to copy and
	' where to put them.  
	set file = fso.GetFile(SAPIROOT & "\builder\filecopy.dat")
	set fstream = file.OpenAsTextStream
	do while fstream.AtEndOfStream <> True
		linein = fstream.ReadLine
		if left(linein, 1) <> "#" then
			fromto = split(linein, ",")
			filefrom = SAPIROOT & "\" & trim(fromto(0))
			fileto = BUILDDEST & "\" & BuildNum & "\" & trim(fromto(1))
			VerifyPath(fileto)
			fso.CopyFile filefrom, fileto
			if err.number <> 0 then
				wscript.echo "Error: " & err.description & ": copying " & filefrom & " to " & fileto
				err.clear
			end if
		end if
	loop
	fstream.Close()
	' Now an xcopy to get all the source & pdbs
	wso.Run("xcopy /s /i " & SAPIROOT & "\src\*.cpp " & BUILDDEST & "\" & BuildNum & "\src")
	wso.Run("xcopy /s /i " & SAPIROOT & "\src\*.h " & BUILDDEST & "\" & BuildNum & "\src")
	wso.Run("xcopy /s /i " & SAPIROOT & "\src\*.pdb " & BUILDDEST & "\" & BuildNum & "\src")
	wso.Run("xcopy /s /i " & SAPIROOT & "\QA\*.cpp " & BUILDDEST & "\" & BuildNum & "\src\QA")
	wso.Run("xcopy /s /i " & SAPIROOT & "\QA\*.h " & BUILDDEST & "\" & BuildNum & "\src\QA")
	wso.Run("xcopy /s /i " & SAPIROOT & "\QA\*.pdb " & BUILDDEST & "\" & BuildNum & "\src\QA")
	' Clean up attributes on copied files
	wso.Run("attrib -r -h " &  BUILDDEST & "\" & BuildNum & " /s")
	' Get rid of slm.ini files from copied files
	wso.Run("del /s " & BUILDDEST & "\" & BuildNum & "\slm.ini")

else
	permstring = "You are not the official builder, therefore your build "
	permstring = permstring & "will not be copied to the build repository.  Please look "
	permstring = permstring & "for your result binaries in the appropriate directories."
	msgbox permstring, , "SPG Build Process"
end if
