function COUNT = awe_count_status(SYS, IGNORE_VIRTUAL)
% COUNT = awe_count_status(M)
% Recursively searches a subsystem and counts how many modules have
% each type of status.  The return value COUNT is a 1x4 vector with
% the counts for:
%    [ACTIVE, BYPASSED, MUTED, INACTIVE]
%
% An optional second argument allows you to include or ignore virtual
% modules.
%   COUNT = awe_count_status(M, IGNORE_VIRTUAL)
%
% By default, IGNORE_VIRTUAL = 1 and virtual modules are not included.

% Copyright 2020.  DSP Concepts, Inc.  All Rights Reserved.

if (nargin < 2)
    IGNORE_VIRTUAL = 1;
end

global AWE_INFO;

COUNT = zeros(1, 4);

% This applies only to audio module and subsystem objects.  Check for this.
if (~isa(SYS, 'awe_module') && ~isa(SYS, 'awe_subsystem'))
    error('This function only applies to awe_module objects');
end

% Call the target specific function
if ((SYS.flattenOnBuild==1) && isa(SYS,'awe_subsystem'))
    % Special case handling of subsystems.  Recursively search and make a
    % list of the status of all internal modules
    
    for i=1:length(SYS.module)
        count = awe_count_status(SYS.module{i});
        COUNT = COUNT + count;
    end
elseif (SYS.isVirtual && IGNORE_VIRTUAL)
       % Do nothing
elseif (SYS.isLive == 0)
    % This is for modules which are not running on target
    STR = lower(SYS.mode);
    
    count = strcmp(upper(STR), {'ACTIVE', 'BYPASSED', 'MUTED', 'INACTIVE'});
    COUNT = COUNT + count;
else
    % This is for modules that are running on the target processor
    if (AWE_INFO.displayControl.minimizeTargetReads)
        STR = lower(SYS.mode);
    else
        STR = lower(target_getstatus(SYS));
    end
     count = strcmp(upper(STR), {'ACTIVE', 'BYPASSED', 'MUTED', 'INACTIVE'});
     COUNT = COUNT + count;
end

