function p = genpath(d)
%GENPATH Generate recursive toolbox path.
%   P = GENPATH returns a new path string by adding
%   all the directories below MATLABROOT/toolbox.
%
%   P = GENPATH(D) returns a path string formed by 
%   recursively adding all the directories below D.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.9 $ $Date: 2000/06/01 19:52:50 $

if nargin==0,
  p = genpath(fullfile(matlabroot,'toolbox'));
  if length(p) > 1, p(end) = []; end % Remove trailing pathsep
  return
end

% Generate path based on given root directory
files = dir(d);
if isempty(files)
  return
end

%
% Add d to the path if it contains files, otherwise don't
% bother.  For example, toolbox\matlab contains only directories
% which makes it unneccessary to add this directory to the path.
% On the other hand toolbox\matlab\general, for example, contains
% files that users may want to access.
%
p = '';
isdir = logical(cat(1,files.isdir));
if ~all(isdir)
  p = [p d pathsep];
end

%
% Recursively descend through directories which are neither
% private nor "class" directories.
%

methodsep = '@';

dirs = files(isdir);
for i=1:length(dirs)
  dirname = dirs(i).name;
  if ~strcmp( dirname,'.')         &...
     ~strcmp( dirname,'..')        &...
     ~strncmp(dirname,methodsep,1) &...
     ~strcmp( dirname,'private') 

   p = [p genpath(fullfile(d,dirname))];
  end
end
