function mingwSetupFcn(setupFile)
%Detects MinGW and sets the environment variable MINGW_ROOT

%Copyright 2015-2020 The MathWorks, Inc.

    mexCompConfig = mex.getCompilerConfigurations('C','Installed');
    idx = ismember({mexCompConfig(:).Name}, 'MinGW64 Compiler (C)');
    mingGWMex = mexCompConfig(idx);
    
    if isempty(mingGWMex)
        error(message('MATLAB:mex:NoCompilerFound_Win64'));
    end
    
    lPath = coder.make.internal.transformPaths( mingGWMex.Location, 'ignoreErrors', true, 'pathType', 'alternate', 'mapUNCPaths', true );
    % set MINGW_ROOT and add to PATH
    contentToWrite =  [sprintf('set "MINGW_ROOT=%s"', fullfile(lPath, 'bin')) ...
        newline ...
        '@set "PATH=%PATH%;%MINGW_ROOT%"'];
    setupFile = fullfile(pwd, setupFile);
    if coder.make.internal.needToUpdateFile(setupFile, contentToWrite)
        fid = fopen(setupFile, 'w+');
        fprintf(fid, '%s', contentToWrite);
        fclose(fid);
    end
end
