function setMATLABOnlineDefaultSettings()
%setMATLABOnlineDefaultSettings   Set the default PCT settings for MATLAB Online
%   Unless overrides are provided via environment variables, this function:
%     - Sets the default cluster profile to 'local'.
%     - Disables pool auto creation if enforced by an environment variable.
%     - Sets the default parallel pool size for the 'local' profile if the
%       corresponding override environment variable exists.
%       The default MATLAB Desktop behavior is to make parpool size the
%       same as feature('numcores'). However, this is not the best value to
%       use in the MATLAB Online environment due to limited container
%       resources such as memory.

% Copyright 2017-2021 The MathWorks, Inc.

p = parallel.Settings;

% Obtain overrides from environment variables.  If environment
% variables are not set, getenv returns an empty char array.
localNumWorkersFromEnv = getenv('DEFAULT_PCT_LOCAL_POOL_SIZE');
poolAutoCreateFromEnv = getenv('DEFAULT_PCT_POOL_AUTO_CREATE');

% Override hard-coded values if appropriate
if ~isempty(localNumWorkersFromEnv)
    localNumWorkersFromEnv = round(str2double(localNumWorkersFromEnv));
    if ~isnan(localNumWorkersFromEnv) && localNumWorkersFromEnv > 0
        profile = p.findProfile('Name', 'local');
        profile.getSchedulerComponent().NumWorkers = localNumWorkersFromEnv;
    end
end

if ~isempty(poolAutoCreateFromEnv)
    if regexpi(poolAutoCreateFromEnv, '^(true|1)$') > 0
        p.Pool.AutoCreate = true;
    elseif regexpi(poolAutoCreateFromEnv, '^(false|0)$') > 0
        p.Pool.AutoCreate = false;
    end
end

end
