function createDeploymentScript(prjFile, outputFile)
%CREATEDEPLOYMENTSCRIPT  Create a deployment script from a MATLAB Compiler
%PRJ file
%
%    CREATEDEPLOYMENTSCRIPT(PRJFILE,OUTPUTFILE) creates a deployment script
%        file by obtaining the information from a MATLAB Compiler project file
%        PRJFILE and generating a MATLAB script file at the location OUTPUTFILE.
%
%INPUT ARGUMENTS:
%
%    - PRJFILE (A row char vector or a string scalar)
%        Path to a MATLAB Compiler or MATLAB Compiler SDK PRJ file.
%
%    - OUTPUTFILE (A row char vector or a string scalar)
%        Path to the file to write the deployment script. The file must
%        not exist and must include either the extension ".m" or no extension.
%        If no extension is provided, ".m" is appended. If a full path is not
%        specified, OUTPUTFILE is saved in the current folder. You must
%        have permission to write to the file.
%
%    EXAMPLES:
%
%    % Create a deployment script named "deployMyMagic.m" from "mymagic.prj"
%    createDeploymentScript("mymagic.prj", "deployMyMagic.m");

% Copyright 2022 The MathWorks, Inc.

arguments
    prjFile (1,:) char {mustBeTextScalar}
    outputFile (1,:) char {mustBeTextScalar}
end

    % Verify Compiler license
    if ~license('checkout','Compiler')
        error(message('Compiler:deploytool:license_checkout_error'))
    end
    
    narginchk(2, 2);
    
    if ~exist(prjFile, 'file')
      error(message('Compiler:build:compatibility:projectDoesNotExist', prjFile))
    end
    
    [~, fileName, ext] =  fileparts(outputFile);

    % Make sure the output file has a clean name
    if ~isvarname(fileName)
        error(message('Compiler:build:compatibility:invalidOutputFileName', fileName))
    end

    if strlength(ext) == 0
        % Append .m
        outputFile = strcat(outputFile,'.m');
    elseif ~strcmpi(ext, '.m')
        % Incompatible extension
        error(message('Compiler:build:compatibility:incompatibleScriptExtension', outputFile))
    end

    outputFile = compiler.internal.validate.makePathAbsolute(outputFile);

    if exist(outputFile, 'file')
      error(message('Compiler:build:compatibility:scriptFileCannotExist', outputFile))
    end
    
    try
        prjStruct = compiler.internal.readPRJStruct(prjFile);
    
        % compiler.build.* does not support extra MCC options
        if prjStruct.param_user_defined_mcr_options.strlength > 0 && ...
                ~strcmp(prjStruct.param_user_defined_mcr_options,'-C')
            warning(message('Compiler:build:compatibility:mccFlagsNotSupported'))
        end
    
    catch E
        % Errors at this stage indicate the input PRJ was not a Compiler one
        if startsWith(E.identifier,'Compiler:build:')
            throw(E)
        else
            error(message('Compiler:build:compatibility:notCompatibilityPRJ', prjFile))
        end
    end
    
    % Verify the most common invalid Project scenarios
    if isfield(prjStruct, "fileset_main") && ~isstruct(prjStruct.fileset_main)
        error(message('Compiler:build:compatibility:noMainFileSelected'))
    end
    if isfield(prjStruct, "fileset_web_main") && ~isstruct(prjStruct.fileset_web_main)
        error(message('Compiler:build:compatibility:noMainFileSelected'))
    end
    if isfield(prjStruct, "fileset_exports") && ~isstruct(prjStruct.fileset_exports)
        error(message('Compiler:build:compatibility:noExportedFunctionsSelected'))
    end
    
    buildTarget = compiler.internal.DeploymentTarget.getDeploymentTargetFromLegacy(prjFile);
    packageTarget = compiler.internal.PackageType.getPackageTypeFromLegacy(prjFile);
    
    % Choose the correct build and package target
    fullPathPRJ = compiler.internal.validate.makePathAbsolute(prjFile);
    builder = compiler.internal.deployScriptGenerator.DeployScriptBuilder(fullPathPRJ, fileparts(fullPathPRJ));
    builder.buildTarget = buildTarget;
    builder.packageTarget = packageTarget;
    
    % Write out the build script to the output file
    buildScript = builder.generateDeploymentScript;
    
    fid = fopen(outputFile, 'w');
    if fid == -1
        error(message('Compiler:build:compatibility:failedToWriteScript', outputFile)) % Handle write error - probably permissions
    end

    c = onCleanup(@()fclose(fid));
    fprintf(fid, '%s',buildScript);
    clear c

end
