function upgradeMATLABCoderProject(projectFile, varargin)
%UPGRADEMATLABCODERPROJECT Will convert the given MATLAB Coder project to use the toolchain approach instead of template makefile.

% CODER.MAKE.UPGRADEMATLABCODERPROJECT(PRJ) attempts to upgrade PRJ's
% configuration to use the Toolchain approach by updating certain settings. 
%
% PRJ's configuration can fall under any of the following cases:
%   -------------------  --------------------------------------------------
%   Config Settings      Upgrade Action
%   -------------------  --------------------------------------------------
%   Toolchain-compliant  No CFG updates are necessary. The configuration
%                        is already using the toolchain approach.
%   -------------------  --------------------------------------------------
%   Upgradable           Automatic updates can be done. The configuration
%                        can be upgraded to use the toolchain approach.
%   -------------------  --------------------------------------------------
%   Non-upgradable       Automatic updates cannot be done. The configuration
%                        cannot be upgraded and will continue to use the
%                        template makefile approach. 
%   -------------------  --------------------------------------------------
%
% CODER.MAKE.UPGRADEMATLABCODERPROJECT(CFG, 'force', true) is the
% same as above except all cases are deemed upgradable. This should be done
% with caution as manual updates to the build process are required in order
% for the upgrade to be complete. 

% Copyright 2012-2018 The MathWorks, Inc.

projectFile = convertStringsToChars(projectFile);
validateattributes(projectFile ,{'char'}, {'nonempty'});
try
    % First make the current working directory of Java and MATLAB sync
    if ~isequal(char(java.lang.System.getProperty('user.dir')), pwd)
        java.lang.System.setProperty('user.dir', pwd);
    end
    if isunix() && (projectFile(1) == '~')
        projectFile = fullfile(char(java.lang.System.getProperty('user.homedir')), projectFile(2:end));
    end
    jf = java.io.File(projectFile);
    if ~jf.isAbsolute()
        projectFile = char(jf.getAbsolutePath);
    end
    [config,project] = loadProjectFile(projectFile);
    if ~isa(config, 'coder.CodeConfig')
        error(message('Coder:common:ProjectFileInvalid', projectFile));
    end
    newconfig = coder.make.upgradeCoderConfigObject(config, varargin{:});
    emlcprivate('copyConfigObjectToProject',newconfig, project);
    com.mathworks.project.impl.model.ProjectManager.forceSave(project);
    open(projectFile);
catch ex
    newex = MException(message('Coder:common:CouldNotConvertProject', projectFile));
    newex = newex.addCause(ex);
    throw(newex);
end
end

function [config, javaProject] = loadProjectFile(projectFile)
[CC.options.ProjectRoot, ~, ~] = fileparts(projectFile);

com.mathworks.project.impl.plugin.PluginManager.allowMatlabThreadUse();

try
    javaProject = com.mathworks.project.impl.model.ProjectManager.load( ...
        java.io.File(projectFile), false, true);
catch me
    if ~exist(projectFile, 'file')
        error(message('Coder:common:ProjectFileNotFound', projectFile));
    else
        error(message('Coder:common:ProjectFileInvalid', projectFile));
    end
end

javaConfig = javaProject.getConfiguration();
artifact = char(javaConfig.getParamAsString('param.artifact'));
shortArtifact = artifact(24:end);
if strcmp(shortArtifact, 'mex.instrumented')
    shortArtifact = 'mex';
end

config = coder.config(shortArtifact);
emlcprivate('copyProjectToConfigObject', javaProject, config);
end

% LocalWords:  Toolchain toolchain
