// A tool making plugin shortcut for macros and tools:
// A plugin shortcut is available after ImageJ restarting, in the Plugins<My Favorite Macros menu.
// This tool installs its own plugin shortcut, if placed into the ImageJ/macros/tools folder.

// Author : Gilles Carpentier
// Faculte des Sciences et Technologies,
// Universite Paris 12 Val de Marne, France.

var KindOfMacro= 0,sortoption=0;
var favoritefoldername= "My Favorite Macros";
var macroname= "",optionchoice="Only macro sets and tools";

macro "Install Macro Plugins Shortcuts Tool -  "{
}

macro "Install Macro Plugins Shortcuts Tool Selected"{
	requires("1.37g");
	choicemenu=newArray("Only macro sets and tools", "All macros");
	Dialog.create("Macro & Tool Sort Choice");
	Dialog.addMessage("Display Macro sets and Tools only, or all kind of macros?\n(\"all macros\" option gives a very long list, less manageable).");
	Dialog.addChoice("->",choicemenu,toString(optionchoice));
	Dialog.show();
	optionchoice= Dialog.getChoice();
	if (optionchoice == "Only macro sets and tools") sortoption=0;
	if (optionchoice == "All macros") sortoption=1;

	macroname = GetMacroListMenu ();
	InstallPluginsStarter(macroname,1);
	if (File.exists(getDirectory("macros") + "tools/MacroPluginShortcutsTool.txt")) {
		macroname="MacroPluginShortcutsTool.txt";KindOfMacro= 1;
		InstallPluginsStarter(macroname,0);
	}
}

function InstallPluginsStarter(macroname,comment) {
	codemacrostarter ="run\(\"Install...\", \"install=[\"+getDirectory(\"macros\")+\"" + macroname + "\]\"\);";
	codetoolstarter = "run\(\"Install...\", \"install=[\"+getDirectory(\"macros\")+\"tools/" + macroname + "\]\"\);";
	StarterDir = getDirectory("startup") + favoritefoldername + "/";
	if (getDirectory("plugins") == "") exit ("Unable to find the Plugins directory; something wrong in the ImageJ folder.");
	pluginlocation = getDirectory("plugins");
	pluginname = ShortMacroName (macroname);
	// Create a <favoritefoldername> repertory in the ImageJ Plugins folder.
	StarterDir = pluginlocation+favoritefoldername+File.separator;
	File.makeDirectory(StarterDir);
	if (!File.exists(StarterDir)) exit ("Unable to create Image Bio Methods Macros directory, something wrong in the ImageJ folder.");
	starterplugin = StarterDir + pluginname +"_ .ijm";
	//Write the new macro
	codestarter=codemacrostarter;
	if (KindOfMacro == 1) codestarter=codetoolstarter;
	f= File.open(starterplugin);
	print (f,codestarter);
	File.close(f);
 	if (comment==1) {
		showMessage ("Installation of the Plugin \"" +pluginname+ "\" is completed.\nIt will be available after ImageJ restarting, in the \"Plugins->" + favoritefoldername + "\" menu.");
	}
}

