// Will "unregister" plugins from mantis that are of this style entry.
//analog F7C5C697-E083-4680-9A59-0C4D0144BCAE D18C290D-D5DF-4409-BA21-07D1FB2B26EC "Now Playing"
//factory D18C290D-D5DF-4409-BA21-07D1FB2B26EC "C:\utils\sq2\now_playing.dll" "Now Playing"

var szDll  = WScript.Arguments.Item(0); // the dll full path or name should match.

var szRemove = ""; // guid to remove.
var oFso = WScript.CreateObject("Scripting.FileSystemObject");

if (!oFso.FileExists("mantis\\mialib.conf")) { // this file don't exist
	WScript.Echo("mantis\\mialib.conf does not appear to exist.  Run this script from your sonique2 install directory.");
	WScript.Quit();
}

// backup the mialib.conf file first.
oFso.CopyFile("mantis\\mialib.conf", "mantis\\mialib.conf.bak");

var oFile = oFso.OpenTextFile("mantis\\mialib.conf",1,false);
var szLine = new String();
var szOutput = "";

if (oFile != null) {
	var szAll = new String(oFile.ReadAll());
	var aLines = szAll.split("\r\n");
	oFile.Close();


	for(var x = 0; x < aLines.length; x++) {
		 var aFields = aLines[x].split(" ");		
		  
		 if (aFields[0] == "factory") {
			var szTest = new String(aFields[2]);

			if (szTest.indexOf(szDll) > -1) { // found a factory line with this dll.
				szRemove = new String(aFields[1]);
			}
		 }
	}

	// now loop the lines again and remove the selected lines with the plugin's unique guid.
	for(x = 0; x < aLines.length; x++) {
		 var aFields = aLines[x].split(" ");		
		 var szTest = "";
		  
		 if ( aFields[0] == "factory") {
			szTest = aFields[1];
		 } else if (aFields[0] == "analog") {
			szTest = aFields[2];
		 }
		
		 
		 if (szTest != szRemove) {
			szOutput += aLines[x] + "\r\n";
		 } else {
			WScript.Echo("Removing line " + aLines[x]);
		 }
	}


	if (szOutput != "") {
		WScript.Echo("Writing\r\n " + szOutput);
		oFile = oFso.OpenTextFile("mantis\\mialib.conf", 2);
		oFile.Write(szOutput);
		oFile.Close();
	}
}
