function saveToolchainToMAT()
%saveToolchainToMAT Generate the SerDes Toolbox ToolchainInfo MAT files

% Copyright 2022-2023 The MathWorks, Inc.

destDir = pwd;
fileExt = '.mat';

% Get the list of files to convert
files = dir('ibis_ami_*.m');

% Generate list of files to exclude
exclude = {'rtwTargetInfo.m', [mfilename '.m']};

hostTCNames = [];
for indx = 1:length(files)
    % Skip excluded files
    if any(strcmp(files(indx).name, exclude))
        continue
    end
    
    % Invoke the local ToolchainInfo MATLAB file
    [~, funcname, ~] = fileparts(files(indx).name);
    fh = str2func(funcname);
    disp(['### Running function "' funcname '" ...']);
    tc = fh();

    % Generate .mat files
    for jndx=1:numel(tc)
        % Get target .mat file name
        if contains(tc(jndx).Name , "2017 v15.0")
            baseName = "ibis_ami_ms_vc_nmake_";
            platform = 'win64';
            verName  = "_v15.0";
        elseif contains(tc(jndx).Name , "2019 v16.0")
            baseName = "ibis_ami_ms_vc_nmake_";
            platform = 'win64';
            verName  = "_v16.0";
        elseif contains(tc(jndx).Name , "2022 v17.0")
            baseName = "ibis_ami_ms_vc_nmake_";
            platform = 'win64';
            verName  = "_v17.0";
        elseif contains(tc(jndx).Name , "MinGW64")
            baseName = "ibis_ami_gnu_gcc_gmake_";
            platform = 'win64';
            verName  = '';
        elseif contains(tc(jndx).Name , "g++ cross-compiler |")
            baseName = "ibis_ami_gnu_gcc_gmake_cross_";
            platform = 'win64';
            verName  = '';
        elseif contains(tc(jndx).Name , "g++ cross-compiler source code |")
            baseName = "ibis_ami_gnu_gcc_gmake_cross_src_";
            platform = 'win64';
            verName  = '';
        elseif contains(tc(jndx).Name , "64-bit Linux")
            baseName = "ibis_ami_gnu_gcc_gmake_";
            platform = 'glnxa64';
            verName  = '';
        end
        matName = strjoin([baseName platform verName fileExt], '');
        hostTCNames = [hostTCNames matName]; %#ok<*AGROW>
        tcFile = fullfile(destDir, string(hostTCNames(end)));

        % Delete .mat file if it already exists
        if isequal(exist(tcFile, 'file'),2)
            delete(tcFile);
        end

        % Save .mat file
        disp([' Toolchain: ' tc(jndx).Name]);
        disp([' MAT File:  ' strrep(tcFile, [matlabroot filesep], '')]);
        tcTemp = tc;    % Save a copy of toolchain matrix
        tc = tc(jndx);  % Save requires a single toolchain
        save(tcFile, 'tc');
        tc = tcTemp;    % Restore toolchain matrix

        % Make .mat file read only
        i_make_file_read_only(tcFile);

        disp(' DONE.');
    end
end

% -------------------------------------------------------------------------
function i_make_file_read_only(tcFile)
if ispc
    tcFile = convertStringsToChars(tcFile);
    dos(['attrib +r ' tcFile]);
else
    tcFile = convertStringsToChars(tcFile);
    unix(['chmod -w ' tcFile]);
end

% LocalWords:  Toolchain toolchains tc toolchain attrib
