'******************************************************************************
'*                                                                            *
'* File:   synctree32.vbs     Version  2.01            Date: 2001-09-04       *
'*                                                                            *
'* Copyright (C) 1995-2001 by Kostis Netzwerkberatung                         *
'* Talstr. 25, D-63322 Rödermark, Tel. +49 6074 881056, FAX 881058            *
'* kosta@kostis.net (Kosta Kostis), http://www.kostis.net/                    *
'*                                                                            *
'******************************************************************************
'*                                                                            *
'* History:                                                                   *
'*     2001-09-04: KK V2.01                                                   *
'*        - corrected bug not displaying removed files using /l               *
'*     2001-08-24: KK V2.00                                                   *
'*        - ported from Borland C++ 3.0 and MS-DOS to VBScript and W2K        *
'*        - removed mask option                                               *
'*        - removed option /r                                                 *
'*     1998-09-02: KK V1.11                                                   *
'*        - checking for illegal switch combinations                          *
'*     1998-09-01: KK V1.10                                                   *
'*        - added /R, /I                                                      *
'*     1996-06-02: KK V1.04                                                   *
'*        - added /N                                                          *
'*     1996-05-21: KK V1.03                                                   *
'*        - added /B                                                          *
'*     1995-10-14: KK V1.02                                                   *
'*        - added /T                                                          *
'*     1995-10-02: KK V1.00                                                   *
'*        - initial coding                                                    *
'*                                                                            *
'******************************************************************************

'******************************************************************************
'
'	global variables
'
'******************************************************************************

Dim	wso			' shell object
Dim	fso			' file system object

Dim	SrcDir			' source directory
Dim	DstDir			' destination directory

Dim	ScnLog			' log switch (log)
Dim	ScnAdd			' add switch (add)
Dim	ScnDel			' del switch (delete)
Dim	ScnHid			' hid switch (hidden)
Dim	ScnSub			' sub switch (subdirectories)
Dim	ScnTst			' tst switch (test, don't do anything)
Dim	ScnAtt			' att switch (ignore read only attribute)
Dim	ScnIgn			' dbg switch (debug)

Dim	CTotDir			' total # of scanned directories
Dim	CTotFile		' total # of scanned files
Dim	CTotByte		' total # of scanned Bytes

Dim	CDelDir			' # of deleted directories
Dim	CDelFile		' # of deleted files
Dim	CDelByte		' # of deleted Bytes

Dim	CAddDir			' # of added directories
Dim	CAddFile		' # of added files
Dim	CAddByte		' # of added Bytes

Dim	CUpdFile		' # of updated files
Dim	CUpdByte		' # of updated Bytes

'******************************************************************************
'
'	global constants
'
'******************************************************************************

Const	ERR_NONE = 0		' OK value

Const	ERR_SRC = 5		' source directory does not existing
Const	ERR_DST = 6		' destination directory does not exist

Const	ERR_COPY = 11		' return code if copy file fails
Const	ERR_REMOVE = 12		' return code if file removal fails

Const	ERR_CREATEDIR = 21	' return code if directory creation fails
Const	ERR_REMOVEDIR = 22	' return code if directory removal fails

Const	SCANFLAG_BATCH = 3	' ScnTst Values
Const	SCANFLAG_TEST = 2
Const	SCANFLAG_NOTEST = 0

Const	SCANFLAG_OK = 0		' parameters all valid
Const	SCANFLAG_UNKNOWN = 1	' unknown parameter
Const	SCANFLAG_BADARGS = 2	' illegal parameter(number)
Const	SCANFLAG_COMBINED = 3	' illegal switch combination

'******************************************************************************

Private Sub Hello (ErrMsg)

	WScript.StdErr.WriteLine _
	"Copyright © 1995-2001 by Kostis Netzwerkberatung"

	WScript.StdErr.WriteLine _
	"Talstr. 25, D-63322 Rödermark, Tel. +49 6074 881056, FAX 881058"

	WScript.StdErr.WriteLine _
	"kosta@kostis.de (Kosta Kostis), http://www.kostis.de/"

	WScript.StdErr.WriteLine _
	"synctree V2.01 (2001-09-04)"

	WScript.StdErr.WriteLine ""

	WScript.StdErr.WriteLine _
	"Syntax: synctree32 src dst {switches}"

	WScript.StdErr.WriteLine ""

	WScript.StdErr.WriteLine _
	"Valid optional switches are: [ /t|/b|/l ] /a /d /s /h /n /i /v"

	WScript.StdErr.WriteLine _
	"The following switches are exclusive and cannot be combined: /t /b /l"

	WScript.StdErr.WriteLine ""
	WScript.StdErr.WriteLine ErrMsg

