function [sDirFiles, mlrFiles, fPathNames, names, pathToTopBuildInfo] = getHierarchicalFileList(h, fPathNames, names)    

%   Copyright 2012-2019 The MathWorks, Inc.
    
    % the first step is to get all of the files under the start dir.  
    [sDirFiles, sDirFilesIdx, pathToTopBuildInfo] = locGetFilesUnderStartDir(h, fPathNames);    
    
    % remove these files
    fPathNames = fPathNames(~sDirFilesIdx);
    names = names(~sDirFilesIdx);
    
    %---------------------------------------------------------
    % similar to the startdir files, get all of the files under matlabroot
    [mlrFiles, mlrFileIdx] = locGetFilesUnderMatlabroot(h, fPathNames);    

    % remove the files under matlabroot since they have  been taken care of
    fPathNames = fPathNames(~mlrFileIdx);
    names = names(~mlrFileIdx);

    
    %---------------------------------------------------------
    % the only items left have explicit paths to them.  they get bundled into a
    % single file with no path stored.
    if ~isempty(fPathNames)
        % check for duplicate file
        h.checkForDups(fPathNames,names);
    end
end

%---------------------------------------------------------
% function: locGetFilesUnderStartDir 
%
% Summary: Get all files in the start directory. 
%
% Inputs: 
%   h - buildInfo
%   fPathNames - reduced list of path names
%
% Outputs:
%   sDirFiles - list of source dir files
%   sDirFilesIdx - indices in original list
%---------------------------------------------------------
function [sDirFiles, sDirFilesIdx, pathToTopBuildInfo] = locGetFilesUnderStartDir(h, fPathNames)
    % We need to trim off the front part of the path so that the relative 
    % path info can be stored in the zip file.
    sDir = coder.internal.getStartDirForPackNGo(h);        

    sDirRegexpID =  regexptranslate('escape', [sDir{1} h.Settings.FileSep]);
    
    sDirFilesIdx = ~cellfun(@isempty,regexp(fPathNames,sDirRegexpID));
    sDirFiles = fPathNames(sDirFilesIdx);
    
    % strip off the start dir to make the paths relative
    sDirFiles =  regexprep(sDirFiles,[sDirRegexpID '(.*)'],'$1');

    % determine location where buildInfo will be serialized
    bDir = h.getSourcePaths(true,{'BuildDir'});
    pathToTopBuildInfo = fullfile(bDir{1}, 'buildInfo.mat');
    pathToTopBuildInfo =  regexprep(pathToTopBuildInfo,[sDirRegexpID '(.*)'],'$1');
end

%---------------------------------------------------------
% function: locGetFilesUnderMatlabroot 
%
% Summary: Get all files in the matlab directory. 
%
% Inputs: 
%   h - buildInfo
%   fPathNames - reduced list of path names
%
% Outputs:
%   mlrFiles - list of matlab dir files
%   mlrFileIdx - indices in original list
%---------------------------------------------------------
function [mlrFiles, mlrFileIdx] = locGetFilesUnderMatlabroot(h, fPathNames)
    mlrID = [h.Settings.Matlabroot h.Settings.FileSep];
    mlrRegexpID = regexptranslate('escape', mlrID);
    mlrFileIdx = ~cellfun(@isempty,regexp(fPathNames,mlrRegexpID));
    mlrFiles = fPathNames(mlrFileIdx);

    %make the path relative
    mlrFiles = regexprep(mlrFiles,[mlrRegexpID '(.*)'],'$1');
end
