$traceMode = false;
$winConsole = true;

trace($traceMode);//leave this as is,change $traceMode above to true/false instead.
EnableWinConsole($winConsole); // leave alone, change $winConsole instead.
// send logging output to a windows console window
//------------------------------------------------------------------------
//  ./main.cs   v0.3
//
//  root main module for 3D2E emaga4 sample game
//
//  Copyright (c) 2003, 2006 by Kenneth C.  Finney.
//------------------------------------------------------------------------

// ========================================================================
// ========================= Initializations ==============================
// ========================================================================

$usageFlag =  false;  //help won't be displayed unless the command line
                      //switch ( -h ) is used

$logModeEnabled =  true; //track the logging state we set in the next line.
SetLogMode(2);   // overwrites existing logfile & closes log file at exit.

// ========================================================================
// ======================= Function Definitions ===========================
// ========================================================================

function OnExit()
//------------------------------------------------------------------------
// This is called from the common code modules. Any last gasp exit
// acticities we might want to perform can be put in this function.
// We need to provide a stub to prevent warnings in the log file.
//------------------------------------------------------------------------
{
}

function OnStart()
//------------------------------------------------------------------------
// This is called from the common code modules. Any last initial
// acticvities we might want to perform can be put in this function.
// We need to provide a stub to prevent warnings in the log file.
//------------------------------------------------------------------------
{
}

function ParseArgs()
//------------------------------------------------------------------------
//  handle the command line arguments
//
//  this function is called from the common code
// NOTE: some variables used in the function are globals (use the $)
// because we need their values that are set inside this function to 
// also be accessible outside this function as well.
//------------------------------------------------------------------------
{
  for(%i = 1; %i  < $Game::argc ; %i++) //loop thru all command line args
  {
    %currentarg    = $Game::argv[%i];   // get current arg from the list
    %nextArgument       = $Game::argv[%i+1]; // get arg after the current one
    %nextArgExists = $Game::argc-%i > 1;// if there *is* a next arg,note that
    $logModeEnabled =  false;           // turn this off-let the args dictate
                                // if logging should be enabled.

    switch$(%currentarg)
    {
      case "-?":       // the user wants command line help, so this cause the
        $usageFlag = true;   // Usage function to be run, instead of the game
        $argumentFlag[%i] = true;                // adjust the argument count

      case "-h":         // exactly the same as "-?"
        $usageFlag = true;
        $argumentFlag[%i] = true;
    }
  }
}

function Usage()
//------------------------------------------------------------------------
// Display the command line usage help
//------------------------------------------------------------------------
{
//  NOTE: any logging entries are written to the file 'console.log'
  Echo("\n\nemaga4 command line options:\n\n" @
         " -h, -?              display this message\n" );
}

function  LoadAddOns(%list)
//------------------------------------------------------------------------
// Exec each of the startup scripts for add-ons.
//------------------------------------------------------------------------
{
  if (%list $= "")
    return;
  %list = NextToken(%list, token, ";");
  LoadAddOns(%list);
  Exec(%token @  "/main.cs");
}

// ========================================================================
// ================ Module Body - Inline Statements =======================
// ========================================================================
//  Parse the command line  arguments
ParseArgs();

//  Either  display the help message or start the program.
if  ($usageFlag)
{
  %saveWinConsole = $winConsole;
  EnableWinConsole(false);// send logging output to a windows console window
  Usage();
  EnableWinConsole(%saveWinConsole);//restore windows console mode
  Quit();
}
else
{

  //  scan argument list, and log an Error message for each unused argument
  // NOTE that the $i is used, not %i. This code is NOT in a function - it is inline,
  // so we need to use the $ (global scope) and not the % (local scope).
  for ($i = 1;  $i  < $Game::argc; $i++)
  {
     if (!$argumentFlag[$i])
        Error("Error: Unknown command line argument:  " @ $Game::argv[$i]);
  }

  if  (!$logModeEnabled)
  {
     SetLogMode(6);      //  Default to  a new logfile each session.
  }
  //  Set the add-on path list to specify the directories that will be
  //  available to the scripts and engine. note that *all* required
  //  directory trees are included: common and control as well as the
  //  user add-ons.
  $pathList = $addonList !$= "" ? $addonList @ ";control;common" : "control;common";
  SetModPaths($pathList);

  // Execute startup script for the common code modules
  Exec("common/main.cs");

  // Execute startup script for the control specific code modules
  Exec("control/main.cs");

  // Execute startup scripts for all user add-ons
  Echo("--------- Loading Add-ons ---------");
  LoadAddOns($addonList);
  Echo("Engine initialization complete.");

  OnStart();
}
