function tmfResolved = getTMFfromRtwRoot(tmf)
%getTMFfromRtwRoot searches for the TMF in matlabroot/rtw/c

%   Copyright 2019-2020 The MathWorks, Inc.

% By default, just return the input file
tmfResolved = tmf;

% Redirected files are legacy template makefiles that no longer exist and
% are redirected to use the corresponding ert template makefile
redirectedMakefiles = {...
    'raccel_unix.tmf', 'raccel_lcc64.tmf', 'raccel_vcx64.tmf', ...
    'accel_unix.tmf', 'accel_lcc64.tmf', 'accel_vcx64.tmf', ...
    'rsim_unix.tmf', 'rsim_lcc64.tmf', 'rsim_vcx64.tmf', ...
    'rtwsfcn_unix.tmf', 'rtwsfcn_vcx64.tmf' ...
    };

if any(strcmp(tmf, redirectedMakefiles))
    % Found a legacy template makefile that will be redirected (by getTMFProperties) to use the
    % corresponding modelrefsim template makefile instead; at this point it's only required to add a
    % path to the template makefile folder.
    tmfResolved = fullfile(tmffolder, tmf);
    return;
end

[p, f] = fileparts(tmf);
if ~isempty(p) || isempty(f)
    return;
end

file = fullfile(tmffolder, tmf);
if isfile(file)
    tmfResolved = file;
end