function GetMacroListMenu () {
	if (getDirectory("macros") == "") exit ("Unable to find the macros directory; something wrong in the ImageJ folder.");
	macrostoolslocation = getDirectory("macros") + "tools/";
	if (File.isDirectory(macrostoolslocation) == 0) exit ("Unable to find the tools directory on the macros directory; something wrong in the ImageJ folder.");
	macrosfolder = getDirectory("macros");
	listofmacro = getFileList(macrosfolder); listoftools = getFileList(macrostoolslocation);
	NbMacro= getMacroNumber (listofmacro,sortoption,macrosfolder); NbTools= getMacroNumber (listoftools,sortoption,macrostoolslocation); NbToolsandMacros=(NbMacro + NbTools);
	MacrosandTools1 = newArray(NbToolsandMacros+2); MacrosandTools1[0] =-1;
	MacrosandTools2 = newArray(NbToolsandMacros+2); MacrosandTools2[0] ="--- Macro List ---";PlaceOfMacro=1;
	PlaceintheMenu(MacrosandTools1,MacrosandTools2,PlaceOfMacro,listofmacro,0,sortoption,macrosfolder);
	MacrosandTools2[NbMacro+1]="--- Tools List ---";
	PlaceintheMenu(MacrosandTools1,MacrosandTools2,(NbMacro+2),listoftools,1,sortoption,macrostoolslocation);
	macrochosen = "--- Macro List ---";
       showProgress(1.0);
	while (macrochosen == "--- Tools List ---" || macrochosen == "--- Macro List ---"){
		Dialog.create("Macro & Tool Choice");
		Dialog.addMessage("Choose a Macro or a Tool in the popup menu\n(list of macros, following by the list of tools):");
		Dialog.addChoice("->",MacrosandTools2);
		Dialog.addMessage("\nNumber of macros="+ NbToolsandMacros + "  ("+NbMacro+" \"Macros\", "+NbTools+" \"Tools\")");
		Dialog.show();
		macrochosen=  Dialog.getChoice();
		for (i=1; i<=NbToolsandMacros; i++) {
			if (MacrosandTools2[i] == macrochosen) KindOfMacro = MacrosandTools1[i];
		}
	}
	return macrochosen;
}

function ShortMacroName (longname) {
	shortnames = longname;
	if (endsWith(longname, ".txt") || endsWith(longname, ".ijm")) shortnames = substring(longname, 0, (lengthOf(longname)-4));
	return shortnames;
}

function getMacroNumber (lmacros,sortoption,dirmac) {
	NumberOfMacro=0;
     	for (i=0; i<lmacros.length; i++) {
         	showProgress(i,lmacros.length);
		if (endsWith(lmacros[i], ".txt") || endsWith(lmacros[i], ".ijm")) {
			if (sortoption == 1) NumberOfMacro ++;
			if (sortoption == 0) {
				testmac=sort(dirmac,lmacros[i]);
				if (testmac==1) NumberOfMacro ++;
			}
		}		
	}
	return NumberOfMacro;
}

function PlaceintheMenu (MacrosandTools1,MacrosandTools2,place,listdir,kind,sortoption,dirmac) {
     	for (i=0; i< listdir.length; i++) {
         	showProgress(i, listdir.length);
		if (endsWith(listdir[i], ".txt") || endsWith(listdir[i], ".ijm")) {
			if (sortoption == 1) {
				MacrosandTools1[place]=kind; MacrosandTools2[place] =listdir[i];
				place ++;
			}
			if (sortoption == 0) {
				testmac=sort(dirmac,listdir[i]);
				if (testmac==1) {
					MacrosandTools1[place]=kind; MacrosandTools2[place] =listdir[i];
					place ++;
				}
			}		
		}
	}
	return MacrosandTools1;
	return MacrosandTools2;
}

function sort(dirmac,mac){
	testmac=0;
	macropath=dirmac+mac;
	macrocontent = File.openAsString(macropath);
	nummacro=nmac(macrocontent, "macro");
	if (nummacro >1)  testmac=1;
	if (indexOf(macrocontent, "Tool")>0) testmac=1;
	return testmac;
}

function nmac(macrotext,kindof){
	nummac=0;
	while (indexOf(macrotext, kindof)> -1) {
		if (indexOf(macrotext, kindof)>-1){
			macrotext=substring(macrotext, (indexOf(macrotext, kindof)+lengthOf(kindof)), lengthOf(macrotext));
			index=0;
			while (indexOf(macrotext, " ") ==0){macrotext=substring(macrotext,1, (lengthOf(macrotext)));}
			if (startsWith(macrotext, "\"") || startsWith(macrotext, "'" )) nummac ++;
		}
	}
	return nummac;
}



