function updateFilePathsAndExtensions(h,varargin)
%UPDATEFILEPATHSANDEXTENSIONS This function updates any file with a missing
% path and/or extension.  By default it assumes the extension is '.c',
% however a cell array of extensions to search for can be passed in.  The
% extensions are tested for in the order they appear in the cell array.

%   Copyright 2005-2023 The MathWorks, Inc.
    
    if nargin > 1
        [varargin{:}] = convertStringsToChars(varargin{:});
    end

    %if all files have extensions and paths, there is nothing to do for this
    %method
    if ~h.checkForUnresolvedFiles()
        return
    end

    % set up the extension list to search.  If none is provided, then default
    % it to '.c'.
    extList = {'.c' '.C' '.cpp' '.CPP' '.s' '.S'};
    incExtList = {'.h' '.H'};

    if nargin > 1
        if iscell(varargin{1})
            extList = varargin{1};
        else
            extList = varargin(1);
        end
    end
    if nargin > 2
        if iscell(varargin{2})
            incExtList = varargin{2};
        else
            incExtList = varargin(2);
        end
    end

    h.Src.updateFilePathsAndExtensions(h.Settings, extList);
    h.Inc.updateFilePathsAndExtensions(h.Settings, incExtList);

    for i=1:length(h.LinkObj)
        h.LinkObj(i).updateFilePathsAndExtensions(extList);
    end

    for i=1:length(h.ModelRefs)
        h.ModelRefs(i).updateFilePathsAndExtensions(extList);
    end

    [missingExt, missingPath] = locUpdateUnresolvedStatus(h);
    h.Settings.HasUnresolvedExtensions = missingExt;
    h.Settings.HasUnresolvedPaths      = missingPath;

end

function [missingExt,missingPath] = locUpdateUnresolvedStatus(h)

    [missingExt,missingPath] = locGetSrcInc(h);
    % if there is at least 1 missing path and 1 missing extension, we can
    % return early, since there is nothing left to check for.
    if missingExt && missingPath
        return;
    end
    
    for i=1:length(h.LinkObj)
        [missingExt,missingPath] = locGetSrcInc(h.LinkObj(i));
        % if there is at least 1 missing path and 1 missing extension, we can
        % return early, since there is nothing left to check for.
        if missingExt && missingPath
            return;
        end
    end
    for i=1:length(h.ModelRefs)
        missingExt = locGetSrcInc(h.ModelRefs(i));
        % if there is at least 1 missing path and 1 missing extension, we can
        % return early, since there is nothing left to check for.
        if missingExt && missingPath
            return;
        end
    end

end

function [missingExt,missingPath] = locGetSrcInc(h)
    missingExt = false;
    missingPath = false;
    srcFiles = get(h.Src.Files,'FileName');
    if ~isempty(srcFiles)
        if ~iscell(srcFiles)
            srcFiles = {srcFiles};
        end
        [paths,~,exts] = cellfun(@fileparts,srcFiles,'UniformOutput',false);
        missingExts = cellfun(@isempty,exts);
        missingExt = any(missingExts);
        missingPaths = cellfun(@isempty,paths);
        missingPath = any(missingPaths);
        % if there is at least 1 missing path and 1 missing extension, we can
        % return early, since there is nothing left to check for.
        if missingExt && missingPath
            return;
        end
    end

    incFiles = get(h.Inc.Files,'FileName');
    if ~isempty(incFiles)
        if ~iscell(incFiles)
            incFiles = {incFiles};
        end
        [paths,~,exts] = cellfun(@fileparts,incFiles,'UniformOutput',false);
        missingExts = cellfun(@isempty,exts);
        missingExt = any(missingExts);
        missingPaths = cellfun(@isempty,paths);
        missingPath = any(missingPaths);
    end
end
