function sldvsubsys(method, varargin)
%sldvsubsys - Utility function to manage the verification subsystem
%

%   Copyright 2007-2011 The MathWorks, Inc.

    if nargin<1
        method = 'maskInit';
    end

    if nargin<2
        blockH = gcbh;
    else
        blockH = varargin{1};
    end

    switch lower(method)
      case 'check'
        consistency_check(blockH);

      case 'copy'
        refBlk = get_param(blockH, 'ReferenceBlock');
        if ~isempty(refBlk)
            libName = bdroot(refBlk);
            if (strcmpi(libName,'sldvlib'))
                set_param(blockH,'LinkStatus','none');

                % Remove the default description that is
                % used by the Simulink browser
                set_param(blockH,'Description','');
            end
        end

      case 'maskinit'
        modelH = bdroot(blockH);
        xlateMode = get_param(modelH,'RTWExternMdlXlate');

        if xlateMode == 1
            set_param(blockH,'SimViewingDevice','off');
        else
            set_param(blockH,'SimViewingDevice','on');
        end


      otherwise
        error(message('Sldv:SldvSubSys:UnknownMethod'));
    end


function consistency_check(blockH)
% Block should be an atomic subsystem
    atomic = get_param(blockH,'TreatAsAtomicUnit');
    if ~strcmp(atomic,'on')
        err = getString(message('Sldv:shared:top:SubsysEnTreatAtomiUnit'));
        config_error(blockH, err);
    end

    % MaskType should be 'VerificationSubsystem'
    maskType = get_param(blockH,'MaskType');
    if ~strcmp(maskType,'VerificationSubsystem')
        err = getString(message('Sldv:shared:top:SubsysSetMaskVerifSubsys'));
        config_error(blockH, err);
    end

    % Should not have output ports
    portHandles = get_param(blockH,'PortHandles');
    if ~isempty(portHandles.Outport)
        err = getString(message('Sldv:shared:top:SubsysRmAllOutPorts'));
        config_error(blockH, err);
    end

function config_error(blockH, msg)
    title     = getString(message('Sldv:shared:top:SubsysConfigError'));
    blockName = get_param(blockH,'Name');
    details   =  getString(message('Sldv:shared:top:SubsysErrDetails', blockName, msg));      

    set_param(blockH,'HiliteAncestors','error');
    h = warndlg(details,title);

    ePath = escape_path(blockH);
    close_cb = sprintf('set_param(sprintf(''%s''),''HiliteAncestors'',''none'');', ePath);
    set(h,'DeleteFcn',close_cb);

function out = escape_path(blockH)
    out = getfullname(blockH);
    out = strrep(out, '\','\\');
    out = strrep(out, sprintf('\n'),'\n');
    out = strrep(out, sprintf('\t'),'\t');
