function cmex(P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18)
%CMEX	Compile MEX-function via external C compiler.
%	CMEX('funcname.c') compiles the MEX-function in funcname.c into
%	a MEX-function in a platform-dependent manner.  The Macintosh version
%	can be manually configured to choose the desired compiler, while 
%	UNIX and PC versions call the cmex script and batch files, respectively.							

%	Copyright (c) 1984-95 by The MathWorks, Inc.

if strcmp(computer,'MAC2'),
	% !!! EDIT HERE !!!
	% Uncomment out one of the following lines to specify 
	% which compiler to use:
	% compiler = 'MPW PPCC'; % PowerMac
	% compiler = 'MPW MRC';  % PowerMac
	% compiler = 'METROWERKS C'; % PowerMac
	% compiler = 'MPW C';    % 680x0
	
	%%%% Do not modify any of the lines below
	
	compiler_var_exists = 1;
	eval('compiler;','compiler_var_exists = 0;')
	if ~compiler_var_exists
		error(['You must edit the beginning of the cmex.m ' 13 ...
		       'function to specify which compiler to use.'])
	end
	
	optimization = NaN;
	debugging = 0;
	MCC = 0; % MATLAB Compiler flag
	verbose = 0;
	
	files = [];
	
	% process input parameters
	argnum=1;
	while argnum <= nargin,
	arg=[];
  eval(['arg = P' int2str(argnum) ';'])
	if arg(1) == '-'
		if lower(arg(2)) == 'o' % optimization
		  argnum = argnum+1;
      eval(['arg = P' int2str(argnum) ';'])
		  if strcmp(lower(arg),'off')
			  optimization = 0;
			else
			  optimization = 1;
			end
		elseif lower(arg(2))=='d' % debug
		  argnum = argnum+1;
      eval(['arg = P' int2str(argnum) ';'])
		  if strcmp(lower(arg),'off')
			  debugging = 0;
			else
			  debugging = 1;
			end
		elseif lower(arg(2))=='m' % MCC MEX-function
			MCC = 1;
		elseif lower(arg(2))=='v' % verbose
		  verbose = 1;
	  elseif lower(arg(2))=='s' % silent == opposite of verbose
			verbose = 0;
		else
		  error(['unknown cmex option: ' arg])
		end
	else
		 files = [files ' ''' arg ''''];
	end
	argnum = argnum+1;
	end
	
	if strcmp(compiler,'MPW PPCC') | strcmp(compiler,'MPW MRC') | ...
		 strcmp(compiler,'MPW C'),
	%
	%  Toolserver is shipped with MPW.  It needs to be set up so that
	%  the command "!toolserver" successfully starts up the ToolServer
	%  application.  More information is available in the MATLAB Readme
	%  file, or in the standard MATLAB documentation.
	%
	% Check to make sure Toolserver is set up correctly:
	  tname=tempname;
	  eval(['!toolserver echo "*" > ''' tname ''''])
		f=fopen(tname,'r');
		if (f==-1)
		  error('ToolServer does not appear to be set up correctly.')
		end
		fclose(f);
		delete(tname);
	end
	
	if strcmp(compiler,'MPW PPCC')
		if isnan(optimization)
			optimization = 1; % default for PowerMac is on
		end
		cmex = 'cmex -processor powerpc -CCOMPILER PPCC';
	if MCC
	  root = matlabroot;
		libdir = [ root ':extern:lib:PowerMac:' ];
		files = [files ' ''' libdir 'libmccmx.xcoff' ''''];
		cmex = [cmex ' -MAKEPEFFLAGS "-l ''libmccmx.xcoff=libmccmx#0x01008000-0x00000000''"'];
	end

	if optimization
		cmex = [cmex ' -optimization on '];
	else
		cmex = [cmex ' -optimization off '];
	end

	if debugging & ~MCC
		cmex = [ cmex ' -buildType debugShell ' ];
	elseif debugging & MCC & verbose
		disp('Warning - debugShell option not supported under MATLAB Compiler')
		disp('with the PPCC compiler.  The .xSYM file is already generated by default.')
	end
	
	cmex = [cmex ' ' files];			

	
	if verbose
		disp([ '!' cmex ])
	end	
	eval( [ '!' cmex])
	
	end
	
	if strcmp(compiler,'MPW MRC')
		if isnan(optimization)
			optimization = 1; % default for PowerMac is on
		end
		cmex = 'cmex -processor powerpc -CCOMPILER MrC';
		if MCC
		  root = matlabroot;
			libdir = [ root ':extern:lib:PowerMac:' ];
			files = [files ' ''' libdir 'libmccmx.xcoff' ''''];
			cmex = [cmex ' -MAKEPEFFLAGS "-l ''libmccmx.xcoff=libmccmx#0x01008000-0x00000000''"'];
		end
	
		if optimization
			cmex = [cmex ' -optimization on '];
		else
			cmex = [cmex ' -optimization off '];
		end
	
		if debugging & ~MCC
			cmex = [ cmex ' -buildType debugShell ' ];
		elseif debugging & MCC & verbose
			disp('Warning - debugShell option not supported under MATLAB Compiler')
			disp('with the MrC compiler.  The .xSYM file is already generated by default.')
		end
		
		cmex = [cmex ' ' files];			
	
		if verbose
			disp([ '!' cmex ])
		end	
		eval( [ '!' cmex])
		
	end
	
	if strcmp(compiler,'MPW C')
		if isnan(optimization)
			optimization = 0; % default for 680x0 is off
		end
		cmex = 'cmex -processor 68k';
		if MCC
		  root = matlabroot;
			libdir = [ root ':extern:lib:68k:MPW:' ];
			files = [files ' ''' libdir 'libmccmx.o' ''''];
			cmex = [cmex ' -CFLAGS "-warnings off"'];
		end

		if optimization & verbose
			disp('Warning - optimization not supported on 680x0 platform.');
		end 
	
		if debugging & ~MCC
			cmex = [ cmex ' -buildType debugShell ' ];
		elseif debugging & MCC & verbose
			disp('Warning - debugShell option not supported under MATLAB Compiler')
			disp('with the MPW C compiler.  The .SYM file is already generated by default.')
		end
		
		cmex = [cmex ' ' files];			
	
	
		if verbose
			disp([ '!' cmex ])
		end	
		eval( [ '!' cmex])
		
	end
	
	if strcmp(compiler,'METROWERKS C')
		if isnan(optimization)
			optimization = 1; % default for PowerMac is on
		end
		cmex = 'metromex';
		if MCC
		  root = matlabroot;
			libdir = [ root ':extern:lib:PowerMac:' ];
			files = [files ' ''' libdir 'libmccmx' ''''];
		end

		if optimization
			cmex = [cmex ' -optimization on '];
		else
			cmex = [cmex ' -optimization off '];
		end
% !!! Uncomment the following line to manually set optimization
%     during Metrowerks MEX-file builds !!!

%		cmex(length(cmex)-3:length(cmex)) = 'off ';
	
		if debugging
				cmex = [ cmex ' -debugging on ' ];
		else
				cmex = [ cmex ' -debugging off ' ];
		end
		
		if verbose
			cmex = [ cmex ' -verbose ' ];
		else
			cmex = [ cmex ' -silent ' ];
		end	

		cmex = [cmex ' ' files];			

		if verbose
			disp(cmex);
		end
		
		eval(cmex)		
	end
else % platform is not equal to MAC2
  cmex = '!cmex ';
  for argnum=1:nargin,
	  eval(['arg = P' int2str(argnum) ';'])
	  cmex = [cmex ' ' arg];
	end
	eval(cmex);
end
