function out=openprj(filename)
%OPENPRJ opens a MATLAB Compiler, MATLAB Coder project or MATLAB Project. 
%
%   OPENPRJ(FILENAME) opens the MATLAB Compiler, MATLAB Coder project or
%   MATLAB Project. If FILENAME is not a valid project file then it is
%   opened in the MATLAB Editor. 
%
%   See also DEPLOYTOOL, MCC, MBUILD, CODER, OPENPROJECT

%   Copyright 2006-2018 The MathWorks, Inc.

out=[];

if ~usejava('swing')
	edit(filename);
    return;
end
% Try opening as a Compiler or Coder project:
try
    com.mathworks.project.impl.plugin.PluginManager.allowMatlabThreadUse();
    valid = i_openDeploymentProject(filename, java.io.File(filename));
    if valid
        return
    end
    
catch ME
    if strcmp(ME.identifier, 'Compiler:deploytool:MatlabOnlineSupportWithFile') || ...
            strcmp(ME.identifier, 'MATLAB:apps:errorcheck:MatlabOnlineSupportWithFile') || ...
            strcmp(ME.identifier, 'MATLAB:toolbox_packaging:packaging:MatlabOnlineSupportWithFile')
    
        ME.throwAsCaller;
    end
end

% Try opening as a MATLAB Project if it is available
try
    valid = i_openMATLABProject(filename);
    if valid
        return
    end
catch exception
    if exception.identifier == "MATLAB:project:api:LoadFail"
        error('MATLAB:open:openFailure','%s', exception.message)
    end
end


% We do not have a product installed that uses this .prj file, so treat it
% like a third-party file.
edit(filename);
return;
end


function valid = i_openDeploymentProject(filename, projectFile)
    % Try to open as a Deployment Project:
    if ~projectFile.isAbsolute()
        projectFile = java.io.File(java.io.File(pwd), filename);
    end

    valid = com.mathworks.project.impl.model.ProjectManager.isProject(projectFile);
    if valid
        
        % Don't allow App Packaging, Toolbox Packaging, or Compiler
        % projects to open in MATLAB Online
        productName = connector.internal.getProductNameByClientType;
        if ~isempty(productName) && strcmp(productName,'MATLAB Online')
            targetKey = com.mathworks.project.impl.model.ProjectManager.getTarget(projectFile).getKey;

            switch char(targetKey)
                case {'target.ezdeploy.standalone', 'target.ezdeploy.library', ...
                        'target.webdeploy', 'target.ezdeploy.mps.archive', 'target.ezdeploy.map.reduce'}
                    throw(MException(message('Compiler:deploytool:MatlabOnlineSupportWithFile', filename, productName)));
                case {'target.mlapps'}
                    throw(MException(message('MATLAB:apps:errorcheck:MatlabOnlineSupportWithFile', filename, productName)));
                case {'target.toolbox'}
                    throw(MException(message('MATLAB:toolbox_packaging:packaging:MatlabOnlineSupportWithFile', filename, productName)));
            end
        end
        com.mathworks.project.impl.DeployTool.invoke(projectFile);
    end
end

function valid = i_openMATLABProject(filename)

    % Do not use an import here because MATLAB will fail to parse the entire file
    % when the imported class doesn't exist (i.e. slproject not installed)
    valid =  matlab.internal.project.util.PathUtils.loadProjectForOpenPRJ(filename);

end
