function toolPath = getCoverageToolPath(toolClass, options)
%getCoverageToolPath returns install path for the coverage tool class

% Copyright 2020-2021 The MathWorks, Inc.

arguments
    toolClass (1,:) char
    options.SkipValidate (1, 1) logical = false;
end

% Simulink Coverage uses a fixed location
persistent binPath;
if isempty(binPath)
    binPath = fullfile(matlabroot, 'bin', computer('arch'));
end
if toolClass=="SlCov.coder.EmbeddedCoder"
    toolPath = binPath;
    return
end

skipValidate = options.SkipValidate;

toolName = eval([toolClass '.ToolNameForPreferences']);
markerFile = eval([toolClass '.ValidateToolInstallationFile']);

prefGroup = ['CodeCoverage_' toolName];
if ispref(prefGroup, 'Path')
    toolPath = getpref(prefGroup,'Path');
    if ~skipValidate
        isValid = coder.coverage.validateToolPath...
                  (toolPath, markerFile);
        if ~isValid
            fixItMsg = DAStudio.message('CoderCoverage:AllTools:SetToolPathFixIt');
            DAStudio.error('CoderCoverage:AllTools:toolPathGetInvalid',...
                           toolPath, toolName, fixItMsg);
        end
    end
elseif ~skipValidate           
    fixItMsg = DAStudio.message('CoderCoverage:AllTools:SetToolPathFixIt');
    DAStudio.error('CoderCoverage:AllTools:toolPathNotSet',...
                   toolName, fixItMsg);
else
    toolPath = '';
end


