function update_toolbox_cache
%UPDATE_TOOLBOX_CACHE Generate Cache of Path and Files on Path
%	UPDATE_TOOLBOX_CACHE saves the toolbox portion of the current
%       MATLAB path and the list of the contents of each directory,
%	as well as method and private directories contained by
%	the toolbox directories on the path.
%
%	The saved MAT file contains an array of structures CachedPath.
%
%	CachedPath.name contains the name of the directory, relative
%	to MATLABROOT.
%
%	CachedPath.contents is a character string containing all
%	names in the directory, each separated by 0 and the string 
%	terminated by 0 0.
%
%	CachedPath.isdir is a character flag vector with one
%	value per name, with 1 indicating that the name is a directory,
%	and 0 otherwise.

%   CGN
%   Copyright 1984-2000 by The MathWorks, Inc.
%   $Revision: 1.2 $ $Date: 2000/07/19 16:22:00 $
%	

%
% Platform-specific preface.
%
cname = computer;
if strncmp(cname,'PC',2)
  DIRSEP = '\';
  TBX = [matlabroot DIRSEP 'toolbox' DIRSEP];
else
  DIRSEP = '/';
  TBX = [matlabroot DIRSEP 'toolbox' DIRSEP];
end
TBXSIZ = size(TBX); TBXSIZ = TBXSIZ(2);

%
% Check whether toolbox cache is writeable
%
tbxcname = [TBX 'local' DIRSEP 'toolbox_cache'];
tbxcnamefull = [tbxcname '.mat'];
tbxcache_exists = exist(tbxcnamefull);
tbxcfid = fopen(tbxcnamefull, 'wb');

if tbxcfid < 0,
  error(['Write access to ' TBX 'local is required']);
end;

fclose(tbxcfid);

%
% Cleanup any file that wsa created a a side-effect of probing the
% writeability of toolbox/local.
%
if tbxcache_exists <= 0,
  try
    delete(tbxcnamefull);
  catch
  end
end;

%
% Get path, path size.
% It's not necessary to do a REHASH TOOLBOX before doing this, because
% WHAT always goes out to the file system, even for toolboxes.
%
p = what(char(0));
ps = size(p);  ps = ps(1);

%
% Count number of toolbox directories.
%
ntb = 1;
for i=1:ps
  j = size(p(i).path);
  if (j(2) >= TBXSIZ & strncmp(TBX, p(i).path, TBXSIZ) == 1)
    ntb = ntb+1;
  end
end

%
% Preallocate path structure
%
d = clock;  d = datestr(datenum(d(1),d(2),d(3),d(4),d(5),d(6)),0);
CachedPath(1).name = ['Path saved ' d];
CachedPath(1).contents = matlabpath;
CachedPath(1).isdir = '/';
if (ntb > 1)
  CachedPath(ntb).name = '';
end

%
% Build each directory contents member of path structure.
%
ntb = 1;
for i=1:ps
  j = size(p(i).path);
  if (j(2) >= TBXSIZ & strncmp(TBX, p(i).path, TBXSIZ) == 1)
    % Normalize toolbox directory name.
    tn = p(i).path; tn = tn(TBXSIZ+1:end);
    if (tn(end) == DIRSEP | tn(end) == ']')
	tn = tn(1:end-1);
    end
    if (DIRSEP ~= '/')
	tn = strrep(tn, DIRSEP, '/');
    end
    % Get directory contents.
    d=dir(p(i).path);
    % Get number of names in directory. 
    k=size(d); k=k(1);
    % Build the contents string and the isDir bitmap.
    n = '';  nf = zeros(1,k);
    for j=1:k
      n = [n d(j).name 0];
      nf(j) = d(j).isdir;
    end;
    n = [n 0];
    % Full in path structure member for this dir.
    ntb = ntb+1;
    CachedPath(ntb).name = tn;
    CachedPath(ntb).contents = n;
    CachedPath(ntb).isdir = char(nf);
  end
end

%
% Save to MAT file
%
save(tbxcname, 'CachedPath');

% End of update_toolbox_cache.m





