function str = add_path(pi,mode,ex)

% add_path - add directories to path
% ----------------------------------
%
% str = add_path(pi,'flat')
%     = add_path(pi,'rec',ex)
%
% Input:
% ------
%  pi - initial directory
%  ex - directories to exclude in recursive call
%
% Output:
% -------
%  str - string appended to path

%--------------------------------
% Author: Harold Figueroa
%--------------------------------
% $Revision: 132 $
% $Date: 2004-12-02 15:58:47 -0500 (Thu, 02 Dec 2004) $
%--------------------------------

%--
% set mode
%--

if (nargin < 2)
	mode = 'flat'; 
end 

%--
% set excluded directories list
%--

if (((nargin < 3) | isempty(ex)) & strcmp(mode,'rec'))
	ex = {''};
end

%--
% create string to append to path according to mode
%--

switch (mode)
	
	%--
	% single directory mode
	%--
	
	case ('flat')

		tmp = what_ext(pi);
		
		str = tmp.path;
		
		for k = 1:length(tmp.dir)
			sub = [pi, filesep, tmp.dir{k}];
			str = [str pathsep sub];
		end
		
	%--
	% recursive directory search mode
	%--
	
	case ('rec')

		tmp = what_ext(pi);
		
		str = tmp.path;
		
		for k = 1:length(tmp.dir)
			
			%--
			% check whether directory appears in excluded list
			%--
			
			ix = find(strcmp(tmp.dir{k},ex));
			
			%--
			% add directory if it not a method or excluded directory
			%--
			
			if (isempty(ix) & (tmp.dir{k} ~= '@'))
				
				tmp_str = add_path([pi, filesep, tmp.dir{k}],'rec',ex);
				str = [str pathsep tmp_str];
				
			end
			
		end
		
end

%--
% DEBUG -- display string appended to path
%--

% sprintf(['\t', strrep(strrep(str,'\','\\'),';',';\n\t')])

%--
% append string to path when no output
%--

if (~nargout)
	path(path,str);
end