% UTP_PREPARE_RESULT prepares a result structure from the inputs
%
% M Hewitson 07-11-07
%
% $Id: utp_prepare_result.m,v 1.13 2011/04/27 09:03:02 hewitson Exp $
%
function result = utp_prepare_result(a, s, stack, call, varargin)

  result.a       = 0;
  result.s       = 0;
  result.fcn     = '?';
  result.num     = '?';
  result.date    = time;
  result.runtime = toc();
  result.msg     = '?';
  result.method  = '?';
  result.class   = '?';
  result.file    = '?';
  
  if numel(stack)>1
    
    % look for utp_run
    idx = -1;
    for kk=1:numel(stack)
      if strcmp(stack(kk).name, 'utp_run')
        idx = kk;
        break;
      end
    end
    
    if idx>0
      call = stack(idx-1).name;
    end
  end
  
  % parse out utp number
  parts = regexp(stack(1).name, '_', 'split');
  parts = regexp(parts{end}, '(\w+)(\(.*\))*', 'tokens');
  utpnum = parts{1}{1};
  
  result.a       = a;
  result.s       = s;
  result.fcn     = sprintf('%s_%s', call, utpnum);
  result.num     = utpnum;
  result.subnum  = parts{1}{2};
  result.msg     = '';
  
  parts = regexp(call, '_', 'split');
  result.method  = parts{end};
  result.class   = parts{end-1};
  result.utp     = strrep(stack(1).name, result.subnum, '');
  if ismember('file', fieldnames(stack(1)))
    result.file    = stack(1).file;
  else
    result.file = '';
  end
  
  if a
    as = 'Pass';
  else
    as = 'Fail';
  end
  if s
    ss = 'Pass';
  else
    ss = 'Fail';
  end

  if numel(varargin) >= 1
    result.msg = varargin{1};
    fprintf('  %-27s  |  %s  |  %s  | %s\n', result.num, ss, as, varargin{1});
  else
    fprintf('  %-27s  |  %s  |  %s\n', result.num, ss, as);
  end
end
