function oString = setup_for_visual(args)
%
% Function: setup_for_visual =================================================
% Abstract:
%
%       Configure the build process for Visual C++ for the x64 platform
%
%       When configuring the build process for Visual, this function wraps the
%       raw make command, args.makeCmd (e.g. 'nmake -f model.mk MAT_FILE=1') in
%       a batch (.bat) file that sets up some environment variables needed
%       during the build process.
%-------------------------------------------------------------------------------
%

% Copyright 1994-2020 The MathWorks, Inc.
% 

  if isfield(args, 'EnvVarSuggestions')

      locErrorNoCompiler();
      return;
  end
    
  % first check for DEVSTUDIO_LOC on the make command-------------------
  DSLoc = coder.make.internal.parsestrforvar(args.makeCmd, 'DEVSTUDIO_LOC');
  if ~isempty(DSLoc)
      args.mexOpts =  coder.make.internal.getMexCompilerInfo('location',DSLoc);
  end

  % if the mexopts struct was not set, then it could be because the
  % TemplateMakefile param was explicitly set to a specific TMF.  Since the
  % TMF is targeted for MSVC (that's why this function was called), try
  % getting a struct for an installed Microsoft compiler.
  if isempty(args.mexOpts)
      args.mexOpts = coder.make.internal.getMexCompilerInfo(...
                                'manufacturer','Microsoft');
  end
  
  % if the mexOpts struct is set, and it's in the 'new style' compiler
  % support, then call the new bat file setup.  otherwise default to
  % the old style for now.
  if ~isempty(args.mexOpts)
      [oString, bat_struct] = locWriteBatFile(args);
  else
      locErrorNoCompiler();
      return;
  end
  
  % Set up TMF token for compiler version
  if isempty(findTMFToken(args.buildInfo, '|>VISUAL_VER<|'))
      args.buildInfo.addTMFTokens('|>VISUAL_VER<|', ...
                                  bat_struct.vcverOnes, ...
                                  'COMPILER_VERSION');
  end
  % if the BuildInfo Object InstallDirmexopts is empty, then we need to
  % update it.
  if isempty(args.buildInfo.InstallDirmexopts)
      lInstallDirmexopts = ['"' args.mexOpts.comp.MexOpt '"'];
      args.buildInfo.InstallDirmexopts = lInstallDirmexopts;
  end
  
%endfunction setup_for_visual

%=============================================================================
% Function: locWriteBatFile 
%
% inputs:
%    comp
%
%
% returns:
%
%
%=============================================================================
function [oString, bat_struct] = locWriteBatFile(args)

  bat_struct=[];

  bat_struct.vcverOnes             = args.mexOpts.verStr;
  bat_struct.originalmexOpts       = args.mexOpts.originalMexOpts;
  bat_struct.specified_via_makeCmd = false;

  % some versions, notably the express versions don't always ship with the
  % vcvars bat file.  in that case we can generate a local mexopts into the
  % build folder and use that instead.
  if isempty(args.mexOpts.batFileLoc)
      vcver = lower(args.mexOpts.comp.ShortName);
      batFileName = ['setup_' vcver '.bat'];

       batFileStr = args.mexOpts.comp.Details.SetEnv;
       fid = fopen( batFileName, 'w+' );
       fprintf( fid, '%s', batFileStr );
       fclose( fid );
      vcvars = batFileName;
  else
      vcvars = strtrim(args.mexOpts.comp.Details.CommandLineShell);
      if (isempty(vcvars) == 0 && (vcvars(1) ~= '"' || vcvars(end) ~= '"'))
          vcvars = ['"' vcvars '"'];
      end
      vcvars = [vcvars ' ' args.mexOpts.batFileArgs];

  end

  % Create command file content
  content = coder.make.internal.getVisualCommandFileContent(args, vcvars);
  
  % Write out command file
  cmdFile = ['.\',args.modelName, '.bat'];
  cmdFileFid = fopen(cmdFile,'wt');
  fprintf(cmdFileFid, '%s', content);
  fclose(cmdFileFid);
  oString = cmdFile;

return;
%End of Function locWriteBatFile

%=============================================================================
% Function: locErrorNoCompiler 
%
% inputs:
%
%
% returns:
%    oString
%
%
%=============================================================================
function locErrorNoCompiler()

DAStudio.error('RTW:buildProcess:noSupportedCompiler');

%End of Function locErrorNoCompiler

% LocalWords:  hacky Env nmake mexopts TMF Dirmexopts MAKEFILEBUILDER TGT TMF's
% LocalWords:  vc tmf MAKECMD vcvars msd devstudio pil Sdk vcver
