function CLBConfigurationSourceFileBrowseCallback(hObj, hDlg, ~, ~)
%CLBConfigurationSourceFileBrowseCallback Callback to browse for CLB config source file

% Copyright 2021 The MathWorks, Inc.

if codertarget.target.isCoderTarget(hObj.getConfigSet())
    regexpstr = '[.][.][\/\\]'; %Relative path ../ or ..\
    regexpstr1 = '[.][\/\\]'; %Relative path ./ or .\
    data = codertarget.data.getParameterValue(hObj, 'CLB.SOURCE');
    CLBSourceFileName = data;
    hCS = hObj.getConfigSet();
    modelName = get_param(hCS.getModel, 'Name');
    modelPath = which (modelName);
    [modelPath, ~] = fileparts(modelPath);
    if regexp(CLBSourceFileName, regexpstr, 'once') == 1 %Path relative to model
        CLBSourceFileName = fullfile(modelPath, CLBSourceFileName);
    elseif regexp(CLBSourceFileName, regexpstr1, 'once') == 1
        CLBSourceFileName = fullfile(modelPath, CLBSourceFileName);
    end
    %this will be modified once CCS workspace is present
    curr_dir = pwd;
    [sourcePath, filename] = fileparts(codertarget.utils.replaceTokens(hObj.getConfigSet(), CLBSourceFileName));
    if (7 == exist(sourcePath, 'dir'))
        cd(sourcePath);
    end
    [fname, pname] = uigetfile('*.c', 'Specify CLB Config source file', filename);
    cd(curr_dir);
    if isequal(fname, 0) || isequal(pname, 0) % user pressed cancel
        return
    else
        editBoxTag = 'Tag_ConfigSet_CoderTarget_CLB_SOURCE_Browse';
        fileName = fullfile(pname, fname);
        hDlg.setWidgetValue(editBoxTag, fileName);
        data = fileName;
        codertarget.data.setParameterValue(hObj, 'CLB.SOURCE', data);
    end  
end
end