function iostruct = getioindecies(model,io,inports,outports,flag,truncatename,varargin);
%Obtains the input output index information used in linearization.

%  Author(s): John Glass
%  Copyright 1986-2004 The MathWorks, Inc.
% $Revision: 1.1.8.2 $ $Date: 2004/12/26 22:24:50 $

%% Create a utilities singleton object
util = slcontrol.Utilities;

%% Initialize name cell arrays
i_name = {};o_name = {};
full_i_name = {};full_o_name = {};
full_i_port = [];full_i_ch = [];
full_o_port = [];full_o_ch = [];

%% If the flag is 'block', then get the block name from varargin
if strcmp(flag,'rootports')
    blocks = find_system(model,'SearchDepth',1,'BlockType','Inport');
    for ct = 1:length(blocks)
        ph = get_param(blocks{ct},'PortHandles');
        nch = get_param(ph.Outport,'CompiledPortWidth');
        if truncatename, block = get_param(blocks{ct},'Name');
        else, block = blocks{ct}, end
        i_name = LocalCreateIOString([1:nch],block,1,'signal',i_name);    
        for ct2 = 1:nch
            full_i_name{end+1} = blocks{ct};
            full_i_port(end+1) = 1;
            full_i_ch(end+1) = ct2;
        end        
    end
    
    blocks = find_system(model,'SearchDepth',1,'BlockType','Outport');
    for ct = 1:length(blocks)
        ph = get_param(blocks{ct},'PortHandles');
        nch = get_param(ph.Inport,'CompiledPortWidth');
        if truncatename, block = get_param(blocks{ct},'Name');
        else, block = blocks{ct}, end
        o_name = LocalCreateIOString([1:nch],block,1,'signal',o_name);   
        for ct2 = 1:nch
            full_o_name{end+1} = blocks{ct};
            full_o_port(end+1) = 1;
            full_o_ch(end+1) = ct2;
        end
    end
    i_ind = 1:length(i_name);
    o_ind = 1:length(o_name);
else
    %% Initialize indicies
    i_ind = [];i_name = {};
    o_ind = [];o_name = {};

    %% Get the active IOs
    activeio = io(find(strcmp(get(io,'Active'),'on')));

    %% Loop to get all of the IO strings in list
    for ct1 = 1:length(activeio)
        %% Get the current port information
        port_handles = get_param(activeio(ct1).Block,'PortHandles');
        port = port_handles.Outport(activeio(ct1).PortNumber);
        port_number = sprintf('%d',activeio(ct1).PortNumber);

        if strcmp(flag,'io')
            %% Get the unique name
            util = slcontrol.Utilities;
            if truncatename; 
                block = getUniqueSignalName(util,port);
            else, 
                signame = get_param(port,'Name');
                if isempty(signame)
                    block = get_param(port,'Parent');
                else
                    block = [get_param(port,'Parent'),'/',signame];
                end
            end
            if isempty(get_param(port,'Name'))
                type = 'block';
            else
                type = 'signal';
            end
        else
            port_number = activeio(ct1).Description;
            blockhandle = varargin{1};
            block = getfullname(double(blockhandle.Handle));
            if truncatename
                block = getUniqueBlockName(util,block);
            end 
            type = 'block';
        end

        if strcmp(activeio(ct1).Type,'in')
            %% Find the indicies to the port handles
            ind = find(port==inports);
            i_ind(end+1:end+length(ind)) = ind;
            i_name = LocalCreateIOString(ind,block,port_number,type,i_name);
            %% Get the full block name information
            for ct = 1:length(ind)
                full_i_name{end+1} = activeio(ct1).Block;
                full_i_port(end+1) = str2num(port_number);
                full_i_ch(end+1) = ind(ct);
            end
        elseif strcmp(activeio(ct1).Type,'out')
            %% Find the indicies to the port handles
            ind = find(port==outports);
            o_ind(end+1:end+length(ind)) = ind;
            o_name = LocalCreateIOString(ind,block,port_number,type,o_name);
            %% Get the full block name information
            for ct = 1:length(ind)
                full_o_name{end+1} = activeio(ct1).Block;
                full_o_port(end+1) = str2num(port_number);
                full_o_ch(end+1) = ind(ct);
            end            
        elseif  (strcmp(activeio(ct1).Type,'outin') || strcmp(io(ct1).Type,'inout'))
            %% Handle the inputs
            ind = find(port==inports);
            i_ind(end+1:end+length(ind)) = ind;
            i_name = LocalCreateIOString(ind,block,port_number,type,i_name);
            %% Get the full block name information
            for ct = 1:length(ind)
                full_i_name{end+1} = activeio(ct1).Block;
                full_i_port(end+1) = str2num(port_number);
                full_i_ch(end+1) = ind(ct);
            end
            %% Handle the outputs
            ind = find(port==outports);
            o_ind(end+1:end+length(ind)) = ind;
            o_name = LocalCreateIOString(ind,block,port_number,type,o_name);
            %% Get the full block name information
            for ct = 1:length(ind)
                full_o_name{end+1} = activeio(ct1).Block;
                full_o_port(end+1) = str2num(port_number);
                full_o_ch(end+1) = ind(ct);
            end             
        end
    end
end

%% Create the IO structure.
iostruct = struct('InputInd',{i_ind},'OutputInd',{o_ind},...
    'InputName',{i_name},'OutputName',{o_name},...
    'FullInputName',{full_i_name},'FullOutputName',{full_o_name},...
    'FullInputPort',{full_i_port},'FullOutputPort',{full_o_port},...
    'FullInputChannel',{full_i_ch},'FullOutputChannel',{full_o_ch},...
    'FullStateName',[]);

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LocalCreateIOString
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function str = LocalCreateIOString(ind,block,portnumber,type,str)

switch type
    case 'block'
        if length(ind) > 1
            for ct = 1:length(ind)
                str{end+1} = sprintf('%s (pt. %s, ch %d)',block,portnumber,ct);
            end
        else
            str{end+1} = sprintf('%s (pt. %s)',block,portnumber);
        end
    case 'signal'
        if length(ind) > 1
            for ct = 1:length(ind)
                str{end+1} = sprintf('%s (ch %d)',block,ct);
            end
        else
            str{end+1} = sprintf('%s',block);
        end
end