function updateFileSeparator(h,varargin)
%UPDATEFILESEPARATOR - this function updates the file separator on all paths in
% the Build Info Object to the value specified in the Settings object.  This
% includes all source and include paths, as well as file paths.  
%

%   Copyright 2005-2020 The MathWorks, Inc.

if (nargin > 1)
    [varargin{:}] = convertStringsToChars(varargin{:});
    if ~ismember(varargin{1},{'/','\'})
        DAStudio.error('RTW:buildInfo:badFileSep');
    end
    
    % if the new filesep is the same as the old filesep, then don't update
    % the paths since it is a somewhat time-consuming operation.
    if ~strcmp(varargin{1},h.Settings.FileSep)
        h.Settings.FileSep = varargin{1};
    end
end

% because this is used as a regexp expression, the '\' must be
% converted to '\\' because '\' is the regexp escape char.
if strcmp(h.Settings.FileSep,'\')
    regexp_filesep = '\\';
else
    regexp_filesep = h.Settings.FileSep;
end

% Matlabroot and other special dirs are in the settings object.  these
% needs to be updated for proper makefile generation.
h.Settings.Matlabroot = regexprep(h.Settings.Matlabroot,...
                                  '[\\/]',regexp_filesep);
h.Settings.LocalAnchorDir = regexprep(h.Settings.LocalAnchorDir,...
                                      '[\\/]',regexp_filesep);

% update the local properties
h.TargetPreCompLibLoc = regexprep(h.TargetPreCompLibLoc,...
                                  '[\\/]',regexp_filesep);
h.InstallDirmexopts = regexprep(h.InstallDirmexopts,...
                                '[\\/]',regexp_filesep);

% update all the key/val pair vectors
locUpdateKeyValPairFileSep(h,'BuildArgs');
locUpdateKeyValPairFileSep(h,'TMFTokens');

% update the include and source modules for the root buildInfo object
h.Src.updateFileSeparator();
h.Inc.updateFileSeparator();
h.Other.updateFileSeparator();

% for each of the link objects, update the filesep on all paths
for i=1:length(h.LinkObj)
    h.LinkObj(i).updateFileSeparator();
end

% for each of the model refs, update the filesep on all paths
for i=1:length(h.ModelRefs)
    h.ModelRefs(i).updateFileSeparator();
end

function locUpdateKeyValPairFileSep(h, type)

switch (type)
  case 'BuildArgs'
    kvVec = h.BuildArgs;
  case 'TMFTokens'
    kvVec = h.Tokens;
  otherwise
    % this should never happen since it is a local function, but just to be
    % safe throw an error.
    DAStudio.error('RTW:utility:invalidInputArgs',...
                   'locUpdateBuildArgFileSep');
end

% get the groups first, if there aren't any 'paths' then there is no need
% ot get the values
grps = get(kvVec,'Group');
if isempty(grps)
    grps = {};
end
if ~iscell(grps)
    grps = {grps};
end

% find the elements that are marked as paths
idx = ~cellfun(@isempty,regexp(grps,'.*_PATH$'));    
if ~any(idx)
    return;
end

vals = get(kvVec,'Value');
if isempty(vals)
    vals = {};
end
if ~iscell(vals)
    vals = {vals};
end

vals(idx) = coder.make.internal.standardizeFileSep(vals(idx), h.Settings.FileSep);
set(kvVec,{'Value'},vals);
