/* Pepito v1.59  -- file: config.c --
   Copyright (c) 2000-02 Carlos del Cacho

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Contact the author: guggenjaimito@yahoo.com
*/

#ifndef UNIQUE_SOURCE_FILE
#  include "datos.h"
#  include <string.h>
#endif

#define SUPPORTED_LANGUAGES  17

lang_str_t lang_piece_str[] = {

    { "Czech",       { 'P', 'J', 'S', 'V', 'D', 'K' } },
    { "Danish",      { 'B', 'S', 'L', 'T', 'D', 'K' } },
    { "Dutch",       { 'O', 'P', 'L', 'T', 'D', 'K' } },
    { "English",     { 'P', 'N', 'B', 'R', 'Q', 'K' } },
    { "Estonian",    { 'P', 'R', 'O', 'V', 'L', 'K' } },
    { "Finnish",     { 'P', 'R', 'L', 'T', 'D', 'K' } },
    { "French",      { 'P', 'C', 'F', 'T', 'D', 'R' } },
    { "German",      { 'B', 'S', 'L', 'T', 'D', 'K' } },
    { "Hungarian",   { 'G', 'H', 'F', 'B', 'V', 'K' } },
    { "Icelandic",   { 'P', 'R', 'B', 'H', 'D', 'K' } },
    { "Italian",     { 'P', 'C', 'A', 'T', 'D', 'R' } },
    { "Norwegian",   { 'B', 'S', 'L', 'T', 'D', 'K' } },
    { "Polish",      { 'P', 'S', 'G', 'W', 'H', 'K' } },
    { "Portuguese",  { 'P', 'C', 'B', 'T', 'D', 'R' } },
    { "Romanian",    { 'P', 'C', 'N', 'T', 'D', 'R' } },
    { "Spanish",     { 'P', 'C', 'A', 'T', 'D', 'R' } },
    { "Swedish",     { 'B', 'S', 'L', 'T', 'D', 'K' } }
};

char *piece_rep = lang_piece_str[15].pieces;

/* Función que lee el archivo de configuración */

