function [b,p] = getBoardProcNum(boardName, procName, varargin)
%GETBOARDPROCNUM Get the board and processor numbers based on the names.

%   Copyright 2006-2011 The MathWorks, Inc.

narginchk(2,3);

% This check covers the case when the IDE or Link for IDE are not installed
try
    if (nargin > 2)
        boardlst = ccsboardinfo(varargin{1});
    else
        boardlst = ccsboardinfo;
    end
catch origErr
    newExc = MException('TICCSEXT:util:CannotAccessCcsSetupInfo',...
        ['Could not obtain board configuration from Code Composer Studio(TM). '...
        'Make sure that Code Composer Studio is installed and properly registered.']);
    lf_throwPjtGenError(newExc, origErr);
end

% This check covers the case when CCS configuration is empty
if isempty(boardlst)
    error(message('TICCSEXT:util:Demo_MismatchInTPBlockAndCcsSetup', boardName));
end

% This check covers the case when CCS configuration is not empty
if exist('qeBypassBoardCheck','file') && qeBypassBoardCheck,
    % This is for QE operation mode only, pick first board and processor.
    board_idx = 1;
    proc_idx = 1;
else
    % This is for normal installation operation mode.
    
    % Initialize indices (-1 means 'not found')
    board_idx = -1;
    proc_idx = -1;
    % Search in the list for the board name saved in the Target Preferences block.
    for k = 1:length(boardlst),
        if strcmpi( boardlst(k).name, boardName ),
            board_idx = k; % board found
            for m = 1:length(boardlst(board_idx).proc),
                if strcmp( boardlst(board_idx).proc(m).name, procName ),
                    proc_idx = m; % proc found
                    break;
                end
            end
            if board_idx~=-1 && proc_idx==-1, % board found, proc not found
                % don't even warn;
                % Locate the first valid processor found in the board
                proc_idx = locateFirstValidProcInBoard(boardlst(board_idx));
            end
            break;
        end
    end
    % Board is not found. Pick a board from the list.
    if board_idx == -1,
        if length(boardlst)==1
            % If there is only one board currently configured in CCS,
            % connect to the one and only board even though the
            % label is different (mini-spec design review 3/26/06)
            disp(['Target Preferences block specifies that the board named ''' boardName ''' '...
                'will be used to run generated code.']);
            disp(['However, since only board named ''' boardlst(1).name ''' is found '...
                'in your system, that board will be used.']);
            % Use first board found in list
            board_idx = 1;
        else
            % If there are multiple boards, ask the user to choose from the
            % list.
            promptstr{1} = 'The board specified in Target Preferences';
            promptstr{2} = 'block is not found in your system.';
            promptstr{3} = 'Select one of the available boards:';
            for i=1:length (boardlst)
                listitems{i} = boardlst(i).name; %#ok<AGROW>
            end
            [selection, action] = listdlg('PromptString', promptstr,...
                'SelectionMode', 'single',...
                'ListString', listitems,...
                'ListSize',[220 160],...
                'Name',linkfoundation.util.getProductName(),...
                'CancelString', 'Abort');
            if action==1 % OK - use board selected by user from the list
                board_idx = selection;
            else % Abort
                DAStudio.error('TICCSEXT:autointerface:EmptyCcsSetup');
            end
        end
        % Locate the first valid processor found in the board
        proc_idx = locateFirstValidProcInBoard(boardlst(board_idx));
    end
end

b = boardlst(board_idx).number;
p = boardlst(board_idx).proc(proc_idx).number;

%% ------------------------------------------------------------------------
function proc_idx = locateFirstValidProcInBoard(thisBoard)

foundValidProc = 0;
for proc_idx=1:numel(thisBoard.proc)
    % Note: non-valid processors have (proc.number == '-')
    if isnumeric(thisBoard.proc(proc_idx).number)
        foundValidProc = 1;
        break;
    end
end
if ~foundValidProc
    error(message('TICCSEXT:util:Demo_BoardContainsNoValidProc', boardlst( 1 ).name));
end
