function [stat,post,dsblk] = blkcond(bldopt,path,prop,bstr,bint,bflt,catalog,code,ws,node,sbSample,blk,dsblk)
%BLKCOND Provides the capability of conditionally executing procedure superblocks.
%

% Initial coding by A S Bozin, 3-27-98
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.7 $ $Date: 2000/06/14 16:08:49 $

   stat = 0;					   % Error flag
   nInputs = bint(5);
   nOutputs = bint(6);
   %
   % Get the condition block parameters
   %
   indInt = bint(18)+1;
   indStr = bint(20)+1;
   indInt = bint(indInt)+indInt;		   % Skip icon data
   mode = bint(indInt);				   % Mode
   nblks = bint(indInt+1);			   % Number of procedures
   indInt = indInt+3;
   pRange = 1:nblks;
   pInputs = bint(indInt+pRange);
   indInt = indInt+nblks;
   pOutputs = bint(indInt+pRange);
   %
   % Get a list of procedures
   %
   ind = mfindstr(bstr,indStr,'|');
   procStr = bstr(indStr:ind-1);
   ind = find(catalog(node).bkBlock == blk);
   kids = catalog(node).bkChild{ind};
   procName = cell(1,nblks);
   [procName{:}] = deal(catalog(kids).name);
   bias = 1;
   seqind = zeros(1,nblks);
   for (k = 1:nblks)
     ind = mfindstr(procStr,bias,'; ');
     i = find(strcmpi(procName,procStr(bias:ind-1)));
     seqind(k) = i(1);
     bias = ind+2;
   end
   kids(:) = kids(seqind);			   % Reorder procedures
   procName(:) = procName(seqind);
   %
   % Calculate block position
   %
   hskip = 120;
   vskip = 30;
   bskip = 2*vskip;
   psize = 80;
   if ((mode == 3) | (nblks == 1))
      bbox = zeros(nblks+1,4);
      rect = [0 0 2*hskip+2*psize 2*hskip+psize*nblks+bskip*(nblks-1)];
   else
      bbox = zeros(nblks+2,4);
      rect = [0 0 2*hskip+3*psize 2*hskip+psize*nblks+bskip*(nblks-1)];
   end
   wdth = rect(3);				   % Box width
   if (bldopt{1})
      zoomFactor = int2str(min(5*fix(20*min([600 800]./rect(3:4))),100));
      rect = [80 80 680 880];
   else
      zoomFactor = '100';
      rect = min(rect,[0 0 600 800])+80;
   end
   %
   % Create a new hierarchical element
   %
   bh = add_block('built-in/SubSystem',path,...
		  'ForegroundColor',prop.fgColor,...
		  'BackgroundColor',prop.bgColor,...
		  'Orientation',prop.orientation,...
		  'DropShadow',prop.border,...
		  'Position',prop.position,...
		  'ScreenColor','white',...
		  'Location',rect,...
		  'ShowPortLabels','off');
   %
   % Add input and output ports
   %
   default = num2cell(bbox(:,1));
   ports = struct('sb',struct('input','',...
			      'output',''),...
		  'blk',struct('name','',...
			       'post',default));
   rect = [20 0 40 10];
   input = cell(nInputs,1);
   sampleTime = sprintf('[%g,%g]',sbSample(1),sbSample(2));
   for (k = 1:nInputs)
      rect([2 4]) = rect([2 4])+vskip;
      blkName = sprintf('input-%d',k);
      input{k} = blkName;
      ph = add_block('built-in/Inport',sprintf('%s/%s',path,blkName),...
		     'SampleTime',sampleTime,...
		     'Position',rect,...
		     'FontSize',10);
   end
   rect = [wdth-40 0 wdth-20 10];
   output = cell(nOutputs,1);
   for (k = 1:nOutputs)
      rect([2 4]) = rect([2 4])+vskip;
      blkName = sprintf('output-%d',k);
      output{k} = blkName;
      ph = add_block('built-in/Outport',sprintf('%s/%s',path,blkName),...
		     'Position',rect,...
		     'FontSize',10);
   end
   ports.sb.input = input;
   ports.sb.output = output;
   %
   % Add the condition block logic
   %
   rect = [hskip 40 hskip+psize 40+psize];
   blkName = 'ConditionBlockLogic';
   logpath = cat(2,path,'/',blkName);
   cbprop = prop;
   cbprop.position = rect;
   cbprop.orientation = 'right';
   if (mode == 2)
      bInputs = nblks-1;
   else
      bInputs = nblks;
   end
   bOutputs = nblks;
   bbox(end,:) = rect;				   % Save the block position
   [post,rect] = blkio('port',bInputs,bOutputs,cbprop,logpath);
   ports.blk(end).name = blkName;
   ports.blk(end).post = post;
   strMode = {'No Default';'With Default';'Sequential'};
   logbh = add_block('libsb2sl/SUP/ConditionBlock',logpath,...
		     'ShowName','off',...
		     'ForegroundColor',cbprop.fgColor,...
		     'BackgroundColor',cbprop.bgColor,...
		     'Orientation',cbprop.orientation,...
		     'DropShadow',cbprop.border,...
		     'Position',rect,...
		     'MODE',strMode{mode});
   %
   % Connect the condition block logic with the mux and demux blocks
   %
   blkio('line',bInputs,bOutputs,post,path,blkName);
   %
   % For each procedure block do
   %
   rect = [hskip+psize 40 hskip+2*psize 40+psize];
   partition = catalog(node).partition;
   priority = '';
   for (k = 1:nblks)
      newnode = kids(k);
      rect([2 4]) = rect([2 4])+psize+bskip;
      bbox(k,:) = rect;
      str = procName{k};
      blkName = sprintf('%s\n(%d)',fixstring(catalog(newnode).name),k);
      ports.blk(k).name = blkName;
      newpath = sprintf('%s/%s',path,blkName);
      if (mode == 3)
	 priority = sprintf('%d',k);
      end
      %
      % Create a subsystem
      %
      newbh = add_block('built-in/SubSystem',newpath,...
			'DropShadow',cbprop.border,...
			'ForegroundColor',cbprop.fgColor,...
			'BackgroundColor',cbprop.bgColor,...
			'Orientation',cbprop.orientation,...
			'Position',rect,...
			'ShowPortLabels','off',...
			'Priority',priority);
      sbhndlmsgs('write',sprintf('Translating %s.',str));
      catalog(newnode).caller = node;		   % Set the current parent
      catalog(newnode).partition = partition;	   % Set the partition
      [stat,post,dsblk(:)] = blksuper(bldopt,newbh,newpath,catalog,code,ws,newnode,dsblk);
      ports.blk(k).post = post;
      %
      % Add enable block
      %
      offset = vskip*catalog(newnode).nInputs;	   % Vertical distance between the input ports
      epos = [20 offset+40 40 offset+60];
      eh = add_block('built-in/EnablePort',cat(2,newpath,'/Enable'),...
		     'ShowName','off',...
		     'Position',epos,...
		     'FontSize',10,...
		     'StatesWhenEnabling','held',...
		     'ShowOutputPort','off');
   end
   %
   % Connect the output ports
   %
   borient = zeros(1,2);
   dstPort = 1;
   if ((mode == 3) | (nblks == 1))		   % Sequential mode
      k = 1;				   	   % Output port pointer
      for (blk = 1:nblks)
	 srcBlock = cat(2,path,'/',ports.blk(blk).name);
	 nsgns = pOutputs(blk);
	 for (nsg = 1:nsgns)
	    srcPort = nsg;
	    dstBlock = cat(2,path,'/',ports.sb.output{k});
	    if (bldopt{3})
	       lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort,bbox,borient);
	    else
	       lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort);
	    end
	    k = k+1;				   % Update the input port counter
	 end
      end
   else						   % No or with default mode
      %
      % First add the merge block
      %
      rect = [hskip+2*psize 40 hskip+3*psize 40+psize];
      blkName = 'ConditionBlockOutput';
      mrgpath = cat(2,path,'/',blkName);
      cbprop.position = rect;
      channels = pOutputs;
      bOutputs = pOutputs(1);			   % Must have the same number of outputs
      bbox(end-1,:) = rect;	   		   % Save the block position
      [post,rect] = blkio('port',channels,bOutputs,cbprop,mrgpath);
      ports.blk(end-1).name = blkName;
      ports.blk(end-1).post = post;
      mrgbh = add_block('built-in/Merge',mrgpath,...
			'ShowName','off',...
			'ForegroundColor',cbprop.fgColor,...
			'BackgroundColor',cbprop.bgColor,...
			'Orientation',cbprop.orientation,...
			'DropShadow',cbprop.border,...
			'Position',rect,...
			'Inputs',mat2char(nblks),...
			'InitialOutput','[]');
      %
      % Connect the condition block logic with the mux and demux blocks
      %
      blkio('line',channels,bOutputs,post,path,blkName);
      %
      % Now, the output ports
      %
      srcBlock = cat(2,path,'/',ports.blk(end-1).name,ports.blk(end-1).post.output);
      for (k = 1:bOutputs)
	 srcPort = k;
	 dstBlock = cat(2,path,'/',ports.sb.output{k});
	 if (bldopt{3})
	    lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort,bbox,borient);
	 else
	    lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort);
	 end	 
      end
      %
      % Connect procedure blocks to the merge block
      %
      if (bOutputs == 1)
	 srcPort = 1;
	 dstBlock = cat(2,path,'/',ports.blk(end-1).name,ports.blk(end-1).post.input{1});
	 for (blk = 1:nblks)
	    srcBlock = cat(2,path,'/',ports.blk(blk).name);
	    dstPort = blk;
	    if (bldopt{3})
	       lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort,bbox,borient);
	    else
	       lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort);
	    end
	 end
      else
	 for (blk = 1:nblks)
	    srcBlock = cat(2,path,'/',ports.blk(blk).name);
	    dstBlock = cat(2,path,'/',ports.blk(end-1).name,ports.blk(end-1).post.input{blk});
	    nsgns = pOutputs(blk);
	    for (nsg = 1:nsgns)
	       srcPort = nsg;
	       dstPort = nsg;
	       if (bldopt{3})
		  lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort,bbox,borient);
	       else
		  lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort);
	       end
	    end
	 end
      end
   end
   %
   % Connect the remaining block inputs
   %
   srcPort = 1;
   dstBlock = cat(2,path,'/',ports.blk(end).name,ports.blk(end).post.input{1});
   for (k = 1:bInputs)
      srcBlock = cat(2,path,'/',ports.sb.input{k});
      dstPort = k;
      if (bldopt{3})
	 lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort,bbox,borient);
      else
	 lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort);
      end
   end
   k = bInputs+1;				   % Input port pointer
   for (blk = 1:nblks)
      dstBlock = cat(2,path,'/',ports.blk(blk).name);
      nsgns = pInputs(blk);
      for (nsg = 1:nsgns)
	 srcBlock = cat(2,path,'/',ports.sb.input{k});
	 dstPort = nsg;
	 if (bldopt{3})
	    lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort,bbox,borient);
	 else
	    lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort);
	 end
	 k = k+1;				   % Update the input port counter
      end
   end
   %
   % Finally, connect the condition logic block with the enable blocks
   %
   borient = [0 3];
   srcBlock = cat(2,path,'/',ports.blk(end).name,ports.blk(end).post.output);
   pInputs(:) = pInputs+1;			   % Enable port number
   for (k = 1:nblks)
      srcPort = k;
      dstBlock = cat(2,path,'/',ports.blk(k).name);
      dstPort = pInputs(k);
      if (bldopt{3})
	 lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort,bbox,borient);
      else
	 lh = autoline(path,srcBlock,srcPort,dstBlock,dstPort);
      end
   end
   post = blkio('init',ones(1,nInputs));
   %
   % Finally, set the zooming factor
   %
   if (bldopt{1})
      set_param(path,'ZoomFactor',zoomFactor);
   end
   return					   % Last card of BLKCOND
