function entries = getArchiveEntries(archiveFilename, files, rootDir, fcnName)
%GETARCHIVEENTRIES Get entries for an archive
%
%   GETARCHIVEENTRIES gets the entries for an archive specifed by
%   ARCHIVEFILENAME.  FILES is a cell array of filenames to add to the
%   archive.  ROOTDIR is the root directory to search for the filenames
%   specified by FILES.  FCNNAME is the name of the calling function.
%
%   The return argument, ENTRIES, is a structure containing fields FILE
%   and ENTRY. FILE is the full filename of ENTRY. 

%   Copyright 2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $   $Date: 2004/11/23 20:39:58 $

% Create archivePath and archiveFile to be used in preventing
% archiveFilename from being added to the archive
[archivePath, archiveName, archiveExt] = fileparts(archiveFilename);
archiveFile = [archiveName archiveExt];
archivePath = fullfile(archivePath,archiveFile);

% Create a structure of the inputs.
inputs = [];
for i = 1:length(files)
   filename = files{i};
   if isAbsolute(filename)
      [path,base,ext] = fileparts(filename);
      inputs(i).path = path;
      inputs(i).file = [base ext];
   else
      inputs(i).path = rootDir;
      inputs(i).file = filename;
   end
end

% Build up list of entries.
entries = [];
while ~isempty(inputs)
   % Pop the next entry off the stack.
   path = inputs(1).path;
   file = inputs(1).file;
   inputs(1) = [];

   filePath = fullfile(path,file);
   if ~isempty(find(file == '*',1))
      dirContents = dir(filePath);
      listing = {dirContents.name};
      listing = setdiff(listing,{'.','..'});
      % Push all matches onto the stack.
      for i = 1:length(listing)
         inputs(end+1).path = path;
         inputs(end).file = fullfile(fileparts(file),listing{i});
      end
   elseif exist(filePath,'dir')
      dirContents = dir(filePath);
      listing = {dirContents.name};
      listing = setdiff(listing,{'.','..'});
      % Push directory contents onto the stack.
      for i = 1:length(listing)
         inputs(end+1).path = path;
         inputs(end).file = fullfile(file,listing{i});
      end
   elseif ~exist(filePath,'file')
      eid=sprintf('MATLAB:%s:fileDoesNotExist',fcnName);
      error(eid,'File "%s" does not exist.',filePath);
   else
      if ispc
         file        = strrep(file,'\','/');
         filePath    = strrep(filePath,'\','/');
         archiveFile = strrep(archiveFile,'\','/');
         archivePath = strrep(archivePath,'\','/');
      end
      file        = strrepdotslash(file);
      filePath    = strrepdotslash(filePath);
      archiveFile = strrepdotslash(archiveFile);
      archivePath = strrepdotslash(archivePath);
      if isequal(archiveFile, file) && ...
            (isequal(archivePath, filePath) || ...
             isequal(dir(archivePath),dir(filePath)))
         % do not add the archiveFilename to the entries structure
         wid = sprintf('MATLAB:%s:archiveName', fcnName);
         warning(wid,'%s can not add the archive file: %s', ...
                     upper(fcnName),archiveFilename);
      else
         entries(end+1).file = filePath;
         entries(end).entry = file;
      end
   end
end

% If there is nothing to do, error.
% There is no such thing as an "empty archive".
if isempty(entries)
   eid=sprintf('MATLAB:%s:noEntries', fcnName);
   error(eid,'Nothing to %s.',fcnName)
end

%--------------------------------------------------------------------------
function name = strrepdotslash(name)
if strncmp(name,'./',2)
   % Remove first occurrence of './'
   name = name(3:end);
end
