// Title of tool: Luts Macros and Tools Updater.txt 
// First version: 17/11/2006

// Description:

// The "Luts Macros and Tools Updater" tool set perfoms a live-updating/restoring of the following ImageJ macros repertories:
// - /macros
// - /macros/tools
// - /macros/toolsets
// - /luts
// For a routine use of installed macros and tools, a plugin shortcut can be installed using the MacroPluginShortcutsTool.txt

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

var distantmacrosrep = "http://rsb.info.nih.gov/ij/macros/";
var distanttoolsrep = "http://rsb.info.nih.gov/ij/macros/tools/";
var distanttoolsetsrep = "http://rsb.info.nih.gov/ij/macros/toolsets/";

var urllist="http://image.bio.methods.free.fr/ij/ijupdatetest/ListOfMacros.txt";distantmacrolink = "";
var KindOfMacro=0,report=0,updates=0,nMacroinL=0,nToolinL=0,nToolsetsinL=0,TheReport="";
var onlinedoclink = "http://image.bio.methods.free.fr/ijmacrolutupdater.html";

var distantlutsrep = "http://image.bio.methods.free.fr/ij/luts/"; distantmacrolink = ""; nLutsinL=0;
var urllistoflut="http://image.bio.methods.free.fr/ij/ijupdatetest/ListOfLUTs.txt";distantmacrolink = "";
var demoimagename = "blobs.gif",non=0;

var xx = requires138b(); // check version at install time
function requires138b() {requires("1.38b"); return 0; }


macro "Stop Lut Scan Tool - Ccc" {non=1;}

// macro updater menu tool
var uCmds = newMenu("Macro, Tools and Toolsets Updater Menu Tool",newArray("Macro, Tools and Toolsets Updater","Get the list of available macros, tools and toolsets"));
macro "Macro, Tools and Toolsets Updater Menu Tool -  "{
	cmd = getArgument();
	if (cmd!="-" && cmd == "Macro, Tools and Toolsets Updater") {MacroToolsToolsetsUpdater ();}
	if (cmd!="-" && cmd == "Get the list of available macros, tools and toolsets") {GetTheListofAvailableMacros ();}
}

// luts updater menu tool
var lCmds = newMenu("LUTs Updater Menu Tool",newArray("LUTs Updater","Get the list of available LUTs"));
macro "LUTs Updater Menu Tool -  " {
	cmd = getArgument();
	if (cmd!="-" && cmd == "LUTs Updater") {LUTsUpdater ();}
	if (cmd!="-" && cmd == "Get the list of available LUTs") {GetthelistofavailableLUTs ();}
}

// last luts instaled menu tool
var luts = lastInstalledLutVisu();
var vCmds = newMenu("Last Installed Luts Menu Tool",luts);
macro "Last Installed Luts Menu Tool - C037T0b11LT7b0aiTbb10L"{
	cmd = getArgument();
	if (cmd!="-" && cmd !="No last luts installed list file found" && cmd != "Record this tool into the \"tools\" folder, to use this menu tool." && cmd != "List of new installed LUTs:") {
		image=checkForImage(cmd);
		if (nImages > 0) {selectImage(image);}
		run(cmd);
	}
}

macro "On Line Documentation Action Tool -  "{
	if (File.openUrlAsString(urllist) == "") exit("You need an internet access to run this function.");
	showMessageWithCancel  ("A notice is avaible on line. Open it with your default web browser?");
	run("URL...", "url=["+onlinedoclink +"]");
} 

// ---- Macro like function -----

