function [stat,post] = blkinteg(path,prop,bstr,bint,bflt,parent,name,catalog,ws,node,sbSample)
%BLKINTEG This block integrates an input signal. The integrator may optionally be resettable.
%
% See also BLKSS, BLKTF, BLKZP, BLKDF.

% Initial coding by A S Bozin, 6-30-97
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.8 $ $Date: 2000/06/30 14:59:59 $

   stat = 0;			   		   % Error flag
   nInputs = bint(5);
   nOutputs = bint(6);
   nStates = bint(7);
   %
   % Get the common block parameters
   %
   symInd = zeros(2,1);				   % Block parameter flags
   str = cell(6,1);
   intOrder = nStates/nOutputs;			   % Order of integration
   str{3} = mat2char(intOrder);
   indStr = bint(17)+1;
   indInt = bint(15)+1;
   indFlt = bint(16);
   mode = bint(indInt);
   indInt = indInt+1;
   nInitX = bint(indInt);
   if (bitand(mode,8))
      ind = mfindstr(bstr,indStr,'|');
      symInd(2) = 1;
      str{2} = bstr(indStr:ind-1);
      indStr = ind+1;
   end
   if (bitand(mode,16))
      ind = mfindstr(bstr,indStr,'|');
      symInd(1) = 1;
      str{1} = bstr(indStr:ind-1);
   end
   %
   % Get block parameter variables
   %
   [symInd,str] = mgetscope(symInd,str,catalog,ws,node);
   if (symInd(2) == 0)
      indInt = indInt+nInitX;
      x0 = zeros(nStates,1);
      iRange = 1:nInitX;
      x0(bint(indInt+iRange)) = bflt(indFlt+iRange);
      str{2} = mat2char(x0);
   end
   indFlt = indFlt+nInitX;
   if (symInd(1) == 0)
      gRange = 1:nOutputs;
      str{1} = mat2char(bflt(indFlt+gRange));
   end
   %
   % Block optimization
   %
   copt = 0;
   if (prop.optimize)
      unitGain = all(evalin('base',str{1}) == 1);  % No need for catch
      if (intOrder == 1)
	 if (unitGain)
	    copt = 1;				   % Built-in integrator block
	 else
	    copt = 2;				   % First order integrator with gain
	 end
      elseif (unitGain)
	 copt = 3;				   % Nth order integrator with unit gain
      end
   end
   %
   % Nth order integrator
   %
   indInt = bint(18)+1;
   indInt = bint(indInt)+indInt;		   % Skip icon data
   mode = bint(indInt);
   if (any(sbSample))
	   imode = bint(indInt+1);
	   method = {'ForwardEuler';'BackwardEuler';'Trapezoidal'};
	   str{6} = method{imode};
   end
   if (mode == 1)
      %
      % Add mux and demux blocks if necessary
      %
      channels = nInputs;
      [post,rect] = blkio('port',channels,nOutputs,prop,path);
      %
      % Integrator block (can be continuous or discrete)
      %
      switch (copt)
      case 0
	 if (any(sbSample))
	    bh = add_block('libsb2sl/DYN/DIntegrator/NthOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IMODE',str{6},...
			   'N',str{3},...
			   'K',str{1},...
			   'X0',str{2},...
			   'TS',mat2char(sbSample));
	 else
	    bh = add_block('libsb2sl/DYN/CIntegrator/NthOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'N',str{3},...
			   'K',str{1},...
			   'X0',str{2});
	 end
      case 1
	 if (any(sbSample))
	    bh = add_block('built-in/DiscreteIntegrator',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IntegratorMethod',str{6},...
			   'InitialCondition',str{2},...
			   'SampleTime',mat2char(sbSample));
	 else
	    bh = add_block('built-in/Integrator',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'InitialCondition',str{2});
	 end
      case 2
	 if (any(sbSample))
	    bh = add_block('libsb2sl/DYN/DIntegrator/1stOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IMODE',str{6},...
			   'K',str{1},...
			   'X0',str{2},...
			   'TS',mat2char(sbSample));
	 else
	    bh = add_block('libsb2sl/DYN/CIntegrator/1stOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'K',str{1},...
			   'X0',str{2});
	 end
      case 3
	 if (any(sbSample))
	    bh = add_block('libsb2sl/DYN/DIntegrator/NthOrder1',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IMODE',str{6},...
			   'N',str{3},...
			   'X0',str{2},...
			   'TS',mat2char(sbSample));
	 else
	    bh = add_block('libsb2sl/DYN/CIntegrator/NthOrder1',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'N',str{3},...
			   'X0',str{2});
	 end
      otherwise
      end
   else
      %
      % Add mux and demux blocks if necessary
      %
      channels = [nOutputs nInputs-nOutputs-1 1];
      [post,rect] = blkio('port',channels,nOutputs,prop,path);
      %
      % Get additional parameters
      %
      if (mode == 2)
	 str{4} = 'Single edge';
      else
	 str{4} = 'Double edge';
      end
      str{5} = mat2char((nInputs-1)/nOutputs-1);
      %
      % Resettable integrator block (can be continuous or discrete)
      %
      switch (copt)
      case 0
	 if (any(sbSample))
	    bh = add_block('libsb2sl/DYN/DResetIntegrator/NthOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IMODE',str{6},...
			   'N',str{3},...
			   'K',str{1},...
			   'X0',str{2},...
			   'RMODE',str{4},...
			   'NR',str{5},...
			   'TS',mat2char(sbSample));
	 else
	    bh = add_block('libsb2sl/DYN/CResetIntegrator/NthOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'N',str{3},...
			   'K',str{1},...
			   'X0',str{2},...
			   'RMODE',str{4},...
			   'NR',str{5});
	 end
      case 1
	 if (any(sbSample))
	    bh = add_block('libsb2sl/DYN/DResetIntegrator/1stOrder1',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IMODE',str{6},...
			   'X0',str{2},...
			   'RMODE',str{4},...
			   'TS',mat2char(sbSample));
	 else
	    bh = add_block('libsb2sl/DYN/CResetIntegrator/1stOrder1',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'X0',str{2},...
			   'RMODE',str{4});
	 end
      case 2
	 if (any(sbSample))
	    bh = add_block('libsb2sl/DYN/DResetIntegrator/1stOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IMODE',str{6},...
			   'K',str{1},...
			   'X0',str{2},...
			   'RMODE',str{4},...
			   'TS',mat2char(sbSample));
	 else
	    bh = add_block('libsb2sl/DYN/CResetIntegrator/1stOrder',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'K',str{1},...
			   'X0',str{2},...
			   'RMODE',str{4});
	 end
      case 3
	 if (any(sbSample))
	    bh = add_block('libsb2sl/DYN/DResetIntegrator/NthOrder1',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'IMODE',str{6},...
			   'N',str{3},...
			   'X0',str{2},...
			   'RMODE',str{4},...
			   'NR',str{5},...
			   'TS',mat2char(sbSample));
	 else
	    bh = add_block('libsb2sl/DYN/CResetIntegrator/NthOrder1',path,...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.bgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow',prop.border,...
			   'Position',rect,...
			   'N',str{3},...
			   'X0',str{2},...
			   'RMODE',str{4},...
			   'NR',str{5});
	 end
      otherwise
      end
   end
   %
   % Connect the current n-th order integrator block with the mux and demux blocks
   %
   blkio('line',channels,nOutputs,post,parent,name);
   return					   % Last card of BLKINTEG
