function fullPath = settingsFilename(rawStr, conflictMode, fileExt, ...
        modelH, showUI, makeDir, opts, dialogTitle)
%

%   Copyright 2006-2015 The MathWorks, Inc.

    if nargin<5
        showUI = false;
    end

    if nargin<6
        makeDir = false;
    end

    if nargin<7
        opts = [];
    end
    
    if nargin<8
        dialogTitle = 'Simulink Design Verifier'; %NO_L10N 
    end

    if ischar(modelH)
         [~, modelName] = fileparts(modelH);
    else
        modelName = get_param(modelH,'Name');
    end
    if isempty(opts)
        try
            testcomp = Sldv.Token.get.getTestComponent;
            settings = testcomp.activeSettings;
        catch myExc  %#ok<NASGU>
            settings = sldvdefaultoptions;
        end
    else
        settings = opts;
    end
    currDir = pwd;


    % When fileExt is '$ModelExt$' the file should have the same extension as
    % the original model (MDL or SLX)
    if strcmp(fileExt,'$ModelExt$')
        if ischar(modelH)
            [~,~,fileExt] = fileparts(modelH);
        else
            modelToCopyfileName = get_param(modelH,'filename'); 
            [~,~,fileExt] = fileparts(modelToCopyfileName);
        end
    end

    % Expand with the model name and correct any separator problems
    rawStr = strrep(rawStr, '$ModelName$', modelName);
    rawStr = strrep(rawStr, '\', filesep);
    rawStr = strrep(rawStr, '/', filesep);

    % Determine the correct path
    [rawPath, rawName, rawExt] = fileparts(rawStr);

    % Determine the needed extension
    if isempty(rawExt)
        actExt = fileExt;
    else
        actExt = rawExt;
        if ~strcmp(rawExt,fileExt)
            % For model extensions we want to force the actual extension
            if (strcmp(fileExt,'.mdl') || strcmp(fileExt,'.slx')) && ...
                    (~strcmp(actExt, '.mdl') && ~strcmp(actExt, '.slx'))
                actExt = fileExt;
            end
        end
    end

    % Determine the path based on the current settings
    if ~isempty(rawPath) && is_abs_path(rawPath)
        actPath = rawPath;
    else
        % Expand with the model name and correct any separator problems
        outDir = strrep(settings.OutputDir, '$ModelName$', modelName);
        outDir = strrep(outDir, '\', filesep);
        outDir = strrep(outDir, '/', filesep);

        if is_abs_path(outDir)
            actPath = fullfile(outDir,rawPath);
        else
            actPath = fullfile(currDir,outDir,rawPath);
        end
    end

    actPath = rmiut.simplifypath(actPath,filesep);

    % Create the output directory if needed
    if ~exist(actPath,'dir') && makeDir
        try
            if ispc
                if strcmp(actPath(1:2),'\\')
                    mkdir(actPath);
                else
                    mkdir(actPath(1:3),actPath(4:end));
                end
            else
                mkdir(actPath(1),actPath(2:end));
            end
        catch Mex %#ok<NASGU>
            errStr = getString(message('Sldv:shared:utils:FailToCreateDirectory', actPath));
            if showUI
                sldvshareprivate('local_error_dlg',errStr,dialogTitle);
                fullPath = '';
                return;
            else
                error(message('Sldv:Settings:OutDirCantCreate', errStr));
            end
        end
    end
    if exist(actPath, 'dir')
        [~, attr] = fileattrib(actPath);
        if (~attr.UserWrite)
            errStr = getString(message('Sldv:shared:utils:DirIsReadOnly',actPath));
            if showUI
                sldvshareprivate('local_error_dlg',errStr,dialogTitle);
                fullPath = '';
                return;
            else
                error(message('Sldv:Settings:OutDirRO', errStr));
            end
        end
    end

    fileName = [rawName actExt];
    fullPath = fullfile(actPath, fileName);


    % See if the name should be incremented to resolve conflicts.
    % A conflict is when the file exists on disk or, in the case of a
    % model, it exists in-memory even though it has been deleted from the
    % disk.
    if exist(fullPath,'file') || ...
            ((strcmp(fileExt,'.mdl') || strcmp(fileExt,'.slx')) && ...
             length(rawName) <= namelengthmax && bdIsLoaded(rawName))
        switch(conflictMode)
            case 'on'
                fullPath = Sldv.utils.uniqueFileNameUsingNumbers(actPath,fileName,actExt);
        end

    end
    fullPath = rmiut.simplifypath(fullPath, filesep);
end
    
function out = is_abs_path(str)
    % Determine if this an absolute path
    if ispc
        out = ~isempty(str) && (any(str==':') || strncmp(str,'\\',2));    
    else
        out = ~isempty(str) && str(1)=='/';            
    end
end
% LocalWords:  RO SLDV dlg simplifypath testcomponent
