function p = path(a,b)
%PATH Get/set search path.
%   PATH, by itself, prettyprints MATLAB's current search path. The
%   initial search path list is set by PATHDEF, and is perhaps
%   individualized by STARTUP.
%
%   P = PATH returns a string containing the path in P.
%   PATH(P) changes the path to P.  PATH(PATH) refreshes MATLAB's
%   view of the directories on the path, ensuring that any changes
%   to non-toolbox directories are visible.
%
%   PATH(P1,P2) changes the path to the concatenation of the two path
%   strings P1 and P2.  Thus PATH(PATH,P) appends a new directory to
%   the current path and PATH(P,PATH) prepends a new path.  If P1 or
%   P2 are already on the path, they are not added.
%
%   For example, the following statements add another directory
%   to MATLAB's search path on various operating systems:
%
%     Unix:     path(path,'/home/myfriend/goodstuff')
%     Windows:  path(path,'c:\tools\goodstuff')
%
%   See also WHAT, CD, DIR, ADDPATH, RMPATH, GENPATH, REHASH.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 5.22 $  $Date: 2000/09/14 14:01:17 $

casesen = isunix; % Only do case sensitive search on UNIX and VMS

if nargin == 0  % pretty-print
    if nargout == 0
      matlabpath
    end
elseif nargin == 1
    if ~isstr(a)
      error('Arguments must be strings.')
    end
    if strncmp(computer,'PC',2),
      a = strrep(a,'/','\'); % Convert '/' to '\'
    end
    matlabpath(a)
elseif nargin == 2
    if ~isstr(a) | ~isstr(b)
      error('Arguments must be strings.')
    end

    % look to see if a is contained in b or vice versa - if so, don't add it
    pp = matlabpath;

    if strncmp(computer,'PC',2),
      a = strrep(a,'/','\'); % Convert '/' to '\'
      b = strrep(b,'/','\'); % Convert '/' to '\'
    end
    if isempty(a)
      if ~strcmp(pp,b), matlabpath(b), end
    elseif isempty(b)
      if ~strcmp(pp,a), matlabpath(a), end
    else
      % Check for special cases path(a,path) or path(path,b)
      if strcmp(pp,a), append = 1; else append = 0; end

      % Add pathseparator to a and b
      if ~isempty(a) & a(end)~=pathsep, a = [a pathsep]; end
      if ~isempty(b) & b(end)~=pathsep, b = [b pathsep]; end

      %
      % Check to see if any directories in b are in a.
      %
      ak = [0 find(a==pathsep)]; % Beginning of each path
      bk = [0 find(b==pathsep)]; % Beginning of each path

      % End of each path (ignoring trailing fileseps).
      ake = ak(2:end)-(a(ak(2:end)-1)==filesep);
      bke = bk(2:end)-(b(bk(2:end)-1)==filesep);

      if append,
        % Find and remove matching directories
        for i=length(ak)-1:-1:1
          aa = a(ak(i)+1:ake(i)-1);
          for j=1:length(bk)-1
            bb = b(bk(j)+1:bke(j)-1);
            if ~casesen, aa = lower(aa); end
            if ~casesen, bb = lower(bb); end
            if strcmp(aa,bb), 
              a(ak(i)+1:ak(i+1)) = [];
              ak(i+1) = ak(i); % Update next value before deleting
              ak(i) = [];
              ake(i) = [];
            end
          end
        end

      else
        % Find and remove matching directories
        for j=length(bk)-1:-1:1
          bb = b(bk(j)+1:bke(j)-1);
          for i=1:length(ak)-1,
            aa = a(ak(i)+1:ake(i)-1);
            if ~casesen, aa = lower(aa); end
            if ~casesen, bb = lower(bb); end
            if strcmp(aa,bb), 
              b(bk(j)+1:bk(j+1)) = [];
              bk(j+1) = bk(j); % Update next value before deleting
              bk(j) = [];
              bke(j) = [];
            end
          end
        end

      end
      if isempty(b),
        matlabpath(a(1:end-1));
      elseif isempty(a)
        matlabpath(b(1:end-1));
      else
        matlabpath([a b(1:end-1)]);
      end
   end
end

if nargout==1, p = matlabpath; end
