function sharedUtilRelativeFolder = unpackSharedCodeFromProtectedModel(protectedModel, dstFolder)
% UNPACKSHAREDCODEFROMPROTECTEDMODEL
%   Unpack the rtwsharedutils relationship from a protected model into the
%   destination folder specified by dstFolder.
%
%   Return the location of the extracted files relative to dstFolder.

% Copyright 2019-2020 The MathWorks, Inc.
%

[opts, fullName] = Simulink.ModelReference.ProtectedModel.getOptions(protectedModel);
buildDirs = RTW.getBuildDir(opts.modelName);
rootTargetDir = fullfile(dstFolder, buildDirs.ModelRefRelativeRootTgtDir);
currentTarget = Simulink.ModelReference.ProtectedModel.getCurrentTarget(opts.modelName);
year = Simulink.ModelReference.common.RelationshipTargetSharedUtils.getRelationshipYear();


h = new_system;
topMdl = get_param(h, 'Name');
c = onCleanup(@()bdclose(h));

% We might need to ask for a password at this point.
if opts.isRTWEncrypted
    % Need the model to be on the path whilst we do the extraction to avoid
    % correct passwords being rejected as incorrect by extractSharedUtils.
    oldPath = addpath(fileparts(fullName));
    c2 = onCleanup(@()path(oldPath));
    isMenuSim = true;  % Not strictly a menu simulation, but we want the dialog to pop up.
    Simulink.ModelReference.ProtectedModel.getPasswordFromDialog(protectedModel, topMdl, 'RTW', isMenuSim);
end

isPackagedModel = false;
xrelType = Simulink.ModelReference.ProtectedModel.CrossReleaseWorkflowType.SharedCodeUpdate;

Simulink.ModelReference.ProtectedModel.extractSharedUtils(fullName, rootTargetDir, currentTarget, 'rtwsharedutils', ...
    year, topMdl, dstFolder, buildDirs, isPackagedModel, xrelType);
sharedUtilRelativeFolder = buildDirs.SharedUtilsTgtDir;

end
