function dctAddFileDependenciesToPath(job, dependencyDir)
; %#ok Undocumented

% Copyright 2005-2020 The MathWorks, Inc.

[attachedFilePaths, attachedFileData] = job.pReturnDependencyData;

iCleanupDependencyDir(dependencyDir);

if ~isempty(attachedFileData)
    zipfileName = iCreateZipFile(attachedFileData, dependencyDir);
    cleanupObject = onCleanup( @() iCleanUp(zipfileName) );
    distcomp.pAddDependenciesFromZipfile(zipfileName, attachedFilePaths, dependencyDir);
end

end %iJobStartup

%--------------------------------------------------------------------------
%
%--------------------------------------------------------------------------
function iCleanupDependencyDir(dependencyDir)
% There appears to be some sort of bug with distributed MATLAB and
% the use of files in /tmp that results in the rmdir statement also
% deleting the zipname file. We have been unable to track this down
% and so will deal with the dependency directory before we create
% the zip file - JLM 25/10/04

% delete the directory if it already exists
try
    if exist(dependencyDir,'dir')
        rmdir(dependencyDir,'s');
    end
catch err
    rethrow(err)
end

end % iCreateDependencyDir

%--------------------------------------------------------------------------
%
%--------------------------------------------------------------------------
function zipname = iCreateZipFile(zipbytes, rootdir)
% WRITEFILES - Take uint8 data read by readFiles and write it to a root
% directory.  

try
    mkdir(rootdir);
catch err
    rethrow(err)
end

zipname = [rootdir filesep 'files.zip'];
fid = fopen(zipname,'w+');
fwrite(fid,zipbytes,'int8');
fclose(fid);
end % iWriteFiles

%--------------------------------------------------------------------------
%
%--------------------------------------------------------------------------
function iCleanUp(zipname)
warningSuppressor = parallel.internal.general.WarningSuppressor('MATLAB:DELETE:Permission');
delete(zipname);
if warningSuppressor.didSuppressedWarningOccur()
    dctSchedulerMessage(1, "Failed to delete file dependencies zip file: " + zipname);
end
end
