$baseMods = "common";
$userMods = "legends";
$displayHelp = false;
$logModeSpecified = false;
$serverVersionScripts = "4"; //global version tag for scripts, not the actual EXE version
$ZipContainerVerbose = 1;
$ZipContentsVerbose = 0;

// -----------------------------------------------------------------------------
// The displayHelp, onStart, onExit and parseArgs function are overriden by mod
//  packages to get hooked into initialization and cleanup
// ----

function onStart()
{
    // -- Default startup function
}

function onExit()
{
    // -- OnExit is called directly from C++ code, whereas onStart is invoked
    //    at the end of this file.
}

function parseArgs()
{
    // -- Here for mod override, the arguments have already been parsed.
}

package Help
{
    function onExit()
    {
        // Override onExit when displaying help
    }
};

// ----

function displayHelp()
{
    activatePackage(Help);

    error(   "Torque command line options:"
    @ "\n" @ "  -log <logmode>         Logging behavior; see main.cs comments for details"
    @ "\n" @ "  -game <game_name>      Reset list of mods to only contain <game_name>"
    @ "\n" @ "  -mod <mod_name>        Add <mod_name> to list of mods"
    @ "\n" @ "  -console               Open a separate console"
    @ "\n" @ "  -show <shape>          Launch the TS show tool"
    @ "\n" @ "  -jSave  <file_name>    Record a journal"
    @ "\n" @ "  -jPlay  <file_name>    Play back a journal"
    @ "\n" @ "  -jDebug <file_name>    Play back a journal and issue an int3 at the end"
    @ "\n" @ "  -help                  Display this help message"
    @ "\n" @ "  -trace                 Start a function trace on startup"
    @ "\n" @ "  -echoload              Echos the access of files"
    @ "\n" @ "  -zipcontentsverbose    Display the content of any zip files when loaded"
    @ "\n"
    );
}

// -----------------------------------------------------------------------------
// Support functions used to manage the mod string and load mods
// ----

function pushFront( %list, %token, %delim )
{
    if( %list $= "" )
        return %token;
    return %token @ %delim @ %list;
}

function pushBack( %list, %token, %delim )
{
    if( %list $= "" )
        return %token;
    return %list @ %delim @ %token;
}

function popFront( %list, %delim )
{
    return nextToken(%list, unused, %delim);
}

function loadMods(%modPath)
{
    %modPath = nextToken(%modPath, token, ";");
    if( %modPath !$= "" )
        loadMods(%modPath);

    exec(%token @ "/main.cs");
}


