WshShell = new ActiveXObject("WScript.Shell");
FileSystem = new ActiveXObject("Scripting.FileSystemObject");

CUI = WScript.FullName.match(/cscript\.exe$/i) != null;

for (var args = new Enumerator(WScript.Arguments); ! args.atEnd(); args.moveNext())
	start(args.item());
WScript.quit(0);

function start(mine)
{
	if (mine.match(/\.#/)) return;	//除外 "hoge.#hage"

	if (FileSystem.FolderExists(mine))
	{
		for (var files = new Enumerator(FileSystem.GetFolder(mine).Files); ! files.atEnd(); files.moveNext())
		{
			var file = files.item();
			var a = file.Name.charAt(0);
			if (a == ".") continue;	//除外 ".hoge"
			if (a == "#") continue;	//除外 "#hoge"
			if (file.Name.match(/^コピー 〜 /)) continue;
			var extensionName = FileSystem.GetExtensionName(file.Name).toLowerCase();
			if (extensionName == "bak") continue;	//除外 "hoge.bak"
			if (extensionName == "lnk") continue;	//除外 "hoge.bak"
			if (extensionName == "url") continue;	//除外 "hoge.bak"
			if (extensionName == "zip") continue;	//除外 "hoge.zip"
			if (extensionName == "7z" ) continue;	//除外 "hoge.7z"
			if (extensionName == "jpg") continue;	//除外 "hoge.jpg"
			if (extensionName == "gif") continue;	//除外 "hoge.gif"
			if (extensionName == "bmp") continue;	//除外 "hoge.bmp"

			start(file.Path);
		}
	}
	else if (FileSystem.FileExists(mine))
	{
		var parentFolderName = FileSystem.GetParentFolderName(mine);
		var fileName = FileSystem.GetFileName(mine);
	/*	var baseName = FileSystem.GetBaseName(mine);	*/
		var extensionName = FileSystem.GetExtensionName(mine).toLowerCase();
		var svnBase = FileSystem.BuildPath(parentFolderName, ".svn\\text-base\\" + fileName + ".svn-base");

		var lastmod = null;
		with (new Date(FileSystem.GetFile(mine).DateLastModified)) {
			lastmod = Z2(getFullYear()) + Z2(getMonth()+1) + Z2(getDate())
			  + "-" + Z2(getHours()) + Z2(getMinutes())/* + Z2(getSeconds())*/;
		}

		// Hoge-1234567890.java.bak  (WinMerge)
		// Hoge.java.BAK  (Hidemaru, perl)
		if (fileName.match(/(.+)-\d{10}\.(.+?)\.bak$/) != null
		 || fileName.match(/(.+)\.(.+?)\.BAK$/) != null) {
			var baseName = RegExp.$1;
			var extensionName = RegExp.$2;
			var bak = FileSystem.BuildPath(parentFolderName, baseName + "." + extensionName + ".#" + lastmod);
			FileSystem.MoveFile(mine, bak);
			if (CUI) WScript.stdout.writeline(bak);
			return;
		}
//		// Hoge-1234567890.java.bak  (WinMerge)
//		if (fileName.match(/(.+)-\d{10}\.(.+?)\.bak$/) != null) {
//			var bak = FileSystem.BuildPath(parentFolderName, RegExp.$1 + "." + RegExp.$2) + ".#" + lastmod;
//			FileSystem.MoveFile(mine, bak);
//			if (CUI) WScript.stdout.writeline(bak);
//			return;
//		}

		if (extensionName == "bak") return;		//除外 "hoge.bak"
		if (extensionName == "svn-base") return;	//除外 "hoge.java.svn-base"

		if (FileSystem.FileExists(svnBase)) {
			var revision = null;
			var result = new Array();
			var exec = WshShell.Exec("svn info \"" + mine + "\"");
			while (!exec.StdOut.AtEndOfStream) {
				result.push(exec.StdOut.ReadLine());
			}
			var pattern = /(?:Last Changed Rev:|最終変更リビジョン:) *([0-9]+)/;
			for (j = 0; j < result.length; j++) {
				if (result[j].match(pattern))
				{
					revision = RegExp.$1;
					break;
				}
			}
			if (revision == null) {
				WScript.echo("Error:\r\n" + fileName + "\r\nCan not get svn revision.");
				WScript.quit(1);
			}
			result = null;

			CopyFile(svnBase, mine + ".#r" + revision);
			CopyFile(mine   , mine + ".#r" + revision + "M-" + lastmod);
			if (CUI) WScript.stdout.writeline(mine + ".#r" + revision + "M-" + lastmod);
		}
		else
		{
			CopyFile(mine, mine + ".#" + lastmod);
			if (CUI) WScript.stdout.writeline(mine + ".#" + lastmod);
		}
	}
	else
	{
//		WScript.echo("Warning:'" + mine + "'\r\n無視しました。");
		WScript.echo("Warning:'" + fileName + "' はコピーできないファイルか、ファイル名にユニコード文字を使用している可能性があります。\r\n処理を中断します。");
		WScript.quit(1);
	}
}

function Z2(v)
{
	var s = "0" + v;
	return s.substr(s.length - 2);
}

//function lastModified(filePath)
//{
//	with (new Date(FileSystem.GetFile(filePath).DateLastModified)) {
//		return Z2(getFullYear()) + Z2(getMonth()+1) + Z2(getDate())
//		  + "-" + Z2(getHours()) + Z2(getMinutes())/* + Z2(getSeconds())*/;
//	}
//}

function CopyFile(s, d)
{
	if (! FileSystem.FileExists(d))
		FileSystem.CopyFile(s, d);
}
