var diffmode=true;    // true for manual check, false for production

var jsonfn=process.argv[2]||"ketaka84.json";  //default json filename
if (jsonfn.indexOf(".json")==-1)jsonfn+=".json";
var json=require("./"+jsonfn);

var source_xml_folder="before_xml/";
var merged_xml_folder="merged_xml/";

//////////////////////////////////////////////////////////////////////

var fs=require("fs");
var mkdirp=require("./mkdirp");
var path=require("path");
var lastfile="",pages={},currentfile="",currentpage="";
var unmerged=[],lasterror="",mergecount=0;

var errormerge=function(item){
	item.err=lasterror;
	lasterror="";
	unmerged.push(JSON.stringify(item));
}

var loadPage=function(filecontent) {
	var page={},lastidx=0,lastid="_";
	filecontent.replace(/<pb id="(.+?)"\/>\n?/g,function(m,m1,idx){
		if (lastidx===0) {
			page[lastid]=filecontent.substr(0,idx);
		}
		page[lastid]=filecontent.substring(lastidx,idx);
		lastidx=idx+m.length;
		lastid=m1;
	});
	page[lastid]=filecontent.substring(lastidx).trim();	
	return page;
}
var fetchText=function(pagecontent,offset,len) {
	var realoff=getOffsetOmitTag(pagecontent,offset);
	if (typeof realoff !=="number") return null;
	return pagecontent.substr(realoff,len);
}

var getOffsetOmitTag=function(pagecontent,offset){
	var intag=false;
	var remain=offset;
	for (var i=0;i<pagecontent.length;i++) {
		var c=pagecontent[i];
		if (c==='<') intag=true;
		if (remain==0) {
			if (intag) {
				lasterror="offset inside tag";
				return null;
			} else {
				return i;
			}
		}
		if (!intag) remain--;
		if (c==='>') intag=false;
	}
	lasterror="invalid offset";
	return null;
}

var merge=function(item) {
	var page=pages[currentpage];
	var realoff=getOffsetOmitTag(page,item.offset);
	var newtext=diffmode?("["+item.from+"|"+item.to+"]"):item.to;
	pages[currentpage]=page.substr(0,realoff)+newtext+page.substr(realoff+item.from.length);
}

var createnewfilecontent=function() {
	var s="";
	for (var i in pages) {
		if (i!=="_") s+='<pb id="'+i+'"/>\n';
		s+=pages[i];
	}
	return s;
}

var savemergefile=function() {
	if (!lastfile)return;
	var outfn=merged_xml_folder+lastfile;
	var dir=path.dirname(outfn);
	mkdirp.sync(dir);	//create folder if not exist
	fs.writeFileSync(outfn,createnewfilecontent(),"utf8");
}

var trymerge=function(item){	
	if (item.file!==lastfile) {
		savemergefile();
		currentfile=item.file;
		pages=loadPage(fs.readFileSync(source_xml_folder+item.file,"utf8"));
	}
	currentpage=item.page;
	var t=fetchText(pages[item.page],item.offset,item.from.length);

	if (t!==item.from) {
		if (!lasterror) lasterror="xml mismatch:"+t;
		errormerge(item);
	} else {
		if (t===item.to){
		 	if (!lasterror) lasterror="sametext";
			errormerge(item);
		} else {
			mergecount++;
			merge(item);
		}
	}
	lastfile=item.file;
}

json.map(trymerge);
savemergefile(); //last file content
var unmergefn=jsonfn.replace(".json","")+"_unmerged.json";
console.log("success merged",mergecount,"unmerged",unmerged.length,"check",unmergefn);
fs.writeFileSync(unmergefn,unmerged.join("\n"),"utf8")
@@ -61,16 +61,36 @@
 oduction
+%0Avar inserterr=true;
 %0A%0Avar js
@@ -628,16 +628,76 @@
 ror=%22%22;%0A
+%09if (inserterr) inserterror(item.offset,unmerged.length+1);%0A
 %09unmerge
@@ -1228,16 +1228,29 @@
 %22number%22
+ %7C%7C realoff%3C0
 ) return
@@ -1296,24 +1296,25 @@
 ff,len);%0A%7D%0A%0A
+%0A
 var getOffse
@@ -1564,20 +1564,18 @@
 %09return 
-null
+-i
 ;%0A%09%09%09%7D e
@@ -1961,25 +1961,222 @@
 .length);%0A%7D%0A
+var inserterror=function(offset,err) %7B%0A%09var page=pages%5Bcurrentpage%5D;%0A%09var realoff=Math.abs(getOffsetOmitTag(page,offset));%0A%09pages%5Bcurrentpage%5D=page.substr(0,realoff)+%22!%22+err+page.substr(realoff);%0A%7D
 %0A
-
 var createne
@@ -2805,20 +2805,104 @@
 ngth);%0A%0A
-
 %09if 
+(!t) %7B%0A%09%09if (!lasterror) lasterror=%22invalid offset%22;%0A%09%09errormerge(item);%0A%09%7D else if 
 (t!==ite

insert error# in xml