End Sub

'******************************************************************************

Private Function IsScript ()

	IsScript = False
	If (Instr (UCase (WScript.FullName), "CSCRIPT") <> 0) Then
		IsScript = True
	End If

End Function

'******************************************************************************
'
'	Sub:
'		initialize statistics
'
'	Parameter:
'		none
'
'
'*****************************************************************************/

Private Sub InitStatistics

	CTotDir = 1
	CTotFile = 0
	CTotByte = 0

	CDelDir = 0
	CDelFile = 0
	CDelByte = 0

	CAddDir = 0
	CAddFile = 0
	CAddByte = 0

	CUpdFile = 0
	CUpdByte = 0

End Sub

'******************************************************************************
'
'	Sub:
'		display statistics
'
'	Parameter:
'		none
'
'
'*****************************************************************************/

Private Sub DisplayStatistics

	WScript.StdOut.WriteLine "# of directories        : " & CTotDir
	WScript.Stdout.WriteLine "# of files              : " & CTotFile
	WScript.Stdout.WriteLine "# of Bytes              : " & CTotByte

	WScript.StdOut.WriteLine "# of deleted directories: " & CDelDir
	WScript.StdOut.WriteLine "# of deleted files      : " & CDelFile
	WScript.StdOut.WriteLine "# of deleted Bytes      : " & CDelByte

	WScript.StdOut.WriteLine "# of added directories  : " & CAddDir
	WScript.StdOut.WriteLine "# of added files        : " & CAddFile
	WScript.StdOut.WriteLine "# of added Bytes        : " & CAddByte

	WScript.StdOut.WriteLine "# of updated files      : " & CUpdFile
	WScript.StdOut.WriteLine "# of updated Bytes      : " & CUpdByte

End Sub

'******************************************************************************
'
'	Function:
'		parse parameters
'
'	Parameter:
'		argv				parameters object
'
'	Returns:
'		SCANFLAG_OK			all parameters valid
'
'		SCANFLAG_UNKNOWN		unknown parameter
'		SCANFLAG_BADARGS		illegal parameter(number)
'
'*****************************************************************************/

Private Function ScanFlag (argv)

	Dim	i
	Dim	ch
	Dim	parms
	Dim	argc

	parms = 0	' no parameter parsed so far
	argc = argv.Count

	'**********************************************************************
	'
	'	set defaults
	'
	'**********************************************************************

	SrcDir = ""
	DstDir = ""
	ScnLog = False
	ScnAdd = False
	ScnDel = False
	ScnHid = False
	ScnSub = False
	ScnTst = SCANFLAG_NOTEST
	ScnAtt = True
	ScnIgn = False
	ScnDbg = False

	For i = 0 To argc - 1

		'***************************************************************
		'
		'	is it a switch (accept both *NIX and DOS style)?
		'
		'***************************************************************

		ch = Left (argv(i), 1)
		If ((ch = "-") Or (ch = "/")) Then

			'*******************************************************
			'
			'	check switch
			'
			'*******************************************************

			ch = LCase (Mid (argv(i), 2, 1))
			Select Case ch

				Case "a"	' add switch

					ScnAdd = True

				Case "d"	' del switch

					ScnDel = True

				Case "h"	' hid switch

					ScnHid = True

				Case "s"	' sub switch

					ScnSub = True

				Case "n"	' att switch

					ScnAtt = False

				Case "i"	' ign switch

					ScnIgn = True

				Case "v"	' dbg switch

					ScnDbg = True

				Case "l"	' log switch

					If ((ScnLog) Or (ScnTst <> SCANFLAG_NOTEST)) Then
						ScanFlag = SCANFLAG_COMBINED
						Exit Function
					End If

					ScnLog = True

				Case "t"	' tst switch

					If ((ScnLog) Or (ScnTst <> SCANFLAG_NOTEST)) Then
						ScanFlag = SCANFLAG_COMBINED
						Exit Function
					End If

					ScnTst = SCANFLAG_TEST

				Case "b"	' bat switch

					If ((ScnLog) Or (ScnTst <> SCANFLAG_NOTEST)) Then
						ScanFlag = SCANFLAG_COMBINED
						Exit Function
					End If

					ScnTst = SCANFLAG_BATCH

				Case Else	' unknown switch

					ScanFlag = SCANFLAG_UNKNOWN
					Exit Function

			End Select
		Else

		'*******************************************************
		'
		'	is flag, got to be a directory name
		'
		'*******************************************************

			parms = parms + 1	' copy src/dst/mask

			Select Case parms

				Case 1

					SrcDir = argv(i)

				Case 2

					DstDir = argv(i)

				Case Else

					ScanFlag = SCANFLAG_BADARGS
					Exit Function

			End Select

		End If
	Next

	'**********************************************************************
	'
	'	everything's great, last consistency check and out
	'
	'**********************************************************************

	If ((Len (SrcDir) = 0) Or (Len (DstDir) = 0)) Then
		ScanFlag = SCANFLAG_BADARGS
		Exit Function
	End If

	ScanFlag = SCANFLAG_OK

