function searchdb(goUp)
% Website search database generation
% Generate javascript searchdb.js that serves as database for 
% javascript-based site search engine
% Version 1.0
% Last modified 26.12.01
% For Matlab 5.x		 
% File searchdb.m						 
% Copyright (c) 2000-2001 Igor Kagan					 
% kigor@tx.technion.ac.il
% http://igoresha.virtualave.net
%
% Usage: In Matlab Command Window, change to your site root directory and type:
% searchdb (first add the directory containing searchdb.m to Matlab search path)
%
% Description:
% This function uses <title>...</title>
% <meta name="description" content="..."> html tags
% <meta name="KeyWords" content="...">
% to fill the entry for each html/htm file.
% If these tags are not specified, searchdb will include filename for this file's entry
% searchdb will create (and _overwrite_ if file with this name already exists!) 
% file searchdb.js, which can be edited in any HTML/text
% editor later, if there is a need to do so.
% 
% Note that links will _not_ work while you are in your local hard drive! 
% Once you upload this file in the root dir of your server, they will work.

% Changes history
% Version 1.0 - first version

 
global smc nfiles ndirs baseurl	% need access these variables during recursive calls 

if nargin < 1,

	% add to path
      	curdir = cd;
      	matlabpath = path;
      	if isempty(findstr(lower(matlabpath),lower(curdir))),
        	path(matlabpath,curdir);
      	end;

	% define startup parameters
	baseurl = pwd;
	goUp	= 0;
	smc	= []; % js contents
	nfiles	= 0;
	ndirs	= 1;  % first is root dir
	startentry = ['var item = new Array(); c=-1;' sprintf('\n')];
	smc	= [smc startentry];
end


% get current dir listing
d = dir;
dirname = [strrep(pwd,baseurl,'') '']; % for root dir
dirname = [strrep(dirname,'\','/')];

if strcmp(dirname,''),
	dirname = '';
else
	dirname = [dirname,'/'];
end
	   
direntry = ['//***',dirname,'***' sprintf('\n')];
ndirs = ndirs + 1;
smc = [smc direntry];
       
d = d(3:length(d));

[t,ind]=sort(cat(1,d.isdir));
d = d(ind);
for i=1:size(d,1),
	if d(i).isdir,
		cd(d(i).name);
		searchdb(1);
	else
		if findstr(d(i).name,'.htm'),
			disp(d(i).name);
			fidr=fopen(d(i).name,'r');
			F = fread(fidr);
			fclose(fidr);
			F = F(find(F~=10 & F~=13 & F~=9));
			S = char(F');
			s = lower(S);
			Title='';
			% find title
			i1 = findstr(s,'<title>'); % length 7
			i2 = findstr(s,'</title>');
			Title = S(i1+7:i2-1);
			% find description
			i1 = findstr(s,'<meta name="description" content="'); % length 34
			if ~isempty(i1),
				i2 = findstr(s(i1+1:length(s)),'">');
				Description = S(i1+34:i1+i2(1)-1);
			else
				Description = '';
			end
			% find keywords
			i1 = findstr(s,'<meta name="keywords" content="'); % length 31
			if ~isempty(i1),
				i2 = findstr(s(i1+1:length(s)),'">');
				Keywords = S(i1+31:i1+i2(1)-1);
			else
				Keywords = '';
			end

			% write file entry
			fileentry = ['c++; item[c]=new Array("',d(i).name,'","',dirname,'","',Title,'","',Keywords,'","',Description,'");' sprintf('\n')];
			nfiles = nfiles + 1;
			smc = [smc fileentry];	     
		end
		
				       
	end

end
if (goUp),
	cd ..
else
	% finish html
	finishentry = ['// ',num2str(nfiles),' files in ',num2str(ndirs),'dirs included'];
	smc = [smc finishentry];
	% write searchdb.js file
	if exist([cd '\searchdb.js']),
		!del searchdb.js
	end
	fidw = fopen('searchdb.js','wt');
	fwrite(fidw,smc,'char');
	fclose(fidw);
	       
end
return


