function varargout = blkio(cmd,varargin)
%BLKIO 
%

% Initial coding by A S Bozin, 6-9-97
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2000/06/14 16:09:31 $

   %
   % Action depends on CMD
   %
   switch (cmd)
   case 'init'
      channels = varargin{1};		   	   % Get input arguments
      post = struct('channels',channels,...
		    'input',[],...
		    'output','');
      input = cell(size(channels));
      input(:) = {''};	 	   	   	   % Default values for the scalar input
      post.input = input;
      varargout = {post};
   case 'port'
      channels = varargin{1};		   	   % Get input arguments
      nOutputs = varargin{2};
      prop = varargin{3};
      path = varargin{4};
      input = cell(size(channels));
      input(:) = {''};	 	   	   	   % Default values for the scalar input
      output = '';		 		   % and scalar output
      rect = prop.position;
      if (any(channels > 1) | nOutputs > 1)
	 lrect = rect;
	 rrect = rect;
	 width = rect(3)-rect(1);
	 cmarg = min(width,5);
	 lrect(3) = lrect(1)+cmarg;
	 rrect(1) = rrect(3)-cmarg;
	 if (any(channels > 1) & nOutputs > 1)
	    nmarg = 2;
	 else
	    nmarg = 1;
	 end
	 bmarg = max(0,min((width-10)/nmarg,15));
	 if (any(channels > 1))	   	   	   % Vector input
	    if (strcmp(prop.orientation,'left'))
	       posn = rrect;
	       rect(3) = rect(3)-bmarg;	   	   % Block location correction
	    else
	       posn = lrect;
	       rect(1) = rect(1)+bmarg;
	    end
	    %
	    % Build as many mux blocks as required
	    %
	    yh = round((posn(4)-posn(2))/size(channels,2));
	    y0 = posn(2);
	    for (k = find(channels > 1))
	       posn([2 4]) = y0+[k-1 k]*yh;
	       input{k} = sprintf('\nMux_%d',k);
	       bh = add_block('built-in/Mux',cat(2,path,input{k}),...
			      'Inputs',sprintf('%d',channels(k)),...
			      'ForegroundColor',prop.fgColor,...
			      'BackgroundColor',prop.fgColor,...
			      'Orientation',prop.orientation,...
			      'DropShadow','off',...
			      'ShowName','off',...
			      'Position',posn);
	    end
	 end
	 if (nOutputs > 1)			   % Vector output
	    output = cat(2,char(10),'Demux');
	    if (strcmp(prop.orientation,'left'))
	       posn = lrect;
	       rect(1) = rect(1)+bmarg;
	    else
	       posn = rrect;
	       rect(3) = rect(3)-bmarg;   	   % Block location correction
	    end
	    %
	    % Build a demux block
	    %
	    bh = add_block('built-in/Demux',cat(2,path,output),...
			   'Outputs',sprintf('%d',nOutputs),...
			   'ForegroundColor',prop.fgColor,...
			   'BackgroundColor',prop.fgColor,...
			   'Orientation',prop.orientation,...
			   'DropShadow','off',...
			   'ShowName','off',...
			   'Position',posn);
	 end
      end
      post = struct('channels',channels,...
		    'input',[],...
		    'output',output);
      post.input = input;
      varargout = {post;rect};		   	   % Output arguments
   case 'line'
      channels = varargin{1};		   	   % Get input arguments
      nOutputs = varargin{2};
      post = varargin{3};
      parent = varargin{4};
      name = varargin{5};
      %
      % Connect mux and demux with the block
      %
      for (k = find(channels > 1))
	 add_line(parent,sprintf('%s%s/1',name,post.input{k}),sprintf('%s/%d',name,k));
      end
      if (nOutputs > 1)
	 add_line(parent,cat(2,name,'/1'),sprintf('%s%s/1',name,post.output));
      end
   otherwise
   end
   return					   % Last card of BLKIO