void            Config()
{
    FILE           *fich,
                  **ppfich = &fich;
    char            linea[512],
                   *pos_cad,
                    arg[80];

    *EGTB_PATH = '\0';
    EGTB_CSIZE = 1048576L;
    BOOK_RANDOM = 1;
    USE_BOOK = 3;
    BOOK_WIDTH = 255;
    log_file = NULL;

    HASH_SIZE = 524288L;
    PAWN_HASH_SIZE = 131072L;
    CACHE_SIZE = 32768L;

    RESIGN_MARGIN = -900;
    CONTROL_INC = 20000;
    DISPLAY_MARGIN = 5 * UNIT_PROF;
    UCI_MODE = 0;
    COORD_MODE = 0;
    BOOK_LEARN = 1;

    MAXPROF = 35 * UNIT_PROF;
    GAME_STYLE = 0;

    Print("\n"VERSION_STRING", Copyright (C) 2000-02 Carlos del Cacho\n\n"
          "Pepito comes with ABSOLUTELY NO WARRANTY. This is free\n"
          "software, you can redistribute it and/or modify it under\n"
          "the terms of the GNU General Public License as published\n"
          "by the FSF; see file COPYING for details.\n\n");

    fich = fopen("config.dat", "r");
    if (fich == NULL)
        fich = fopen("CONFIG.DAT", "r");
    if (fich == NULL)
        fich = fopen("Config.dat", "r");
    if (fich == NULL) {
        Print("Couldn't find config.dat file. Using defaults.\n");
        return;
    } else {
        while (LeeLinea(ppfich, linea, 512)) {
            if (*linea == '#' || *linea == '\0')
                continue;
            pos_cad = linea;
            ExtraeArg(&pos_cad, arg);
            if (!strcmp(arg, "HASH_SIZE")) {
                ExtraeArg(&pos_cad, arg);
                HASH_SIZE = atoi(arg);
                switch (arg[strlen(arg) - 1]) {
                case 'k':
                case 'K':
                    HASH_SIZE = (long)(HASH_SIZE * (double)1024/sizeof(entrada_hash));
                    break;
                case 'b':
                case 'B':
                    HASH_SIZE = (HASH_SIZE+1)/sizeof(entrada_hash);
                    break;
                default:
                    HASH_SIZE = (long)(HASH_SIZE * ((double)1024*1024/sizeof(entrada_hash)));
                    break;
                }
                if (!HASH_SIZE)
                    HASH_SIZE = 1;
                while (HASH_SIZE & (HASH_SIZE - 1))
                    HASH_SIZE &= HASH_SIZE - 1;
            } else if (!strcmp(arg, "PAWN_HASH_SIZE")) {
                ExtraeArg(&pos_cad, arg);
                PAWN_HASH_SIZE = atoi(arg);
                switch (arg[strlen(arg) - 1]) {
                case 'k':
                case 'K':
                    PAWN_HASH_SIZE = (unsigned long)(PAWN_HASH_SIZE * (double)1024/sizeof(entrada_hash_p));
                    break;
                case 'b':
                case 'B':
                    PAWN_HASH_SIZE = (PAWN_HASH_SIZE+1)/sizeof(entrada_hash_p);
                    break;
                default:
                    PAWN_HASH_SIZE = (long)(PAWN_HASH_SIZE * ((double)1024*1024/sizeof(entrada_hash_p)));
                }
                if (!PAWN_HASH_SIZE)
                    PAWN_HASH_SIZE = 1;
                while (PAWN_HASH_SIZE & (PAWN_HASH_SIZE - 1))
                    PAWN_HASH_SIZE &= PAWN_HASH_SIZE - 1;
            } else if (!strcmp(arg, "ECACHE_SIZE")) {
                ExtraeArg(&pos_cad, arg);
                CACHE_SIZE = atoi(arg);
                switch (arg[strlen(arg) - 1]) {
                case 'k':
                case 'K':
                    CACHE_SIZE *= 64;
                    break;
                case 'b':
                case 'B':
                    CACHE_SIZE = (CACHE_SIZE+1)/16;
                    break;
                default:
                    CACHE_SIZE *= 65536;
                }
                if (!CACHE_SIZE)
                    CACHE_SIZE = 1;
                while (CACHE_SIZE & (CACHE_SIZE - 1))
                    CACHE_SIZE &= CACHE_SIZE - 1;
            } else if (!strcmp(arg, "GAME_STYLE")) {
                ExtraeArg(&pos_cad, arg);
                if (!strcmp(arg,"solid"))
                  GAME_STYLE = 0;
                else if (!strcmp(arg,"normal"))
                  GAME_STYLE = 1;  
                else if (!strcmp(arg,"agressive") || // misspelled it once, it's
                         !strcmp(arg,"aggressive"))  // here for historical reasons
                  GAME_STYLE = 2;
            } else if (!strcmp(arg, "EGTB_PATH")) {
                ExtraeArg(&pos_cad, arg);
                strcpy(EGTB_PATH, arg);
            } else if (!strcmp(arg, "EGTB_CSIZE")) {
                ExtraeArg(&pos_cad, arg);
                EGTB_CSIZE = atoi(arg) * 1048576L;
            } else if (!strcmp(arg, "BOOK_FILE")) {
                ExtraeArg(&pos_cad, arg);
                strcpy(BOOK_FILE, arg);
            } else if (!strcmp(arg, "RESIGN_MARGIN")) {
                ExtraeArg(&pos_cad, arg);
                RESIGN_MARGIN = atoi(arg);
            } else if (!strcmp(arg, "USE_BOOK")) {
                ExtraeArg(&pos_cad, arg);
                if (!strcmp("no", arg))
                    USE_BOOK = 0;
            } else if (!strcmp(arg, "BOOK_RANDOM")) {
                ExtraeArg(&pos_cad, arg);
                BOOK_RANDOM = strcmp("no", arg);
            } else if (!strcmp(arg, "DISPLAY_MARGIN")) {
                ExtraeArg(&pos_cad, arg);
                DISPLAY_MARGIN = atoi(arg) * UNIT_PROF;
            } else if (!strcmp(arg, "LANGUAGE")) {
                if (ExtraeArg(&pos_cad, arg))
                {
                    int i;

                    for (i=0; i < SUPPORTED_LANGUAGES; i++)
                        if (!strcmp(lang_piece_str[i].language,arg))
                            break;

                    if (i < SUPPORTED_LANGUAGES)
                        piece_rep = lang_piece_str[i].pieces;
                }
            }
            else if (!strcmp(arg, "LOGGING")) {
                ExtraeArg(&pos_cad, arg);
                if (strcmp("no", arg))
                    log_file = fopen("pepito.log", "w");
            }
            else if (!strcmp(arg, "COORD_MODE")) {
                ExtraeArg(&pos_cad, arg);
                if (!strcmp("yes", arg))
                    COORD_MODE = 1;
            }
            else if (!strcmp(arg, "BOOK_WIDTH")) {
                ExtraeArg(&pos_cad, arg);
                if (!(BOOK_WIDTH = atoi(arg)))
                    BOOK_WIDTH = 255;
            }
            else if (!strcmp(arg, "BOOK_LEARN")) {
                ExtraeArg(&pos_cad, arg);
                if (!strcmp("no", arg))
                    BOOK_LEARN = 0;
            }
            else if (!strcmp(arg, "BATCH_FILE")) {
                ExtraeArg(&pos_cad, arg);
                batch_file = fopen(arg,"r");
            }
        }
        fclose(fich);
    }

    Print("HASH_SIZE: %ld entries / %0.2f MB\n", HASH_SIZE, (float) (HASH_SIZE / (1024*1024/sizeof(entrada_hash))));
    Print("PAWN_HASH_SIZE: %ld entries / %0.2f MB\n", PAWN_HASH_SIZE, (float) (PAWN_HASH_SIZE / (1024*1024/sizeof(entrada_hash_p))));
    Print("ECACHE_SIZE: %ld entries / %0.2f MB\n", CACHE_SIZE, (float) (CACHE_SIZE / 65536.0));
    Print("EGTB_PATH: %s\n", EGTB_PATH);
    Print("RMARGIN: %d\n", RESIGN_MARGIN);
    Print("USE_BOOK: %s\n", USE_BOOK ? "yes" : "no");

    strcpy(linea, BOOK_FILE);
    strcat(linea, ".ind");
    fich = fopen(linea, "rb");
    Print("Trying to open %s... %sound\n", BOOK_FILE, (fich == NULL) ? "Not f" : "F");

    if (fich != NULL)
        fclose(fich);
    else
        *BOOK_FILE = 0;

    Print("BOOK_RANDOM: %s\n\n", BOOK_RANDOM ? "yes" : "no");
    Print("\nType \"help\" for a list of available commands.\n");
    gamelog_file = fopen("pepito.pgn", "a");
}
