function [errorCode, messageStr] = mbuild_helper(varargin)
%MBUILD_HELPER is a helper function that contains the code that MBUILD.M
%   (an autogenerated file) executes.  It sets up the inputs to call mex.
%
%   For information on how to use MBUILD see MBUILD help by typing "help
%   mbuild" or "mbuild -h".

%   $Date $
% Copyright 1984-2014 The MathWorks, Inc.

errMsg     = '';

try
    for i=1:numel((varargin))
        if (find(varargin{i} == ' '))
            varargin{i} = [ '"' varargin{i} '"' ];
        end
        
        if( strcmp(varargin{i},'-h') || strcmp(varargin{i},'-help') )
            displayHelp;
            return;
        end
        
        if ( strcmp(computer,'MACI64') && strcmp(varargin{i},'-g') )
            CMDLINE250='CMDLINE250=xcrun -sdk macosx$SDKVER dsymutil "$EXEDIR$EXENAME.app/Contents/MacOS/$EXENAME"';
            varargin={CMDLINE250, varargin{:}};
            break;
        end        
    end
    
    % When mbuild -setup -client mbuild_com is executed, the
    % Compiler for COM components is selected
    varargin={'-largeArrayDims','-client','mbuild',varargin{:}};
    errCode = mex(varargin{:});    
catch ex
    errMsg = ex.message;
    errCode = 1;
end

if (nargout > 0)
    if( nargout == 2)
        messageStr = errMsg;
    end
    errorCode = errCode;
elseif (errCode ~= 0)
    error(message('Compiler:mbuild_helper:genericFailure',errMsg));
end


%%%%%%%%%%%%%%%%%%%%
%%% SUBFUNCTIONS %%%
%%%%%%%%%%%%%%%%%%%%

% Display MBUILD Help
function displayHelp

if ispc
    [errCode, result] = compilerDos(['"' matlabroot '\bin\mbuild.bat' '"' ' -h']);
else
    [errCode, result] = unix(['"' matlabroot '/bin/mbuild' '"' ' -h']);   
end

if(errCode)
   error(message('Compiler:mbuild_helper:unableToDisplayHelp'));
else
   disp(result);
end