function MacroToolsToolsetsUpdater () {
	distantmacrolink = urllist;
	if (File.openUrlAsString(distantmacrolink) == "") exit("You need an internet access to run this function.");
	Dialog.create("Macro & Tool Updater");
	Dialog.addMessage("The updater will look for new available macros, tools and toolsets.");
	Dialog.addMessage("\nChoose Updater Options:");
	Dialog.addCheckbox("- Check for macros, tools and toolsets updates (longer query):", 0);
	Dialog.addMessage("This option check for modifications, corrections or improvements \nfor the Macros, Tool and Toolsets already installed onto your \nImageJ version.");
	Dialog.addMessage("\n ");
	Dialog.addCheckbox("- Make an installation report.", 1);
	Dialog.show();
	updates= Dialog.getCheckbox();
	report= Dialog.getCheckbox();
	// To get the contents of the distant list of macros.
	showStatus("Internet link...");
	macrotextnih =File.openUrlAsString(distantmacrolink);
	showStatus("");
	// To obtain the separated lists of macros and tools from the distant file list.
	lines=split(macrotextnih,"\n");
	getMandTNumber (lines);
	lMacro = newArray(nMacroinL); lTool = newArray(nToolinL);  lToolsets = newArray(nToolsetsinL);
	lMacro = lPlace (lines,lMacro,"Macro "); lTool = lPlace (lines,lTool,"Tool "); lToolsets = lPlace (lines,lToolsets,"Toolset ");
	// To get the contents of local ImageJ (Macros, Tools, Toolsets) folders, into separated lists of macros, tools and toolsets.
	macrosfolder = "";alert=0;
	if (getDirectory("macros") == "") {
		// built a macros repertory
		macrobuilt= getDirectory("startup") ;
		MacroDir = macrobuilt +"macros/" +File.separator;
		File.makeDirectory(MacroDir);
		if (!File.exists(MacroDir)) exit ("Unable to create the \" macros \" directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
		macrosfolder = macrobuilt +"macros/"; alert =1;
	}
	if (macrosfolder == "") macrosfolder = getDirectory("macros");
	macrostoolslocation = macrosfolder + "tools/";
	if (File.isDirectory(macrostoolslocation) == 0) {
		toolsDir = macrostoolslocation+File.separator;
		File.makeDirectory(toolsDir);
		if (!File.exists(toolsDir)) exit("Unable to create the \" tools \" directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
	}
	macrostoolsetlocation = macrosfolder + "toolsets/";
	if (File.isDirectory(macrostoolsetlocation) == 0) {
		toolsetsDir = macrostoolsetlocation+File.separator;
		File.makeDirectory(toolsetsDir);
		if (!File.exists(toolsetsDir)) exit("Unable to create the \" toolsets \" directory, something wrong in the ImageJ folder\nTry again after restarting Image.");
	}
	listofmacro = getFileList(macrosfolder); listoftools = getFileList(macrostoolslocation);listoftoolsets = getFileList(macrostoolsetlocation);

	NbMacro= getMacroNumber (listofmacro); NbTools= getMacroNumber (listoftools); NbToolsets= getMacroNumber (listoftoolsets); NbToolsandMacros=(NbMacro + NbTools +NbToolsets);
	FMacros = newArray(NbMacro); FTools =newArray(NbTools); FToolsets =newArray(NbToolsets);
	FMacros = lPlace (listofmacro,FMacros,""); FTools = lPlace (listoftools,FTools,""); FToolsets =lPlace (listoftoolsets,FToolsets,"");
 
	// To get the new macros, tools and toolsets present in the list, not present into the local ImageJ .
	nbNewMacro= (nMacroinL - nbThings(lMacro,FMacros)); nbNewTools= (nToolinL - nbThings(lTool,FTools));nbNewToolsets= (nToolsetsinL - nbThings(lToolsets,FToolsets));
	NewMacros = newArray(nbNewMacro); NewTools =newArray(nbNewTools);NewToolsets =newArray(nbNewToolsets);
	NewMacros = PlaceNewThings (lMacro,FMacros,NewMacros); NewTools = PlaceNewThings (lTool,FTools,NewTools);NewToolsets = PlaceNewThings (lToolsets,FToolsets,NewToolsets);

	// To get the new macros and tool updates available for the local ImageJ macros and tools.
	if(updates==1) {
		SortedNewMacros = newArray(NbMacro);
		UpdatedMacro = newArray(Nbmacroupdates (FMacros,lMacro,distantmacrosrep,macrosfolder,SortedNewMacros));
		UpdatedMacro = placeUpdatemacro(UpdatedMacro,FMacros,SortedNewMacros);
		SortedNewTools = newArray(NbTools);
		UpdatedTool = newArray(Nbmacroupdates (FTools,lTool,distanttoolsrep,macrostoolslocation,SortedNewTools));
		UpdatedTool = placeUpdatemacro(UpdatedTool,FTools,SortedNewTools);
		SortedNewToolsets = newArray(NbToolsets);
		UpdatedToolsets = newArray(Nbmacroupdates (FToolsets,lToolsets,distanttoolsetsrep,macrostoolsetlocation,SortedNewToolsets));
		UpdatedToolsets = placeUpdatemacro(UpdatedToolsets,FToolsets,SortedNewToolsets);
 
	} else {UpdatedMacro=0;UpdatedTool=0;UpdatedToolsets=0;}
	// To make a dialog box to the user.
	Messagenew=""; messageupdat="";
	message1="- No Macro, Tool or Toolset update available.";
	message2="- There is no new Macro, Tool or Toolset available.";
	message3="- Macros, Tools and Toolsets Updates were not queried.";
	if ((NewMacros.length + NewTools.length +NewToolsets.length) > 0) {
				Messagenew = "- There are "+ NewMacros.length + " new Macro(s), " + NewTools.length + " new Tool(s) and " + NewToolsets.length + " new Toolset(s), available on line.";
	} else {Messagenew = message2;}

	if(updates==1) {
		if ((UpdatedMacro.length + UpdatedTool.length + UpdatedToolsets.length) > 0) {
			messageupdat="- There are "+ UpdatedMacro.length + " Macro(s), "+ UpdatedTool.length +" Tool(s) and "+ UpdatedToolsets.length +" Toolset(s), updates available on line.";
		} else {messageupdat= message1;}
	} else {messageupdat = message3;} 
	continue = "";
	if ((messageupdat != message3) ||  (Messagenew != message2)) continue = " \n-> Perform the installation?";
	if ((Messagenew== message2) && (messageupdat== message1)) exit("No new macro, tool or toolset update available: your ImageJ Macro folder is up to date.");
	showProgress(1.0);
	showMessageWithCancel("Installation User Confirmation",Messagenew +"\n"+ messageupdat + "\n"+ continue);
	if (continue == "") exit;
	// To perform the installation of the new macros and tools.
	if ((NewMacros.length + NewTools.length + NewToolsets.length) > 0) {
		instalmacro(NewMacros,distantmacrosrep,macrosfolder);
		instalmacro(NewTools,distanttoolsrep,macrostoolslocation);
		instalmacro(NewToolsets,distanttoolsetsrep,macrostoolsetlocation);
	}
	// To update the macros and tools.
	if (updates==1) {
		if ((UpdatedMacro.length + UpdatedTool.length + UpdatedToolsets.length) > 0) {
			archmacros(UpdatedMacro,macrosfolder,"Archived Macros");
			archmacros(UpdatedTool,macrostoolslocation,"Archived Tools");
			archmacros(UpdatedToolsets,macrostoolsetlocation,"Archived Toolsets");
			instalmacro(UpdatedMacro,distantmacrosrep,macrosfolder);
			instalmacro(UpdatedTool,distanttoolsrep,macrostoolslocation);
			instalmacro(UpdatedToolsets,distanttoolsetsrep,macrostoolsetlocation);
		}
	}
	// To make the installation report.
	if (report==1) rapport (FMacros,FTools,FToolsets,lMacro,lTool,lToolsets,NewMacros,NewTools,NewToolsets,UpdatedMacro,UpdatedTool,UpdatedToolsets,macrosfolder,"Update reports");
	if (alert==1) showMessage ("Restart ImageJ to finish the installation of the macros resources.");
}
	// Contents of arrays:
	// FMacros, FTools ; lists of macros and tools into your ImageJ.
	// lMacro, lTool, lToolsets ; lists of macros, tools and toolsets into the online ImageJ macro list.
	// NewMacros, NewTools, NewToolsets ; lists of macros, tools and toolsets into the online ImageJ macro list.
	// UpdatedMacro, UpdatedTool, UpdatedToolsets ; lists of macros , tools and toolsets updates available on the ImageJ web site.


function GetTheListofAvailableMacros () {
	showMessageWithCancel ("Get on line available macros list?");
	if (File.openUrlAsString(urllist) == "") exit("You need an internet access to run this function.");
	run("URL...", "url=["+urllist+"]");
}

// lut part

function LUTsUpdater () {
	distantmacrolink = urllistoflut;
	message="The updater allows to visualize the LUTs on a\"ramp\",\nor their effects onto an image.";
	Dialog.create("Lut Updater User Options");
	Dialog.addMessage(message);
	Dialog.addChoice("->",newArray("Ramp","Image","No Visualize"));
	Dialog.addCheckbox("Update report?", 1);
	Dialog.show();
	visuchoice=Dialog.getChoice();
	lutreport= Dialog.getCheckbox();

	if (File.openUrlAsString(distantmacrolink) == "") exit ("You need an internet access to run this function.");
	showStatus("Internet link...");
	luttextnih =File.openUrlAsString(distantmacrolink);
	showStatus("");
	lines=split(luttextnih,"\n");nLuts=GetnLUTs (lines);
	lLuts = newArray(nLuts);lLuts=llutPlace (lines,lLuts);
	if (getDirectory("startup") == 0) exit ("Unable to find the startup directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
	ijpath = getDirectory("startup");lutpath=ijpath+"luts/";
	if (File.isDirectory(lutpath) == 0) {
		LutDir = lutpath +File.separator;
		File.makeDirectory(LutDir);
		if (!File.exists(LutDir)) exit ("Unable to create the \" luts \" directory, something wrong in the ImageJ folder.\nTry again after restarting Image.");
	}
	listoflut = getFileList(lutpath);nlocallut=GetnLUTs (listoflut);
	llocalLuts = newArray(nlocallut);llocalLuts=llutPlace (listoflut,llocalLuts);
	nbNewLut= (nLuts - nbThings(lLuts,llocalLuts));lNewlut=newArray(nbNewLut);
	lNewlut=PlaceNewThings (lLuts,llocalLuts,lNewlut);
	if (lNewlut.length >0) {
		message="Number of new available LUT(s): " + lNewlut.length + ". Perform the installation? ";
		if (visuchoice != "No Visualize") message = message+ "\n(The scan of new installed LUTs can be interrupted by clicking \nonto the visualisation image window).";
		showMessageWithCancel (message);
	}
	instalmacro(lNewlut,distantlutsrep,lutpath);

	if (lNewlut.length >0) {
		if (lutreport == 1 ) { lutRapport (lutpath,lNewlut);}
		if (visuchoice != "No Visualize") {
			if (visuchoice == "Image") OpenImageLink(0);
			if (visuchoice == "Ramp") makeRamp();
		}
		if (visuchoice != "No Visualize") {
			if (lNewlut.length>0) {
				show=1;
				showNewLuts(lutpath,lNewlut,show);
				message="Result of the LUT update process: "+lNewlut.length+" new LUTs has been";
				message=message+"\ninstalled. Use the \"Last Installed Luts Menu Tool\" to see";
				message=message+"\nagain a particular LUT.";
				message=message+"\n \n(Restart ImageJ to make the LUTs available from the";
				message=message+"\n\ÓImage -> Lookup Tables\" menu).";
				showMessage (message);
				UpdateLutList();
			}
		} 
	} else { showMessage ("No new lut available. Your \"luts\" folder is up to date.");}
}

function lastInstalledLutVisu() {
	lutlistfile = getDirectory("startup")+"luts"+File.separator+ " LUTs Update Reports"+File.separator+"Last New Installed Luts.txt";
	if (!File.exists(lutlistfile)) return newArray ("No last luts installed list file found");	list = newArray("No last luts installed list file found");listOfCommands = newArray("no commands");
	lastlutlist =File.openAsString(lutlistfile);
	lines=split(lastlutlist,"\n");
	listOfCommands=newArray(lines.length+2);
	listOfCommands[0]="List of new installed LUTs:";listOfCommands[1]="-";
	for (i=0; i<lines.length; i++) {listOfCommands[i+2]=lines[i];}
	return listOfCommands;
}
function GetthelistofavailableLUTs () {
	if (File.openUrlAsString(urllistoflut) == "") exit("You need an internet access to run this function.");
	showMessageWithCancel ("Get on line available LUTs list?");
	run("URL...", "url=["+urllistoflut +"]");
}

function UpdateLutList() {
	kind="tools";macroname="Luts Macros and Tools Updater.txt";
	macropath=getDirectory("macros") + kind + File.separator+ macroname;
	if (!File.exists(macropath)) exit ("Record this tool into the \"tools\" folder with the name \"" +macroname+ "\",\nto use this menu tool.");
	run("Install...", "install=["+macropath+"]");
}

macro "About Luts Macros and Tools Updater Action Tool -  " {
	about1();
}

// ---- functions ----- //
// Function giving the number (NumberOfMacro) of macros and tools contained a list (lmacros).
function getMacroNumber (lmacros) {
	NumberOfMacro=0;
     	for (i=0; i<lmacros.length; i++) {
         	showProgress(i,lmacros.length);
		if (endsWith(lmacros[i], ".txt") || endsWith(lmacros[i], ".ijm")) NumberOfMacro ++;	}
	return NumberOfMacro;
}

// Function giving the macros number (nMacroinL) and the tools number (nToolinL) containened in an array (lmacros).
// macros are sorted according .txt or .img and 'Macro' or 'Tool' prefixes.
function getMandTNumber (lmacros) {
	nMacroinL=0;nToolinL=0;nToolsetsinL=0;
     	for (i=0; i<lmacros.length; i++) {
         	showProgress(i,lmacros.length);
		if (endsWith(lmacros[i], ".txt") || endsWith(lmacros[i], ".ijm")) {
			if (startsWith(lmacros[i], "Macro ")) nMacroinL ++;
			if (startsWith(lmacros[i], "Tool ")) nToolinL ++;
			if (startsWith(lmacros[i], "Toolset ")) nToolsetsinL ++;
		}		
	}
}

// Function giving the list of macros (Lele) with the prefix (kind) contained in the list (lmacros).
function lPlace (lmacros,Lele,kind) {
	compteur=0;
     	for (i=0; i<lmacros.length; i++) {
         	showProgress(i,lmacros.length);
		if (endsWith(lmacros[i], ".txt") || endsWith(lmacros[i], ".ijm")) {
			if (startsWith(lmacros[i], kind)) {
				Lele[compteur]=substring(lmacros[i],lengthOf(kind),lengthOf(lmacros[i]));
				compteur ++;
			}
		}		
	}
	return Lele;
}

// Function setting in the report  (TheReport) the contents of the list (tab), adding the prefix (prefix).
function listit (tab,prefix) {
	for (i=0; i<tab.length; i++) {
		TheReport=TheReport + "\n"+ prefix+tab[i];
	}
}

// Function giving the number of elements contained in the first list (liste1) and in the second list (liste2).
function nbThings(liste1,liste2) {
	compteur=0;
     	for (i=0; i<liste1.length; i++) {
		showProgress(i,liste1.length);
		for (j=0; j<liste2.length; j++) {
			if (liste1[i]==liste2[j]) compteur ++;
		}
	}
	return compteur;
}

// Function giving the list of elements (New) contained in the first list (liste1) and in the second list (liste2).
function PlaceNewThings (liste1,liste2,New) {
	newcompteur=0;
	test=0;
	for (i=0; i<liste1.length; i++) {
		showProgress(i,liste1.length);
		for (j=0; j<liste2.length; j++) {
			if (liste1[i]==liste2[j]) test ++;
		}
		if (test==0) {
			New[newcompteur]=liste1[i];
			newcompteur ++;
		}
		test=0;
	}
	return New;
}

// Function sorting the number of elements (compteur) which are macros and tools, and can be updated (sorted [n], if n=1 the macro is updatable).
function Nbmacroupdates (macrolocalist,macrodistlist,urlrep,localrep,sorted) {
	compteur=0;
	for (i=0; i<macrolocalist.length; i++) {
		showProgress(i,macrolocalist.length);
		for (j=0; j<macrodistlist.length; j++) {
			if (macrolocalist[i]==macrodistlist[j]) {
				macrotextnih=getdistantmacro (macrodistlist[j],urlrep);				macropath = localrep + macrolocalist[i];
				if (File.exists(macropath)) {
					thelocalmacro = File.openAsString(macropath);
					if (thelocalmacro != macrotextnih) {
						if (macrolocalist[i] != "StartupMacros.txt") {
							compteur=compteur+1;
							sorted[i]=1;
						}
					}
				}
			}
		}
	}
	return compteur;
}

// Function giving the list (listupdate) of macros and tools which can be updated.
function placeUpdatemacro(listupdate,locallist,Sorted) {
	compteur=0;
	for (i=0; i<Sorted.length; i++) {
		if (Sorted[i] == 1) {
			listupdate[compteur]=locallist[i];
			compteur++;
		}
	}
	return listupdate;
}

// Function archiving the old macros.
function archmacros(list,localrep,foldername){
	// Create an Archived Tools directory in tools repertory
	if (list.length > 0) {
		ArchDir = localrep+ " " + foldername+File.separator;
		File.makeDirectory(ArchDir);
		if (!File.exists(ArchDir)) exit("Unable to create the "+foldername+" directory, something wrong in the ImageJ folder");
	}
	for (i=0; i<list.length; i++) {
		if (File.exists(localrep+list[i])) {
			thelocalmacro = File.openAsString(localrep+list[i]);
			f= File.open(ArchDir + list[i]);
			print (f,thelocalmacro);
			File.close(f);
		}
	}
}

// Function installing a list (list) of distant macro located at (urlrep), into the local folder (localrep).
function instalmacro(list,urlrep,localrep){
	for (i=0; i<list.length; i++) {
		showProgress(i,list.length);
		macrotextnih= getdistantmacro (list[i],urlrep);
		f= File.open(localrep + list[i]);
		print (f,macrotextnih);
		File.close(f);
	}
	showProgress(1.0);
}

// Function giving the content of a distant macro (name) located at the distant repertory (urlrep).
function getdistantmacro (name,urlrep) {
	distantmacrolink = urlrep + name;
	if (indexOf(distantmacrolink, " ") > -1) {
		while (indexOf(distantmacrolink, " ") > -1) {
			distantmacrolink=substring(distantmacrolink, 0, (indexOf(distantmacrolink, " ")))+"%20"+substring(distantmacrolink, (indexOf(distantmacrolink, " ")+1),lengthOf(distantmacrolink) );
		}
	}
	showStatus("Internet link...");
	macrotextnih =File.openUrlAsString(distantmacrolink);
	showStatus("");
	return macrotextnih;
}

// Function making a detailed report of every list obtained.
function rapport (FMacros,FTools,FToolsets,lMacro,lTool,lToolsets,NewMacros,NewTools,NewToolsets,UpdatedMacro,UpdatedTool,UpdatedToolsets,localrep,foldername) {
	TheReport="";
	getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
	reportname=  ""+dayOfMonth+"-"+(month+1) +"-"+year+" at "+hour+"."+minute+"."+second+"";
	TheReport=TheReport + "Installation report for the update performed on: "+reportname;
	if ((NewMacros.length+NewTools.length +NewToolsets.length)>0) {
		TheReport=TheReport +"\n\n--- The following new Macros, Tools and Toolsets have been installed: ";
		TheReport=TheReport + "("+(NewMacros.length+NewTools.length+NewToolsets.length)+" Macros: "+NewMacros.length+ " \"macros\" , "+NewTools.length+" \"tools\" & "+NewToolsets.length+" \"toolsets\" ):";
		listit (NewMacros,"Macro ");
		if (NewTools.length > 0) TheReport=TheReport + "\n--------";
		listit (NewTools,"Tool ");
		if (NewToolsets.length > 0) TheReport=TheReport + "\n--------";
		listit (NewToolsets,"Toolset ");
	}
	if(updates==1) {
		TheReport=TheReport + "\n\n--- The following Macros, Tools and Toolsets update have been performed: ";
		TheReport=TheReport +"("+(UpdatedMacro.length+UpdatedTool.length+UpdatedToolsets.length)+" Macros: "+UpdatedMacro.length+ " \"macros\" , "+UpdatedTool.length+" \"tools\" & "+UpdatedToolsets.length+" \"toolsets\"):";
		listit  (UpdatedMacro,"Macro ");
		if (UpdatedTool.length > 0) TheReport=TheReport + "\n--------"; listit (UpdatedTool,"Tool ");
		if (UpdatedToolsets.length > 0) TheReport=TheReport + "\n--------"; listit (UpdatedToolsets,"Toolset ");
	}
	if (updates==0) TheReport=TheReport + "\n\n--- The Macros, Tools and Toolsets updated have not been queried.";
	TheReport=TheReport + "\n\n--- Note: the list of macros, tools and toolsets installed onto you computer before this update ---";
	TheReport=TheReport + "\n      ("+(FMacros.length+FTools.length+ FToolsets.length)+" Macros: "+ FMacros.length + " \"macros\" , "+FTools.length+" \"tools\" & "+ FToolsets.length+" \"toolsets\"):";
	listit (FMacros,"Macro ");TheReport=TheReport +"\n--------";listit (FTools,"Tool ");TheReport=TheReport +"\n--------";listit (FToolsets,"Toolset ");

	TheReport=TheReport +"\n\n--- Note: the list of macros, tools and toolsets available at the ImageJ web site ---";
	TheReport=TheReport + "\n      ("+(lMacro.length+lTool.length+ lToolsets.length)+" Macros: "+ lMacro.length + " \"macros\" , "+lTool.length+" \"tools\" & "+ lToolsets.length +" \"toolsets\"):";
	listit (lMacro,"Macro "); TheReport=TheReport +"\n--------"; listit (lTool,"Tool ");TheReport=TheReport +"\n--------"; listit (lToolsets,"Toolset ");
	UpdateReports = localrep+ " " + foldername+File.separator;
	File.makeDirectory(UpdateReports);
	if (!File.exists(UpdateReports)) exit("Unable to create the "+foldername+" directory, something wrong in the ImageJ folder");
	f= File.open(UpdateReports + reportname+".txt" );	print (f,TheReport);
	File.close(f);
	if (File.exists(UpdateReports + reportname+".txt")) open(UpdateReports + reportname+".txt");
}

function about1() {
	about="";
	about=about + "The \"Luts Macro and Tools Updater\" ImageJ tool bar icons allow to perfom a live";
	about=about + "\nupdate of the ImageJ macros and luts repertories.";
	about=about + "\n \n\"Macro, Tools and Toolsets Updater Menu Tool\" ImageJ tool bar icon:";
	about=about + "\n    - \"Macro, Tools and Toolsets Updater\" looks for new and updated macros,";
 	about=about + "\n       Tools and Toolsets and installs them.";
	about=about + "\n \n\"LUTs Updater Menu Tool\" ImageJ tool bar icon:";
	about=about + "\n    - \"LUTs Updater\" looks for new luts and installs them.";
	about=about + "\n \n\"Last Installed Luts Menu Tool\" ImageJ tool bar icon:";
	about=about + "\n    - Gives a menu of last new installed LUTs, allowing to test them on images or";
	 about=about + "\n      ramps.";
	about=about + "\n \n- Click on the \"On Line Documentation\" ImageJ tool bar icon for more details.";
	about=about + "\n- Click on the \"Version Infos\" ImageJ tool bar icon to look for new beta versions.";
	about=about + "\n------------------------------------------------------------------";
	about=about +"\nAuthor : Gilles Carpentier"+"\nFaculte des Sciences et Technologies"+"\nUniversite Paris 12 Val de Marne, France.";
	showMessage(about);
}
// function specific to the LUTs Updater

function OpenImageLink(question) {
	demoimalocation = getDirectory("startup");	
	fildestination = demoimalocation+ "Downloaded Demo Images/" +demoimagename;
	if (File.exists(fildestination) && ! isOpen(demoimagename)) {
		if (question ==1 ) showMessageWithCancel ("The \"" + demoimagename + "\" has already been downloaded. Open it?");
		open(fildestination);
	}
	else {
		if (! isOpen(demoimagename)) { run("Blobs (25K)"); }
		selectImage(demoimagename);
		nomdimage = getTitle;
		// Create a <Downloaded Demo Images> repertory in ImageJ folder.
		ImaDemo = demoimalocation+"Downloaded Demo Images"+File.separator;
		File.makeDirectory(ImaDemo);
		if (!File.exists(ImaDemo)) exit("Unable to create directory, something wrong in the ImageJ folder");
		selectWindow(nomdimage);
		save(""+ImaDemo+""+ nomdimage +"");
	}
}

function lutRapport (cheminlut,liste) {
	lutrapport="";LastLuts="";
	getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
	reportname=  ""+dayOfMonth+"-"+(month+1) +"-"+year+" at "+hour+"."+minute+"."+second+"";
	lutrapport=lutrapport + "Installation report for the LUT update performed on: "+reportname;
	lutrapport=lutrapport +"\n \nThe following "+liste.length+ " new LUTs has been installed into the \""+cheminlut+ "\"\nrepertory of ImageJ:\n";	for (i=0; i<liste.length; i++) {
		lutrapport= lutrapport + "\nLut name: " + liste[i] ;
		LastLuts=LastLuts  + ShortName (liste[i]) + "\n";
	}
	foldername="LUTs Update Reports";
	LutUpdateReports = cheminlut+ " " + foldername+File.separator;
	File.makeDirectory(LutUpdateReports);
	if (!File.exists(LutUpdateReports)) exit("Unable to create the "+foldername+" directory, something wrong in the ImageJ folder");
	f= File.open(LutUpdateReports + reportname+".txt" );	print (f,lutrapport);
	File.close(f);
	f= File.open(LutUpdateReports + "Last New Installed Luts.txt" );	print (f,LastLuts);
	File.close(f);
	if (File.exists(LutUpdateReports + reportname+".txt")) open(LutUpdateReports + reportname+".txt");
}

// Function giving the number of LUTs in a list
function GetnLUTs (lutlist) {
	NumberOfLUTs=0;
     	for (i=0; i<lutlist.length; i++) {
         	showProgress(i,lutlist.length);
		if (endsWith(lutlist[i], ".lut")) NumberOfLUTs ++;	}
	showProgress(1.0); return NumberOfLUTs;
}

// Function giving the list of elements (Lele) with the sufix ".lut" contained in the list (llut).
function llutPlace (llut,Lele) {
	compteur=0;
     	for (i=0; i<llut.length; i++) {
         	showProgress(i,llut.length);
		if (endsWith(llut[i], ".lut")) {Lele[compteur]=llut[i];compteur ++;}	}
	showProgress(1.0); return Lele;
}

// function opening the new luts
function showNewLuts(cheminLut,lNewlut,show) {
	nomderampe = getTitle; setTool(10);
     	for (i=0; i<lNewlut.length; i++) {
		if (show ==1) {
			open(cheminLut + lNewlut[i]);
			rename(lNewlut[i]);
			if (non == 0) wait(500);
		}
	}
	rename (nomderampe);non=0;
}

function makeRamp () {
	setBatchMode(true);
	if (! isOpen("LUTs visualisation")) { 
		newImage("LUTs visualisation", "8-bit ramp", 256, 32,1);
	} else {selectImage("LUTs visualisation");}
	nomderampe = getTitle;
	setBatchMode("exit and display");
	return nomderampe;
}

function checkForImage (cmd) {
	imaname="";
	if (nImages > 0) {
		setBatchMode(true);visu=0;depth = bitDepth;imaname= getTitle;ok=0;
		if (depth > 8) {
			showMessage("Color tables cannot be assigned to RGB image.\nThe lut will be drawed on a \"ramp\" window,\nor applied to the demo image.");
			rampname= lookForaRamp ();
			if  (rampname == "") {makeRamp ();selectImage("LUTs visualisation");imaname= getTitle;depth = bitDepth;ok=1;}
			if  (rampname != "" && ok==0) {selectImage(rampname);imaname= getTitle;depth = bitDepth;ok=1;}
		} 
		if (depth == 8 && startsWith(imaname, "LUTs :" ) && ok==0) ok=1;
          	if (depth == 8 && imaname == demoimagename)  ok=1;
          	if (depth == 8 && imaname == "LUTs visualisation")  ok=1;
		selectImage (imaname);
		if (ok ==1 && depth ==8) {rename ("LUTs : " + cmd);}
		imaname= getTitle;
		setBatchMode("exit and display");
	}
	return imaname;	
}

function lookForaRamp () {
	rampansvers = "";
	if (nImages > 0) {
		for (i=1; i <= nImages; i++) {
				selectImage(i);imaname=getTitle;
				if (bitDepth == 8 && startsWith(imaname, "LUTs :")) {rampansvers=imaname;}
			} 
		}
	return rampansvers;
}

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


// --- End of code of the macro project ---//
// ----------------------------------//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
// -------------------*** Additionnal code for on line update resources ***-----------------------------

//Developer info
//Kind:Tool
//Title:"Luts Macros and Tools Updater" 
//Version:1.0
//Date:17 November 2006
//Origin:NIH
//End info

macro "Version Infos Action Tool - C0589T1e16VTcf13?" {

	// variables for on line update resources
	beginsign="//Developer info";endsign="//End info"; 
	kind="tools/"; 
	urlrep="http://image.bio.methods.free.fr/ij/ijmacro/macroupdater/";
	name="Luts Macros and Tools Updater.txt"; 
	namedev="Luts Macros and Tools Updater-dev.txt"; 
	favoritefoldername= "Image.Bio.Methods";

	version=versionMessage();
	if (indexOf(version, "install it?" ) > 0 ) {
		macrotext=getdistantmacro (namedev,urlrep);macrolocal="";
		macropath=getDirectory("macros")+kind+namedev;
		if (File.exists(macropath)) {macrolocal=File.openAsString(macropath);}
		if (macrotext != macrolocal) {
			//perfom the installation
			Dialog.create("New version installation option");
			Dialog.addMessage(version);
			Dialog.addCheckbox("Install a Plugin Shortcut?", 0);
			Dialog.addMessage("(This option provides a shortcut in the plugins menu of ImageJ, making easier\nthe next use of the new installed version).");
			Dialog.show();
			plugin= Dialog.getCheckbox();
			f= File.open(macropath);
			print (f,macrotext);
			File.close(f);
			if (plugin ==1) {InstallPluginsStarter(namedev);}
			message="The installation of the "+giveDevInfo (macrotext,1)+ " "+ giveDevInfo (macrotext,2)+ "is completed.";
			message=message+ " Do you want to run it?";
			showMessageWithCancel(message);
			run("Install...", "install=["+macropath+"]");
		}
	} else {showMessage (version); // comment without installation available}
}

function versionMessage() {
	
	version="";
	if (getDirectory("startup") == 0) exit ("Unable to find the startup directory, something wrong in the ImageJ folder");
	if (getDirectory("macros") == 0) exit ("Unable to find the macros directory, something wrong in the ImageJ folder");
	MacroPath=getDirectory("macros");thismacropath=MacroPath+kind+name;
	if (! File.exists(thismacropath)) exit ("This macro has to be recorded under the name of \"" +name+"\"\ninto the \"macros/"+kind+"\" folder of ImageJ.");

	macrotext=File.openAsString(thismacropath);
	macrotextdistant=getdistantmacro (namedev,urlrep);

	version="";macrolocal="";
	version=version + "\n \nThis version of the " + giveDevInfo (macrotext,1) + " " + giveDevInfo (macrotext,2);
	version=version + "is provided by the " + giveDevInfo (macrotext,5)+ " web site.";
	version=version + "\nVersion number: " + giveDevInfo (macrotext,3)+ " - " + giveDevInfo (macrotext,4) +".";
	
	if (macrotextdistant !="" ) {
		new=giveDevInfo (macrotextdistant,3);old=giveDevInfo (macrotext,3);
		if (new > old) {
			macropath=getDirectory("macros")+kind+namedev;
			if (File.exists(macropath)) {macrolocal=File.openAsString(macropath);}
			if (macrotextdistant != macrolocal) {
				update="\n \nA new beta version "+new+ " is available on the "  +giveDevInfo (macrotextdistant,5)+ " web site: ";
				update=update+ "\n \nDo you want to install it?";
			} else {
				update ="\n \nThe new "+new+" beta version called \"" +namedev+ "\" provided by \nthe "+giveDevInfo (macrotextdistant,5) +" web site has already be installed";
				update = update+ " in the \"" +giveDevInfo (macrotext,1)+ "\" repertory \nof ImageJ.";
			}
		} else {
			update="No new Beta version available.";
		}
		version=version +"\n" + update ;
	} 
	return version; 
}

function giveDevInfo (text,n) {
	lines=split(text,"\n");
	if ( (indexOf(text, beginsign)<0) || (indexOf(text, endsign)<0) ) exit ("Not upgradable macro code.");
	for (i=0; lines[i] != endsign; i ++) {}
	for (j=i; lines[j] != beginsign; j --) {}
	infotext=newArray(i-j-1);
	for (i=0; i < infotext.length; i ++) {infotext[i]=lines[i+j+1];}
	info=infotext[n-1]; signature=":";
	cut = indexOf(info, signature);
	info = substring(info,(cut+lengthOf(signature)),lengthOf(info));
	return info;
}

// Function giving the content of a distant macro (name) located at the distant repertory (urlrep).
function getdistantmacro (name,urlrep) {
	macrotextnih="";
	if (File.openUrlAsString("http://rsb.info.nih.gov/ij/macros/Arrays.txt") != "") {
		distantmacrolink = urlrep + name;
		if (indexOf(distantmacrolink, " ") > -1) {
			while (indexOf(distantmacrolink, " ") > -1) {
				distantmacrolink=substring(distantmacrolink, 0, (indexOf(distantmacrolink, " ")))+"%20"+substring(distantmacrolink, (indexOf(distantmacrolink, " ")+1),lengthOf(distantmacrolink) );
			}
		}
		showStatus("Internet link...");
		macrotextnih =File.openUrlAsString(distantmacrolink);
		showStatus("");
	} else { showMessage ("No internet connection to looks for beta version.");}
	return macrotextnih;
}

function InstallPluginsStarter(macroname) {
	// from MacroPluginShortcutsTool.txt
	codestarter = "run\(\"Install...\", \"install=[\"+getDirectory(\"macros\")+\""+kind+ macroname + "\]\"\);";
	if (getDirectory("plugins") == "") exit ("Unable to find the Plugins directory; something wrong in the ImageJ folder.");
	if (endsWith(macroname, ".txt") || endsWith(macroname, ".ijm")) pluginname = substring(macroname, 0, (lengthOf(macroname)-4));
	StarterDir = getDirectory("plugins")+favoritefoldername+File.separator;
	File.makeDirectory(StarterDir);
	if (!File.exists(StarterDir)) exit ("Unable to create "+favoritefoldername+" Macros directory, something wrong in the ImageJ folder.");
	starterplugin = StarterDir + pluginname +"_ .ijm";
	f= File.open(StarterDir + pluginname +"_ .ijm");
	print (f,codestarter);
	File.close(f);
	showMessage ("The plugin shortcut \"" +pluginname+ "\" will be available after\nImageJ restarting, in the \"Plugins->" + favoritefoldername + "\" menu.");
}

// *** End of additionnal code for on line update ressources ***

