function updated = rpp_update_doc
% RPP_UPDATE_DOC Update built-in S-functions documentation.
%    The documentation is updated based on the YAML comment in .c files.
%    The number of updated blocks is returned.
set_param('rpp_lib', 'Lock', 'off');
blocks = find_system('rpp_lib', 'Type', 'block');
updated = 0;
for i=1:length(blocks),
    params = get_param(blocks{i}, 'ObjectParameters');
    fields = fieldnames(params);
    fn = get_param(blocks{i}, 'FunctionName');
    % We have to unset Matlab's library path in order to run pandoc - it
    % needs newer libc than the one shipped with Matlab
    [rc, new_help] = system(['unset LD_LIBRARY_PATH; scripts/doc_parse.py --html ' fn '.c']);
    if rc ~= 0,
        disp(new_help)
        throw(MException('rpp:pandoc_failure', ['Failed to extract doc from ' fn '.c']))
    end
    old_help = get_param(blocks{i}, 'MaskHelp');
    if ~strcmp(old_help, new_help),
        disp([blocks{i} ': Help not up-to-date - upating'])
        set_param(blocks{i}, 'MaskHelp', new_help)
        updated = updated + 1;
    else
        disp([blocks{i} ': Help up-to-date'])
    end
end
set_param('rpp_lib', 'Lock', 'on');