function intelSetupFcn(setupFile, intelVersion, msvcVersion)
% This function is used by the Intel toolchain MATLABSetup (in intel_tc.m)
% to initialize the environment for the Intel compiler.

% The compilervars.bat file provided by Intel relies on some custom
% environment variables (created when the Intel compiler is installed) to
% locate where Visual Studio is installed. These environment variables may
% not be present. Instead determine the VS installation location from mex
% and set the environment variables here.

% Copyright 2017-2023 The MathWorks, Inc.

% Add a command to set an environment variable for the Intel
% compilervars.bat file to find visual studio

supportedVers = {'2017', '2019', '2022'};
verNumbers = {'15.0', '16.0', '17.0'};
mexOptName = ['Microsoft-' verNumbers{ strcmpi(msvcVersion, supportedVers) }];
compInfo = coder.make.internal.getMexCompInfoFromKey(mexOptName);
if isempty(compInfo)
    error(message('MATLAB:mex:NoCompilerFound_Win64'));
else
    vcVarsLocation = compInfo.comp.Details.CommandLineShell;
    % The Intel setup script only needs the VS installation dir
    installationDir = extractBefore(vcVarsLocation, '\VC\');
    vsDirEnvSetupCmd = ['set "VS' msvcVersion 'INSTALLDIR=' installationDir '"'];
end

% g2692513 stores the correct value in CommandLineShell
intelKey = ['INTELC' intelVersion 'MSVS' msvcVersion];
intelCompInfo = coder.make.internal.getMexCompInfoFromKey(intelKey);
if isempty(intelCompInfo)
    error(message('MATLAB:mex:NoCompilerFound_Win64'));
end
intelSetupLocation = intelCompInfo.comp.Details.CommandLineShell;
intelShellSetupCmd = ['call ' intelSetupLocation 'intel64 vs' msvcVersion];

% Create the bat file
contentToWrite = [vsDirEnvSetupCmd newline intelShellSetupCmd];
setupFile = fullfile(pwd, setupFile);
if coder.make.internal.needToUpdateFile(setupFile, contentToWrite)
    fid = fopen(setupFile, 'w+');
    fprintf(fid, '%s', contentToWrite);
    fclose(fid);
end
end
