function [stat,post] = blklimint(path,prop,bstr,bint,bflt,parent,name,catalog,ws,node,sbSample)
%BLKLIMINT This block is an integrator with upper and lower limits on the state value.
%
% See also BLKINTEG.

% Initial coding by A S Bozin, 6-30-97
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.8 $ $Date: 2000/08/30 15:02:19 $

   stat = 0;			   		   % Error flag
   nInputs = bint(5);
   nOutputs = bint(6);
   nStates = bint(7);
   %
   % Add mux and demux blocks if necessary
   %
   [post,rect] = blkio('port',nInputs,nOutputs,prop,path);
   %
   % Get the upper limit, lower limit, gain and initial conditions
   %
   indStr = bint(17)+1;
   indInt = bint(15)+1;
   symInd = zeros(1,4);
   str = cell(5,1);
   mode = bint(indInt);
   if (bitand(mode,8))
      ind = mfindstr(bstr,indStr,'|');
      symInd(4) = 1;
      str{4} = bstr(indStr:ind-1);
      indStr = ind+1;
   end
   indInt = indInt+1;
   nCoeffs = bint(indInt);
   indInt = indInt+2*nCoeffs+1;
   if (bitand(mode,16))
      iRange = 1:bint(indInt);
      kRange = bint(indInt+iRange);
      symInd(kRange) = 1;
      for (k = kRange)
	 ind = mfindstr(bstr,indStr,'|');
	 str{k} = bstr(indStr:ind-1);
	 indStr = ind+1;
      end
   end
   %
   % Get the block parameter variables
   %
   [symInd,str] = mgetscope(symInd,str,catalog,ws,node);
   if (symInd(4) == 0)
      x0 = zeros(nStates,1);
      if (nCoeffs ~= 0)
	 indInt = bint(15)+2+nCoeffs;
	 cRange = 1:nCoeffs;
	 indFlt = bint(16);
	 x0(bint(indInt+cRange)) = bflt(indFlt+cRange);
      end
      str{4} = mat2char(x0);
   end
   indFlt = bint(19);
   pLen = zeros(3,1)+nStates;
   ub = cumsum(pLen);
   lb = [0;ub(1:end-1)]+1;
   for (k = find(~symInd(1:3)))
      params = zeros(1,pLen(k));
      bRange = lb(k):ub(k);
      params(:) = bflt(indFlt+bRange);
      str{k} = mat2char(params);
   end
   %
   % Block optimization
   %
   copt = 0;
   if (prop.optimize)
      unitGain = all(evalin('base',str{3}) == 1);  % No need for catch
      if (unitGain)
	 copt = 1;				   % Built-in integrator block
      end
   end
   %
   % Limited integrator block (can be continuous or discrete)
   %
   if (any(sbSample))
      indInt = bint(18)+1;
      indInt = bint(indInt)+indInt;		   % Skip icon data
      imode = bint(indInt);
      method = {'ForwardEuler';'BackwardEuler';'Trapezoidal'};
      str{5} = method{imode};
   end
   switch (copt)
   case 0
      if (any(sbSample))
	 bh = add_block('libsb2sl/DYN/DLimInt',path,...
			'ForegroundColor',prop.fgColor,...
			'BackgroundColor',prop.bgColor,...
			'Orientation',prop.orientation,...
			'DropShadow',prop.border,...
			'Position',rect,...
			'IMODE',str{5},...
			'UB',str{1},...
			'LB',str{2},...
			'K',str{3},...
			'X0',str{4},...
			'TS',mat2char(sbSample));
      else
	 bh = add_block('libsb2sl/DYN/CLimInt',path,...
			'ForegroundColor',prop.fgColor,...
			'BackgroundColor',prop.bgColor,...
			'Orientation',prop.orientation,...
			'DropShadow',prop.border,...
			'Position',rect,...
			'UB',str{1},...
			'LB',str{2},...
			'K',str{3},...
			'X0',str{4});
      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{5},...
			'LimitOutput','on',...
			'UpperSaturationLimit',str{1},...
			'LowerSaturationLimit',str{2},...
			'InitialCondition',str{4},...
			'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,...
			'LimitOutput','on',...
			'UpperSaturationLimit',str{1},...
			'LowerSaturationLimit',str{2},...
			'InitialCondition',str{4});
      end
   otherwise
   end
   %
   % Connect the current limited integrator block with the mux and demux blocks
   %
   blkio('line',nInputs,nOutputs,post,parent,name);
   return					   % Last card of BLKLIMINT
