function [ret, groups] = getPaths(h, type, replaceMatlabroot, varargin)
%GETPATHS - returns a cell array of paths.  this function is called like this:
% 
% paths = getPaths(type, concatenatePaths, replaceMatlabroot,...
%                  includeGroupIDs, excludeGroupIDs)
%
%    type               This argument controls which types(include or source
%                       paths) of paths are returned. It must be one of:
%                       {'include', 'source', 'all'}
%
%    replaceMatlabroot  logical value, if true will automatically replace
%                       $(MATLAB_ROOT) with the hard-coded path.  if
%                       concatenatePaths is false. this arg has no effect.
%
%    includeGroupIDs    This is a cell array of Group IDs.  Only paths that
%                       have GroupID in this set will be returned.  If this
%                       is empty, the all paths are included
%
%    excludeGroupIDs    This is a cell array of Group IDs. Any paths that have
%                       one of these Group ID's will be excluded.
%
% includeGroupIDs and excludeGroupIDs are optional


% Copyright 2005-2019 The MathWorks, Inc.

%check the args to make sure they all conform to the expected format
[argsOK, repMLR, incGroups, excGroups] = ...
    locCheckArgs(type, replaceMatlabroot, varargin{:});
if ~(argsOK)
    DAStudio.error('RTW:buildInfo:badMethodArguments',...
                   'RTW.BuildInfoBuildObj.getPaths');
end

ret = {};
groups = {};

paths  = {};
pathGroups = {};

if any(strcmp(type, {'include', 'all'}))
    paths      = [paths;     get(h.Inc.Paths,'Value')];
    pathGroups = [pathGroups; get(h.Inc.Paths,'Group')];
end

if any(strcmp(type, {'source', 'all'}))
    paths      = [paths;      get(h.Src.Paths,'Value')];
    pathGroups = [pathGroups; get(h.Src.Paths,'Group')];
end

if strcmp(type, 'syslib')
    paths      = [paths;      get(h.SysLib.Paths,'Value')];
    pathGroups = [pathGroups; get(h.SysLib.Paths,'Group')];
end

if isempty(pathGroups)
    pathGroups(1:length(paths)) = {''};
end

if isempty(incGroups)
    includeIdx = true(length(paths), 1);    
else
    includeIdx = ismember(pathGroups, incGroups);
end

if isempty(excGroups)
    excludeIdx = false(length(paths), 1);    
else
    excludeIdx = ismember(pathGroups, excGroups);
end

includeIdx = includeIdx & (~excludeIdx);

paths = paths(includeIdx);
% if there are no paths left, then return an empty cell array
if isempty(paths)
    return;
end

% should special tokens like $(MATLAB_ROOT) be replaced?
if (repMLR)
    specialDirs = h.Settings.getSpecialDirsAndTokens('includeRegexpPats',...
                                                     true);
    sToks = regexptranslate('escape',specialDirs.toks);
    ret = regexprep(paths, sToks, specialDirs.regexpPats)';
else
    ret = paths';
end

groups    = pathGroups(includeIdx);

if ~iscell(ret)
    ret = {ret};
    groups = {groups};
end

function [argsOK, repMLR, incGroups, excGroups] =...
    locCheckArgs(type, replaceMatlabroot, varargin)

argsOK = true;
incGroups = {};
excGroups = {};

% make sure the type was specified correctly
tf = strcmp(type,{'include', 'source', 'all', 'syslib'});
if ~any(tf)
    argsOK = false;
    return;
end

% for replaceMatlabroot just check for non-zero versus 0
% value, it is not critical that they it is actually a logical.
repMLR      = (replaceMatlabroot ~= 0);

nargs = length(varargin);
% all of the args must be cell arrays for the caller.  If a char array
% or scalar is sent int, just convert them to cell arrays

% the first arg is the incGroups
if (nargs >= 1)
    if ~iscellstr(varargin{1})
        if ~ischar(varargin{1})
            % this is an incompatible data type
            argsOK = false;
            return;
        end
        % Only use this arg if it is not ''.
        if ~isempty(varargin{1})
            incGroups = varargin(1);
        end
    else
        %get the unique list, just in case there are dups
        incGroups = unique(varargin{1});
    end
end

% the second arg is the excGroups
if (nargs >= 2)
    if ~iscellstr(varargin{2})
        if ~ischar(varargin{2})
            % this is an incompatible data type
            argsOK = false;
            return;
        end
        % Only use this arg if it is not ''.
        if ~isempty(varargin{2})
            excGroups = varargin(2);
        end
    else
        %get the unique list, just in case there are dups
        excGroups = unique(varargin{2});
    end
end