// -----------------------------------------------------------------------------
// Process command line arguments
// ----

    for( $i = 1; $i < $Game::argc ; $i++ )
    {
        $arg = $Game::argv[$i];
        $nextArg = $Game::argv[$i+1];
        $hasNextArg = $Game::argc - $i > 1;

        switch$( $arg )
        {
            // ---- ---- ---- ----
            case "-log":
                $argUsed[$i]++;

                // -- Notes on logmode: console log is written to 'console.log'
                //    '-log 0' disables console logging
                //    '-log 1' appends to existing logfile; it also closes the file
                //             (flushing the write buffer) after every write.
                //    '-log 2' overwrites any existing logfile; it also only closes
                //             the logfile when the application shuts down.  (default)

                if( $hasNextArg )
                {
                    // -- if console logging is enabled, dump the console thus far
                    if( $nextArg != 0 )
                        $nextArg += 4;
    
                    setLogMode($nextArg);
                    $logModeSpecified = true;
                    $argUsed[$i+1]++;
                    $i++;
                }
                else
                    error("Error: Missing Command Line argument. Usage: -log <Mode: 0,1,2>");

            // ---- ---- ---- ----
            case "-mod":
                $argUsed[$i]++;
                if( $hasNextArg )
                {
                    // -- Append the mod to the end of the current list
                    $userMods = strreplace($userMods, $nextArg, "");
                    $userMods = pushFront($userMods, $nextArg, ";");
                    $argUsed[$i+1]++;
                    $i++;
                }
                else
                    error("Error: Missing Command Line argument. Usage: -mod <mod_name>");

            // ---- ---- ---- ----
            case "-game":
                $argUsed[$i]++;
                if( $hasNextArg )
                {
                    // -- Remove all mods, start over with game
                    $userMods = $nextArg;
                    $argUsed[$i+1]++;
                    $i++;
                }
                else
                    error("Error: Missing Command Line argument. Usage: -game <game_name>");

            // ---- ---- ---- ----
            case "-show":
                // A useful shortcut for -mod show
                $userMods = strreplace($userMods, "show", "");
                $userMods = pushFront($userMods, "show", ";");
                $argUsed[$i]++;

            // ---- ---- ---- ----
            case "-console":
                enableWinConsole(true);
                $argUsed[$i]++;

            // ---- ---- ---- ----
            case "-jSave":
                $argUsed[$i]++;
                if( $hasNextArg )
                {
                    echo("Saving event log to journal: " @ $nextArg);
                    saveJournal($nextArg);
                    $argUsed[$i+1]++;
                    $i++;
                }
                else
                    error("Error: Missing Command Line argument. Usage: -jSave <journal_name>");

            // ---- ---- ---- ----
            case "-jPlay":
                $argUsed[$i]++;
                if( $hasNextArg )
                {
                    playJournal($nextArg,false);
                    $argUsed[$i+1]++;
                    $i++;
                }
                else
                    error("Error: Missing Command Line argument. Usage: -jPlay <journal_name>");

            // ---- ---- ---- ----
            case "-jDebug":
                $argUsed[$i]++;
                if( $hasNextArg )
                {
                    playJournal($nextArg,true);
                    $argUsed[$i+1]++;
                    $i++;
                }
                else
                    error("Error: Missing Command Line argument. Usage: -jDebug <journal_name>");

            // ---- ---- ---- ----
            case "-trace":
                trace(true);
                $argUsed[$i]++;

            // ---- ---- ---- ----
            case "-echoload":
                setEchoFileLoads(true);
                $argUsed[$i]++;

            // ---- ---- ---- ----
            case "-zipcontentsverbose":
                $ZipContentsVerbose = 1;
                $argUsed[$i]++;

            // ---- ---- ---- ----
            case "-help":
                $displayHelp = true;
                $argUsed[$i]++;

            // ---- ---- ---- ----
            case "-perfcounter":
                $argUsed[$i]++;

                if( $hasNextArg )
                {
                    SetPerfCounterEnable($nextArg);
                    $argUsed[$i+1]++;
                    $i++;
                }
                else
                    error("Error: Missing Command Line argument. Usage: -perfcounter <Mode: 0,1>");
        }
    }

// -----------------------------------------------------------------------------
// Action Stage, lets start this game up...
// ----

// -- Default to a new logfile each session.
if( !$logModeSpecified )
    setLogMode(5);

// -- combine the user and base mods into modpath
$modPath = pushBack($userMods, $baseMods, ";");

// -- dictate the directories visible to the scripts and resource engine
setModPaths($modPath);

// -- Get the first mod on the list, which will be the last to be applied...
nextToken($modPath, currentMod, ";");

// -- Execute startup scripts for each mod, starting at base and working up
echo("");
warn("--------- Loading MODS ---------");
loadMods($modPath);
echo("");

// -- Parse the command line arguments
warn("--------- Parsing Arguments ---------");
parseArgs();
echo("");

// ----
if( $pref::enableConsole )
    enableWinConsole(true);

if( $pref::enableEchoLoad )
    setEchoFileLoads(true);

if( $pref::Server::enablePerfCounter !$= "" )
    SetPerfCounterEnable($pref::Server::enablePerfCounter);

if( $displayHelp )
{
    enableWinConsole(true);
    displayHelp();
}
else
{
    onStart();
    echo("Engine initialized...");

    // -- Display an error message for unused arguments
    for( $i = 1; $i < $Game::argc; $i++ )
        if( !$argUsed[$i] )
            error("Error: Unknown command line argument: " @ $Game::argv[$i]);
}

// -----------------------------------------------------------------------------
