% MAINFNC is the main function call for all built-in models.
% 
% CALL:
%     varargout = mainFnc(inputs, modelFilename, getModelDescription, getModelDocumentation, getVersion, versionTable)
% 
% A typical call from the built-in model main function will look like:
% 
%   varargout = utils.models.mainFnc(varargin(:), ...
%     mfilename, ...
%     @getModelDescription, ...
%     @getModelDocumentation, ...
%     @getVersion, ...
%     @versionTable);
% 
% INPUTS:
%                 inputs - cell-array of inputs to the built-in model
%          modelFilename - the full filename of the model (typically you use mfilename)
%    getModelDescription - a function handle to the getModelDescription function
%  getModelDocumentation - a function handle to the getModelDocumentation function
%             getVersion - a function handle to the getVersion function
%           versionTable - a function handle to the versionTable function
% 
% VERSION: $Id: mainFnc.m,v 1.3 2011/04/29 14:25:14 hewitson Exp $
% 
% 
function varargout = mainFnc(inputs, modelFilename, getModelDescription, getModelDocumentation, getVersion, versionTable)
  
  % Process inputs
  [info, pl, constructorInfo, fcn] = utils.models.processModelInputs(inputs, ...
    modelFilename, ...
    getModelDescription, ...
    getModelDocumentation, ...
    getVersion, ...
    versionTable);
  
  if ~isempty(info)
    varargout{1} = {info};
    return;
  end
  
  % Build the object
  hpl = copy(pl, 1);
  out = fcn(pl);
  
  % Set the method version string in the minfo object
  if ~isempty(constructorInfo) && utils.helper.isSubclassOf(class(out), 'ltpda_uoh')
    % If this is a user-call via a constructor, then we add history
    out = addHistoryStep(out, constructorInfo, hpl);
  end
  
  if nargout > 0
    varargout{1} = {out, pl};
  else
    error('!!! Invalid number of output')
  end
  
end
