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

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

ENV = WshShell.Environment("Process");
ENV("lang") = "en";

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 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-20101013120842.BAT.bak  (WinMerge)
		// Hoge.java.BAK  (Hidemaru, perl)
		if (fileName.match(/(.+)-\d{10}\.(.+?)\.bak$/) != null
		 || fileName.match(/(.+)-20\d{12}\.(.+?)\.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;
		}

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

		var svnInfo = {};
		var exec = WshShell.Exec("svn info \"" + mine + "\"");
		while (!exec.StdOut.AtEndOfStream) {
			a = exec.StdOut.ReadLine().split(/: /, 2);
			if (a.length == 2)
				svnInfo[a[0]] = a[1];
		}
		var revision = svnInfo["Last Changed Rev"];
	//	var lastChangedDate = svnInfo["Last Changed Date"];

		if (revision != null) {

			var svnBase = FileSystem.BuildPath(FileSystem.GetSpecialFolder(2), "_.svn-base");
			if (FileSystem.FileExists(svnBase)) FileSystem.DeleteFile(svnBase);
			var cmd = "svn export -r BASE \"" + mine + "\" \"" + svnBase + "\"";
			try {
				if (WshShell.Run(cmd, 0, true)) throw "";
			} catch (e) {
				WScript.echo(">>>ERROR<<<\r\n" + cmd);
				WScript.quit(1);
			}

			//テストちゅう - 手抜き：サイズと日付しか比較していない
			var f1 = FileSystem.GetFile(mine);
			var f2 = FileSystem.GetFile(svnBase);
			if (f1.Size == f2.Size
			  && new Date(f1.DateLastModified).getTime() == new Date(f2.DateLastModified).getTime())
			{
				return;
			}
			f1 = null;
			f2 = 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 CopyFile(s, d)
{
	if (! FileSystem.FileExists(d))
		FileSystem.CopyFile(s, d);
}
