function out = defaultModelTemplate(in)
% defaultModelTemplate - Set or get the current default model template
%
% Get the full path to the default model template
%
%   t = Simulink.defaultModelTemplate;
%
% Set the current default model template.  This persists between Simulink
% sessions.  It can be specified as a full path or just a template name.
% If Simulink can't find it, an error is thrown.
%
%   Simulink.defaultModelTemplate(my_template);
%

%   Copyright 2015-2023 The MathWorks, Inc.


if nargin
    validateattributes(in,{'char','string'},{'scalartext'},1);
    % Convert the supplied value to a fully qualified template file
    % name.
    [templateFile,info] = sltemplate.internal.findMatchingTemplate(in);
    if ~strcmp(info.Type,'Model')
        DAStudio.error('sltemplate:Registry:NotModelTemplate', templateFile);
    end
    i_save(templateFile);
    sltemplate.ui.StartPage.setDefaultModelTemplate(templateFile);
end

if nargout || ~nargin
    out = Simulink.internal.getDefaultModelTemplate;
end

end

function i_save(filename)
    % Replace matlabroot with a token: $matlabroot
    r = matlabroot;
    if strncmp(filename,r,numel(r))
        filename = ['$matlabroot' filename(numel(r)+1:end)];
    end
    % Store the default model template in the settings.
    s = matlab.settings.internal.settings;
    if ~s.hasGroup('sltemplate')
        s.addGroup('sltemplate');
    end
    s = s.sltemplate;
    if ~s.hasSetting('DefaultModelTemplate')
        s.addSetting('DefaultModelTemplate');
    end
    s.DefaultModelTemplate.PersonalValue = filename;
end

