function MplabGetBuildinfo(buildInfo)
% BUILDINFO_DEMO - Demonstrates the Build Info API executed
% during the RTW build process.  This function is invoked as a result of
% set_param(model,'PostCodeGenCommand','get_buildinfo(buildInfo)')

%   Copyright 1994-2006 The MathWorks, Inc.
%   $Revision: 1.2 $


% Get the source and lib files
global mplab_build_info_model;
global mplab_build_info_lib;
global mplab_build_info_libFolders;

src = {};
headers = {};

mplab_build_info_model = '';
mplab_build_info_lib = '';
mplab_build_info_libFolders = '';

% Get all the files in the buildInfo object
%bInfo.allSrcFileList = getFiles(buildInfo, 'all', false, false);

% Get the source files from all groups with path and expanded matlabroot
%bInfo.srcFilesWPathMLRoot = getSourceFiles(buildInfo, true, true, {});

% Get the source files from all groups with path
%src = [src getSourceFiles(buildInfo, true, true)];

% The ert_main.c file is implicitly added as a part of the buildInfo but for
% dsPIC, we donot use this file at all. Hence it shall be eliminated from
% the build process when building from MPLAB using the plug-in.

src_len = length(src);
buildInfo_src = getSourceFiles(buildInfo, true, true);

for idx = 1: length(buildInfo_src)
    % if ert_main.c is found, skip it.
    if isempty( findstr(char(buildInfo_src(idx)), 'ert_main.c'))
        %copy the string into the original string of src
        src_len = src_len + 1;
        src(src_len) = buildInfo_src(idx);
    end
end

% Get the source files from all groups with path
headers = [headers getIncludeFiles(buildInfo, true, true, {})];

% Get the source files from all groups without path
%bInfo.srcFilesWOPath = getSourceFiles(buildInfo, false, false, {});


% Get the source files associated with S-Functions, exclude everything else
%mplab_build_info = [mplab_build_info, ' ', getSourceFiles(buildInfo, true, true, 'Sfcn', {})];

% Get the source files associated with custom code, exclude everything else
%mplab_build_info = [mplab_build_info, ' ', getSourceFiles(buildInfo, true, true, 'CustomCode', {})];

% In order to get access to the rtwmakecfg source file, we have to first
% access the link object buildinfo_rtwmake_lib.
% This provides a handle to the link object buildInfo_rtwmakecfg_lib
lib = findLinkObject(buildInfo, 'buildinfo_rtwmakecfg_lib');

if ~isempty(lib)
    % Get the source files associated with library buildinfo_rtwmakecfg_lib
    %mplab_build_info = [mplab_build_info, ' ', getSourceFiles(lib, true, true)];

    % By default, the path and extension to the source files information
    % are not be included. This can be updated with the following API call
    updateFilePathsAndExtensions(buildInfo, '.c');
    updateFilePathsAndExtensions(buildInfo, '.h');

    % Get the library source files with the paths and extensions
    src = [src getSourceFiles(lib, true, true)];
    % Get the library include files with the paths and extensions
    headers = [headers getSourceFiles(lib, true, true)];

end

for( idx = 1:length(src) )
    %     if(idx == 1)
    %         mplab_build_info = [mplab_build_info '___mplabSourceFiles___'];
    %     end
    mplab_build_info_model = [mplab_build_info_model, char(src{idx}),sprintf('\n')];
end

for( idx = 1:length(headers) )
    %     if(idx == 1)
    %         mplab_build_info = [mplab_build_info ' ___mplabHeaderFiles___'];
    %     end
    mplab_build_info_model = [mplab_build_info_model, char(headers{idx}),sprintf('\n')];
    if( ~isempty(strfind(headers{idx}, 'rtwtypes.h')) )
        mplab_build_info_lib = [mplab_build_info_lib, char(headers{idx}),sprintf('\n')];
    end
end


%add an additional newline for the MPLAB API
mplab_build_info_model = [mplab_build_info_model, sprintf('\n')];
%%{

buildInfo.updateFilePathsAndExtensions
%buildInfo.getViewer
%buildInfo.linkObj

% Intentions below is to obtain all the relevant .C and .H modules into the
% array, except for the rt_logging.c

%Older code
%rtlibSrc = buildInfo.linkObj(1).getSourceFiles(true,true);
%for( idx = 1:length(rtlibSrc) )
%    if( isempty(strfind(rtlibSrc{idx}, 'rt_logging.c')) )
%        mplab_build_info_lib = [mplab_build_info_lib, char(rtlibSrc{idx}),sprintf('\n')];
%    end
%end

%Newer Code compatible with 2008a and 2007b.

lib = findLinkObject(buildInfo, 'rtwlib'); 
if ~isempty(lib)
    rtlibSrc = lib.getSourceFiles(true,true);
    for idx = 1:length(rtlibSrc) 
        if( isempty(strfind(rtlibSrc{idx}, 'rt_logging.c')) )
            mplab_build_info_lib = [mplab_build_info_lib, char(rtlibSrc{idx}),sprintf('\n')];
        end
    end
end

lib = findLinkObject(buildInfo, 'rtwshared'); 
if ~isempty(lib)
    rtlibSrc = lib.getSourceFiles(true,true);
    for idx = 1:length(rtlibSrc) 
        if( isempty(strfind(rtlibSrc{idx}, 'rt_logging.c')) )
            mplab_build_info_lib = [mplab_build_info_lib, char(rtlibSrc{idx}),sprintf('\n')];
        end
    end
end
%a = buildInfo.linkObj(1).getIncludeFiles(true,true)

%Get the path of the Header files only from the Source Directory. 
% Praveen - Strange but this is what the earlier code was doing.

%libHeaderFolders = buildInfo.linkObj(1).getSourcePaths(true);
lib = findLinkObject(buildInfo, 'rtwlib'); 
if ~isempty(lib)
    libHeaderFolders = lib.getSourcePaths(true);
    for( folderIdx = 1:length(libHeaderFolders) )
        libHeaders = dir(fullfile(libHeaderFolders{folderIdx},'*.h'));
        mplab_build_info_libFolders =  [mplab_build_info_libFolders, fullfile(libHeaderFolders{folderIdx},'*.h'),sprintf('\n')];
        mplab_build_info_libFolders =  [mplab_build_info_libFolders, fullfile(libHeaderFolders{folderIdx},'*.c'),sprintf('\n')];
        for( idx = 1:length(libHeaders) )
            mplab_build_info_lib = [mplab_build_info_lib, fullfile(libHeaderFolders{folderIdx},libHeaders(idx).name),sprintf('\n')];
        end
    end
end

lib = findLinkObject(buildInfo, 'rtwshared'); 
if ~isempty(lib)
    libHeaderFolders = lib.getSourcePaths(true);
    for( folderIdx = 1:length(libHeaderFolders) )
        libHeaders = dir(fullfile(libHeaderFolders{folderIdx},'*.h'));
        mplab_build_info_libFolders =  [mplab_build_info_libFolders, fullfile(libHeaderFolders{folderIdx},'*.h'),sprintf('\n')];
        mplab_build_info_libFolders =  [mplab_build_info_libFolders, fullfile(libHeaderFolders{folderIdx},'*.c'),sprintf('\n')];
        for( idx = 1:length(libHeaders) )
            mplab_build_info_lib = [mplab_build_info_lib, fullfile(libHeaderFolders{folderIdx},libHeaders(idx).name),sprintf('\n')];
        end
    end
end
%add an additional newline for the MPLAB API
mplab_build_info_lib = [mplab_build_info_lib, sprintf('\n')];
%%}