End Function

'******************************************************************************
'
'	Function:
'		remove directory
'
'	Parameter:
'		Dir				directory name
'
'	Returns:
'		ERR_NONE			all OK
'		ERR_REMOVEDIR			unable to remove directory
'
'*****************************************************************************/

Private Function RmDir (Dir)

	RmDir = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	remove (destination) directory
	'
	'**********************************************************************

	CDelDir = CDelDir + 1
	Select Case ScnTst

		Case SCANFLAG_TEST

			WScript.StdOut.WriteLine Dir & " [DIR removed test]"
			Exit Function

		Case SCANFLAG_BATCH

			WScript.StdOut.WriteLine "RMDIR " & Dir
			Exit Function

	End Select

	fso.DeleteFolder (Dir)
	If (ScnLog) Then
		If (Not fso.FolderExists (Dir)) Then
			WScript.StdOut.WriteLine Dir & " [DIR removed]"
		Else
			WScript.StdOut.WriteLine Dir & " [DIR NOT removed - ERROR]"
			RmDir = ERR_REMOVEDIR
			Exit Function
		End If
	End If

End Function

'******************************************************************************
'
'	Function:
'		remove file if needed
'
'	Parameters:
'		Dir				destination directory
'		File				file to be checked for deletion
'
'	Returns:
'		see		DelFile ()
'
'******************************************************************************

Private Function ChkFile (Dir, File)

	Dim	Help
	Dim	Src
	Dim	Dst
	Dim	rc
	Dim	fi
	Dim	fsize

	ChkFile = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	path relative to source dir
	'
	'**********************************************************************

	Help = Right (Dir, Len (Dir) - Len (DstDir))

	'**********************************************************************
	'
	'	create source filename
	'
	'**********************************************************************

	Src = SrcDir & Help & "\" & File

	'**********************************************************************
	'
	'	check if source file is present
	'
	'**********************************************************************

	If (fso.FileExists (Src)) Then
		ChkFile = ERR_NONE
		Exit Function
	End If

	'**********************************************************************
	'
	'	source file not represent, remove destination file
	'
	'**********************************************************************

	Dst = Dir & "\" & File

	CDelFile = CDelFile + 1
	Set fi = fso.GetFile (Dst)
	fsize = fi.size
	CDelByte = CDelByte + fSize

	Select Case ScnTst

		Case SCANFLAG_TEST

			WScript.StdOut.WriteLine Dst & _
				" [removed (" & fsize & " Byte) test]"
			ChkFile = ERR_NONE
			Exit Function

		Case SCANFLAG_BATCH

			WScript.StdOut.WriteLine "DEL " & Dst
			ChkFile = ERR_NONE
			Exit Function

	End Select

	fi.Delete True
	If (ScnLog) Then
		If (Not fso.FileExists (dst)) Then
			WScript.StdOut.WriteLine Dst & _
				" [removed (" & fsize & " Byte)]"
		Else
			WScript.StdOut.WriteLine Dst & _
				" [NOT removed (" & fsize & " Byte) - error]"
			ChkFile = ERR_REMOVE
			Exit Function
		End If
	End If

End Function

'******************************************************************************
'
'	Function:
'		delete files in destination directory
'
'	Parameters:
'		Dir		directory name
'
'	Returns:
'		see		DelFile ()
'
'******************************************************************************

Private Function DelDir (Dir)

	Dim	fo
	Dim	fi
	Dim	i
	Dim	rc

	DelDir = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	open directory
	'
	'**********************************************************************

	Set fo = fso.GetFolder (Dir)
	Set fi = fo.Files

	'**********************************************************************
	'
	'	run through files
	'
	'**********************************************************************

	For Each i In fi
		rc = ChkFile (Dir, i.Name)
		If (rc <> ERR_NONE) Then
			DelDir = rc
			Exit Function
		End If
	Next

