function openExample(file, varargin)
%openExample Open an example for modification and execution.
%   openExample(E) opens an example, identified by E, in a new folder. If
%   the folder already exists, it opens the existing version.
%   openExample(E,'supportingFile', SF) opens an example, identified by E, and launches a supporting file,
%   identified by SF.
%   openExample(E,'workDir', WD) opens an example, identified by E, in a folder,
%   identified by WD.


%   Copyright 2015-2023 The MathWorks, Inc.

% parse the input.
p = inputParser;
validationFcn = @(x) validateattributes(x,{'char','string'},{'nonempty'});
addRequired(p,'file', validationFcn);
addParameter(p,'supportingFile', '', validationFcn);
addParameter(p,'workDir', '', validationFcn);
parse(p, file, varargin{:});
file = p.Results.file;
workDir = p.Results.workDir;
supportingFile = p.Results.supportingFile;

[exampleId, isMainFile] = matlab.internal.examples.identifyExample(file);
if ~isMainFile
    supportingFile = file;
    file = exampleId;
end

metadata = findExample(exampleId);
if numel(metadata.project) > 0
    % Check if a project is open. If so, ask the user if they want to close
    % it before opening the example project.
    choice = matlab.internal.project.example.closeCurrentProjectRequest;
    if ~choice
        % User clicked cancel, so do not carry on
        return
    end
end

[workDir,metadata] = setupExample(metadata, workDir);

override = false;
if ~isempty(supportingFile)
    override = true;
    overrideFile = supportingFile;
end


% Change folder for runnability and such.
cd(workDir)

% Reset workDir to account for symbolic links in user's $HOME path
workDir = pwd;

% Open.
openCompleted = false;
if override
    openCompleted = matlab.internal.examples.openSupportingFile(workDir, metadata, overrideFile);
end

if ~openCompleted
    for iFiles = 1:numel(metadata.files)
        f = metadata.files{iFiles};
        if f.open
            open(fullfile(workDir,f.filename))
            openCompleted = true;
            break;
        end
    end
end

if ~openCompleted
    for i = 1:numel(metadata.project)
        p = metadata.project(i);
        if p.open
            if p.supported
                openProject(p.root);
            else
                [~, ~, topLevelProjectRoot] = matlab.internal.project.example.projectDemoSetUp(metadata.project.path, ...
                                          workDir, metadata.project.cmSystem);
                openProject(topLevelProjectRoot);
            end
            openCompleted = true;
            break;
        end
    end
end

if ~openCompleted
    if isfield(metadata,'callback')
        eval(metadata.callback)
        openCompleted = true;
    end
end

if ~openCompleted
    mainFile = matlab.internal.examples.getMainFile(metadata);
    edit(fullfile(workDir, mainFile))
end

end
