function bake_mlib( sPrefix, sPath )
% BAKE_MLIB - create library function and helper library
%
% This function has to be called after modifications of struct
% data method modifications.

% This file is part of the HörTech Open Master Hearing Aid (openMHA)
% Copyright © 2011 2013 2017 HörTech gGmbH
%
% openMHA is free software: you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as published by
% the Free Software Foundation, version 3 of the License.
%
% openMHA is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU Affero General Public License, version 3 for more details.
%
% You should have received a copy of the GNU Affero General Public License, 
% version 3 along with openMHA.  If not, see <http://www.gnu.org/licenses/>.

  ;
  if nargin < 2
    sPath = '';
  end
  if (numel(sPath) > 0) && (sPath(end) ~= filesep)
    sPath(end+1) = filesep;
  end
  d = dir([sPath,sPrefix,'_*.m']);
  sFunName = ['lib',sPrefix];
  sStructName = sPrefix;
  fh = fopen([sFunName,'.m'],'w');
  if fh < 0
    error('Unable to create file');
  end
  fprintf(fh,'function sLib = %s()\n',sFunName);
  fprintf(fh,'%% %s - function handle library\n',upper(sFunName));
  fprintf(fh,'%%\n');
  fprintf(fh,'%% Usage:\n');
  fprintf(fh,'%% %s()\n',sFunName);
  fprintf(fh,'%%\n');
  fprintf(fh,'%% Help to function "fun" can be accessed by calling\n');
  fprintf(fh,'%% %s.help.fun()\n',sPrefix);
  fprintf(fh,'%%\n\n');
  
  fprintf(fh,'%% This file was generated by "bake_mlib %s".\n', ...
          sPrefix);
  fprintf(fh,'%% Do not edit! Edit sources %s_*.m instead.\n%%\n', ...
          sPrefix);
  fprintf(fh,'%% Date: %s\n', ...
          datestr(now));
  fprintf(fh,'sLib = struct;\n');
  for k=1:numel(d)
    lname = d(k).name(1:end-2);
    sname = lname((numel(sPrefix)+2):end);
    fprintf(fh,'sLib.%s = @%s;\n',sname,lname);
    fprintf(fh,'sLib.help.%s = @help_%s;\n',sname,sname);
  end
  fprintf(fh,'assignin(''caller'',''%s'',sLib);\n',sPrefix);
  fprintf(fh,'\n\n');
  for k=1:numel(d)
    lname = d(k).name(1:end-2);
    sname = lname((numel(sPrefix)+2):end);
    fhsub = fopen([sPath,d(k).name],'r');
    if fhsub<0
      error('unable to open file');
    end
    sSubFun = fread(fhsub,inf,'uchar');
    fwrite(fh,sSubFun,'uchar');
    fprintf(fh,'\n\n');
    fclose(fhsub);
    sUsage = firstline(char(sSubFun'));
    sUsage = strrep(sUsage,'function ','');
    sUsage = strrep(sUsage,[sPrefix,'_'],[sPrefix,'.']);
    if isempty(find(sUsage=='(')) && isempty(find(sUsage==')'))
      sUsage = [sUsage,'()'];
    end
    sUsage = [char(10),char(10),' Usage:',char(10),'  ',...
              sPrefix,' = ',sFunName,'();',...
              char(10),'  ',sUsage,';',char(10)];
    addpath(sPath);
    try
      sh = help(lname);
    catch
      sh = '';
    end
    rmpath(sPath);
    [shf,sh] = firstline(sh);
    shf = strrep(shf,[upper(sname),' - '],'');
    shf = strrep(shf,upper(sname),'');
    shf = strrep(shf,sname,'');
    idx = find(shf~=' ');
    if ~isempty(idx) && idx(1) > 1
      shf(1:(idx(1)-1)) = '';
    end
    sh = [' ',upper(sname),' - ',shf,sUsage,sh];
    sh = strrep(sh,'''','''''');
    sh = strrep(sh,sprintf('\n'),''',char(10),''');
    fprintf(fh,'function help_%s\n',sname);
    fprintf(fh,'disp([''%s'']);\n',sh);
    fprintf(fh,'\n\n');
  end
  fclose(fh);

function [s,rest] = firstline( s )
  rest = '';
  idx_nl = find(s==10);
  if ~isempty(idx_nl)
    rest = s(idx_nl(1):end);
    s(idx_nl(1):end) = '';
  end
