function result = path2rc( outputFile )
%PATH2RC Save the current MATLAB path in the pathdef.m file.
%   PATH2RC saves the current MATLABPATH in the pathdef.m
%   which was read on startup.
%
%   PATH2RC outputFile saves the current MATLABPATH in the
%   specified file.
%
%   PATH2RC returns:
%     0 if the file was saved successfully
%     1 if the file could not be saved
%     2 if the original pathdef.m was not found
%     3 if the original pathdef.m was found but couldn't be read
% 
%   See also PATH and PATHTOOL.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.15 $

% Make sure the input file is there
inputFile = which('pathdef.m');
if isempty(inputFile),
  result = 2;
  return;
end  

% Determine the outputFile and make sure it's writable
if nargin==0
  outputFile = inputFile;
end

rc = fopen(outputFile, 'at');
if rc == -1
  result = 1;
  return;
end
fclose( rc );


% Split path into multiple printable lines.
eol = sprintf('\n');         % End of line character(s)
quote = '''';     % Single quote
comma = ',';      % Comma
cont = '...';     % Continuation
tab = sprintf('\t'); % Tab

P = path;
R = [ 'p = [' cont eol ];
ks = findstr(P,pathsep);
k = length(P);
if P(k) ~= pathsep, ks = [ks k]; end
j = 1;
for k = ks
   R = [R tab quote P(j:k) quote comma cont eol];
   j = k+1;
end
R = [R '''''];' ];

% Replace any occurrences of literal matlabroot with the call to matlabroot.
if isunix
  R = strrep(R,[quote matlabroot],['matlabroot,' quote]);
else
  R = strirep(R,[quote matlabroot],['matlabroot,' quote]);
end

% Read the entire pathdef.m file.
rc = fopen( inputFile,'rt' );
if rc==-1,
  result = 3;
  return;
end

T = setstr(fread(rc))';
fclose(rc);


k = findstr(T,'% PATH DEFINED HERE');

% Find the beginning of the next line
k = min(find(T(k(1):end)==eol))+k(1)+1;

% Append the new path string to the pathdef.m header
T = [T(1:k-2) R];

% Write the new pathdef.m file.
rc = fopen( outputFile,'wt' );
if rc ~= -1, 
  fwrite(rc,T);
  fclose(rc);
  result = 0;
  clear pathdef;
else
  result = 1;
end

%---------------------------------------------
function s = strirep(s1,s2,s3)
%STRIREP Replace string with another (case insenstive)
%   S = STRIREP(S1,S2,S3) replaces all occurrences of the string S2 in
%   string S1 with the string S3.  The new string is returned.
%
%   Example:
%       s1='This is a good example';
%       strrep(s1,'good','great') returns 'This is a great example'
%       strrep(s1,'bad','great')  returns 'This is a good example'
%       strrep(s1,'','great')     returns 'This is a good example'
%
%   See also FINDSTR.

% Take care of trivial cases:
%   s2 > s1
%   s1 is empty
%   s2 is empty
if (length(s2)>length(s1)) | isempty(s2) | isempty(s1),
  s=s1;
  return;
end;

% Find all occurrences of s2 in s1
s2len=length(s2);
s2pos=findistr(s1,s2)-1;

% Build resulting string
s=[];
s1pos=1;
for i=1:length(s2pos),
  s=[s,s1(s1pos:s2pos(i)),s3];
  s1pos=s2pos(i)+s2len+1;
end;
s=[s,s1(s1pos:length(s1))];


%-------------------------------------------------
function k = findistr(s1,s2)
%FINIDSTR Find one string within another (case insensitive).
%   K = FINDISTR(S1,S2) returns the starting indices of any occurrences
%   of the shorter of the two strings in the longer.
%
%   Examples
%       s = 'How much wood would a woodchuck chuck?';
%       findstr(s,'a')    returns  21
%       findstr(s,'wood') returns  [10 23]
%       findstr(s,'Wood') returns  []
%       findstr(s,' ')    returns  [4 9 14 20 22 32]
%
%   See also STRCMP, STRNCMP, STRMATCH.

% Make s2 the shorter.
if length(s1) < length(s2)
   t = s1; s1 = s2; s2 = t;
end

% Quick exit for empty string. 
if isempty(s2)
   k = [];
   return
end

% Extend s1 by something as long as s2.
s1 = [s1 zeros(size(s2))];

% Repeatedly shorten an index vector which points to increasingly 
% longer matches. 
k = 1:length(s1)-length(s2);
for i = 1:length(s2)
   k = k(find(lower(s1(k+i-1)) == lower(s2(i))));
   if isempty(k), break, end
end
