// Copyright 1996, 1997 by Jon Dart.  All Rights Reserved.

#include "globals.h"
#include "bearing.h"
#include "moveord.h"
#include "scoring.h"
#include "pinfo.h"

#ifdef _WINDOWS
#include "book.h"
#include "arasavw.h"
CArasanView *theView;
char initialFilename[MAX_PATH];
#else
char cmd_buf[256];
int cmd_buf_count;
#endif

Book *opening_book = NULL;
Log *theLog = NULL;
Options *global_options;
Move_Array *game_moves;
char *programPath;

void init_globals(const char *pathName, 
  BOOL useOpeningBook, BOOL createLog, CWinApp *mainApp)
{
    // must do this first, many things depend on it:
    Bitmap::init();
    // set up attack bitmaps, etc.
    Bearing::init();
    // repetition table
    programPath = new char[MAX_PATH];
    strcpy(programPath,pathName);

    // We need to intialize the options class differently
    // depending on whether we are a Windows app (with a
    // GUI) or a console program.
#ifdef _WINDOWS
    global_options = new Options(mainApp);
#else
    global_options = new Options();
    cmd_buf_count = 0;
    *cmd_buf = '\0';
#endif
    game_moves = new Move_Array();
    if (createLog)
    {
       theLog = new Log();
       theLog->clear();
       theLog->write_header();
    }
    if (useOpeningBook)
    {
       opening_book = new Book(); 
    }
    Move_Ordering::clear_killer();
    Move_Ordering::clear_history();
}

// Make this a __cdecl function so we can pass it to atexit()
void __cdecl cleanup_globals()
{
    if (opening_book) delete opening_book;
    delete game_moves;
    Move_Record::freeAll(TRUE);
    delete global_options;
    if (theLog) delete theLog;
    delete [] programPath;
    Scoring::cleanup();
    Position_Info::freeAll(TRUE);
    Bitmap::cleanup();
}