End Function

'******************************************************************************
'
'	Function:
'		walk through directories and delete if appropriate
'
'	Parameter:
'		Dir				name of current root
'
'	Returns:
'		ERR_NONE			all OK
'
'*****************************************************************************/

Private Function DelTree (Dir)

	Dim	SubDir
	Dim	fo
	Dim	fi
	Dim	rc

	DelTree = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	process subdirectories first
	'
	'*********************************************************************/

	Set fo = fso.GetFolder (Dir)
	Set fi = fo.Subfolders

	For Each i In fi

		If (Left (i.Name, 1) <> ".") Then

			'******************************************************
			'
			'	process subdirectories
			'
			'******************************************************

			rc = DelTree (Dir & "\" & i.Name)
			If (rc <> ERR_NONE) Then
				DelTree = rc
				Exit Function
			End If
		End If
	Next

	'**********************************************************************
	'
	'	walk through current directory
	'
	'**********************************************************************

	rc = DelDir (Dir)
	If (rc <> ERR_NONE) Then
		DelTree = rc
		Exit Function
	End If

	'**********************************************************************
	'
	'	if not needed, delete directory
	'
	'*********************************************************************'

	SubDir = SrcDir & Right (Dir, Len (Dir) - Len (DstDir))
	If (Not fso.FolderExists (SubDir)) Then
		rc = RmDir (Dir)
		If (rc <> ERR_NONE) Then
			DelTree = rc
			Exit Function
		End If
	End If
	
End Function

'******************************************************************************
'
'	Function:
'		create directory
'
'	Parameter:
'		Dir				directory name
'
'	Returns:
'		ERR_NONE			all OK
'		ERR_CREATEDIR			unable to create directory
'
'*****************************************************************************/

Private Function MkDir (Dir)

	Dim	fo

	MkDir = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	check if destination dir is already present
	'
	'**********************************************************************

	If (Not fso.FolderExists (Dir)) Then

		CAddDir = CAddDir + 1
		Select Case ScnTst

			Case SCANFLAG_TEST
				WScript.StdOut.WriteLine Dir & " [DIR created test]"
				Exit Function

			Case SCANFLAG_BATCH:
				WScript.StdOut.WriteLine "MKDIR " & Dir
				Exit Function

		End Select

		'**************************************************************
		'
		'	create directory
		'
		'*************************************************************/

		Set fo = fso.CreateFolder (Dir)
		If (ScnLog) Then
			If (fso.FolderExists (Dir)) Then
				WScript.StdOut.WriteLine Dir & " [DIR created]"
			Else
				WScript.StdOut.WriteLine Dir & " [DIR NOT created - ERROR]"
				MkDir = ERR_CREATEDIR
				Exit Function
			End If
		End If

	End If

End Function

'******************************************************************************
'
'	Function:
'		sync a single file
'
'	Parameter:
'		Dir				source directory
'		File				filename
'
'	Returns:
'		see		CopyFile ()
'
'*****************************************************************************/

Private Function SyncFile (Dir, File)

	Dim	rc
	Dim	Src
	Dim	Dst
	Dim	fi
	Dim	fid
	Dim	cflag
	Dim	size

	SyncFile = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	filenames
	'
	'**********************************************************************

	Dst = DstDir & Right (Dir, Len (Dir) - Len (SrcDir)) & "\" & File
	Src = Dir
	If (Right (Src, 1) <> "\") Then
		Src = Src & "\"
	End If
	Src = Src & File

	Set fi = fso.GetFile (Src)
	size = fi.Size

	'**********************************************************************
	'
	'	check if destination file is already present
	'
	'**********************************************************************

	If (Not fso.FileExists (Dst)) Then

		'**************************************************************
		'
		'	if selected, add the not yet present file
		'
		'*************************************************************/

		If (ScnAdd) Then

			CAddFile = CAddFile + 1
			CAddByte = CAddByte + size
			Select Case ScnTst

				Case SCANFLAG_TEST

					WScript.StdOut.WriteLine Dst & _
						" [created test (" & size & " Byte) ]"
					Exit Function

				Case SCANFLAG_BATCH

					WScript.StdOut.WriteLine "COPY " & Src & " " & Dst
					Exit Function

			End Select

			fso.CopyFile Src, Dst
			If (Not fso.FileExists (Dst)) Then
				If (ScnLog) Then
					WScript.StdOut.WriteLine Dst & _
						" [NOT created (" & size & " Byte) - ERROR]"
				End If
				SyncFile = ERR_COPY
				Exit Function
			End If

			Set fi = fso.GetFile (Dst)
			If (size <> fi.Size) Then
				If (ScnLog) Then
					WScript.StdOut.WriteLine Dst & _
						" [created WRONG size (" & _
						fi.Size & "/" & size & _
						" Byte) - ERROR]"
				End If
				SyncFile = ERR_COPY
				Exit Function
			End If

			If (ScnLog) Then
				WScript.StdOut.WriteLine Dst & _
					" [created (" & size & " Byte) ]"
			End If

			' check for remove read only bit switch
			If (Not ScnAtt) Then
				' remove read only bit if present
				If (fi.Attributes And &H01) Then
					fi.Attributes = fi.Attributes And &HFE
				End If
			End If

		End If

		Exit Function

	End If

	'**********************************************************************
	'
	'	check if destination file differs in size or age
	'	if ScnIgn, check size only
	'
	'**********************************************************************

	Set fid = fso.GetFile (Dst)

	cflag = False
	If (fi.size <> fid.Size) Then
		cflag = True
	End If

	'**********************************************************************
	'
	'	checking dates absolute may not work properly when source and
	'	destination file systems are different (eg. W2K vs. PATHWORKS)
	'
	'**********************************************************************

	If (Not ScnIgn) Then
		If (fi.DateLastModified <> fid.DateLastModified) Then
			cflag = True
		End If
	End If

	If (cflag = False) Then
		Exit Function
	End If

	CUpdFile = CUpdFile + 1
	CUpdByte = CUpdByte + size
	Select Case ScnTst

		Case	SCANFLAG_TEST:

			WScript.StdOut.WriteLine Dst & _
				" [updated test (" & size & " Byte) ]"
			Exit Function

		Case	SCANFLAG_BATCH:

			WScript.StdOut.WriteLine "COPY " & Src & " " & Dst
			Exit Function

	End Select

	fi.Copy Dst

	If (size <> fi.Size) Then
		If (ScnLog) Then
			WScript.StdOut.WriteLine Dst & _
				" [updated WRONG size (" & _
				fi.Size & "/" & size & _
				" Byte) - ERROR]"
		End If
		SyncFile = ERR_COPY
		Exit Function
	End If

	If (ScnLog) Then
		WScript.StdOut.WriteLine Dst & _
			" [updated (" & size & " Byte) ]"
	End If

End Function

'******************************************************************************
'
'	Function:
'		work through a directory and check every file
'
'	Parameter:
'		Dir				directory name
'
'	Returns:
'		see		CopyFile ()
'
'******************************************************************************

Private Function SyncDir (Dir)

	Dim	rc
	Dim	fo
	Dim	fi
	Dim	i

	SyncDir = ERR_NONE

	On Error Resume Next
	
	'**********************************************************************
	'
	'	go through directory file by file
	'
	'**********************************************************************

	Set fo = fso.GetFolder (Dir)
	Set fi = fo.Files
	For Each i In fi
		If (Not ((Not ScnHid) And ((i.Attributes And &H02) > 0))) Then
			CTotFile = CTotFile + 1
			CTotByte = CTotByte + i.Size
			rc = SyncFile (Dir, i.Name)
			If (ScnDbg) Then
				WScript.StdErr.WriteLine _
					"SyncFile (" & Dir _
					& ", " & i.name & ") = " & rc
			End If
			SyncDir = rc
			If (rc <> ERR_NONE) Then
				Exit Function
			End If
		End If
	Next

End Function

'******************************************************************************
'
'	Function:
'		walk through directories and update if appropriate
'
'	Parameter:
'		Dir				name of current root
'
'	Returns:
'		ERR_NONE			all OK
'
'******************************************************************************

Private Function SyncTree (Dir)

	Dim	rc
	Dim	Dst
	Dim	fo
	Dim	fi
	Dim	i

	SyncTree = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	create pointer to destination directory
	'
	'**********************************************************************

	Dst = DstDir & Right (Dir, Len (Dir) - Len (SrcDir))

	'**********************************************************************
	'
	'	if needed, create destination directory
	'
	'**********************************************************************

	rc = MkDir (Dst)
	If (ScnDbg) Then
		WScript.StdErr.WriteLine _
			"MkDir (" & Dst & ") = " & rc
	End If
	If (rc <> ERR_NONE) Then
		SyncTree = rc
		Exit Function
	End If

	'**********************************************************************
	'
	'	walk through current directory
	'
	'**********************************************************************

	rc = SyncDir (Dir)
	If (ScnDbg) Then
		WScript.StdErr.WriteLine _
			"SyncDir (" & Dir & ") = " & rc
	End If
	If (rc <> ERR_NONE) Then
		SyncTree = rc
		Exit Function
	End If

	'**********************************************************************
	'
	'	check subdirectories
	'
	'*********************************************************************/

	Set fo = fso.GetFolder (Dir)
	Set fi = fo.Subfolders
	For Each i In fi

		If (Left (i.Name, 1) <> "." ) Then

			'******************************************************
			'
			'	process subdirectory
			'
			'******************************************************

			CTotDir = CTotDir + 1
			rc = SyncTree (Dir & "\" & i.Name)
			If (rc <> ERR_NONE) Then
				SyncTreee = rc
				Exit Function
			End If

		End If

	Next

End Function

'******************************************************************************
'
'	Main
'
'******************************************************************************

Private Function Main

	Dim	rc
	Dim	argv

	Main = ERR_NONE

	On Error Resume Next

	'**********************************************************************
	'
	'	parse parameters
	'
	'**********************************************************************

	Set argv = WScript.Arguments

	rc = ScanFlag (argv)
	If (rc <> SCANFLAG_OK) Then

		Select Case rc

			Case SCANFLAG_UNKNOWN

				Hello "ScanFlag: invalid switch"

			Case SCANFLAG_BADARGS

				Hello "ScanFlag: invalid syntax"

			Case SCANFLAG_COMBINED

				Hello "ScanFlag: invalid switch combination"

			Case Else

				Hello "ScanFlag: unknown internal error " & rc

		End Select

		Main = rc
		Exit Function

	End If

	'**********************************************************************
	'
	'	check if source directory is present
	'
	'**********************************************************************

	If (Not fso.FolderExists (SrcDir)) Then
		WScript.StdOut.WriteLine _
			"synctree32: unable to open SrcDir " & SrcDir
		Main = ERR_SRC
		Exit Function
	End If

	'**********************************************************************
	'
	'	check if destination directory is present
	'
	'**********************************************************************

	If (Not fso.FolderExists (DstDir)) Then
		WScript.StdOut.WriteLine _
			"synctree32: unable to open DstDir " & DstDir
		Main = ERR_DST
		Exit Function
	End If

	'**********************************************************************
	'
	'	initialize statistics
	'
	'**********************************************************************

	InitStatistics

	'**********************************************************************
	'
	'	check if recoursion is requested
	'
	'**********************************************************************

	If (ScnSub) Then

		'**************************************************************
		'
		'	process directory tree
		'
		'**************************************************************

		If (ScnDel) Then
			rc = DelTree (DstDir)
			If (ScnDbg) Then
				WScript.StdErr.WriteLine _
					"DelTree (" & DstDir & ") = " & rc
			End If
			If (rc <> ERR_NONE) Then
				Main = rc
				Exit Function
			End If
		End If

		rc = SyncTree (SrcDir)
		If (ScnDbg) Then
			WScript.StdErr.WriteLine _
				"SyncTree (" & SrcDir & ") = " & rc
		End If
		Main = rc
		Exit Function

	End If

	'**********************************************************************
	'
	'	process single directory
	'
	'**********************************************************************

	If (ScnDel) Then
		rc = DelDir (DstDir)
		If (ScnDbg) Then
			WScript.StdErr.WriteLine _
				"DelDir (" & DstDir & ") = " & rc
		End If
		If (rc <> ERR_NONE) Then
			Main = rc
			Exit Function
		End If
	End If

	rc = SyncDir (SrcDir)
	If (ScnDbg) Then
		WScript.StdErr.WriteLine _
			"SyncDir (" & SrcDir & ") = " & rc
	End If
	Main = rc

End Function

'******************************************************************************

Dim	rc

If (IsScript () <> True) Then
	MsgBox "Ich will Kühe! Nein, doch lieber CSCRIPT"
	WScript.Quit 42
End If

Set wso = CreateObject ("WScript.Shell")
Set fso = CreateObject ("Scripting.FileSystemObject")

'******************************************************************************
'
'	initialize statistics
'
'******************************************************************************

InitStatistics

'******************************************************************************
'
'	do your job
'
'******************************************************************************

rc = Main

'******************************************************************************
'
'	display statistics
'
'******************************************************************************

If (rc <> 2) Then
	DisplayStatistics
End If
WScript.StdOut.WriteLine "# return code is " & rc

WScript.quit rc
