/*
 *  Copyright (C) 2006 Ludovic Jacomme (ludovic.jacomme@gmail.com)
 *
 *  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 of the License, 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; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <zlib.h>
#include "SDL.h"

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <pspctrl.h>
#include <pspthreadman.h>

#include <pspwlan.h>
#include <pspkernel.h>
#include <psppower.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "psp_global.h"
#include "psp_sdl.h"

  char psp_homedir[128];
  GGO_t GGO;

int
psp_global_reset()
{
  GGO.current_x = 0;
  GGO.current_y = 0;
  GGO.last_move_x = 0;
  GGO.last_move_y = 0;
  GGO.passes  = 0;

  memset(GGO.board_array, GGO.board_max_size, 0);

  return 0;
}

int
psp_global_init()
{
  memset(&GGO, sizeof(GGO_t), 0);
  GGO.board_max_width = 19;
  GGO.board_min_width = 5;
  GGO.board_max_size  = GGO.board_max_width * GGO.board_max_width;
  GGO.board_array = (char *)malloc( GGO.board_max_size );
  GGO.board_width = 10;

  GGO.board_color = 0x0; /* Black */
  GGO.xor_color   = psp_sdl_rgb(0xff, 0xff, 0xff);

  GGO.go_handicap = 0;
  GGO.go_player_color = GGO_BLACK;
  GGO.psp_cpu_clock = 222;
  GGO.psp_screenshot_mode = 0;

  psp_parse_configuration();
 
  scePowerSetClockFrequency(GGO.psp_cpu_clock, GGO.psp_cpu_clock, GGO.psp_cpu_clock/2);

  psp_global_reset();

  return 0;
}

int
psp_save_configuration(void)
{
  char  FileName[MAX_PATH];
  FILE* FileDesc;
  int   error = 0;

  strcpy(FileName, psp_homedir);
  strcat(FileName, "/pspggo.cfg");

  FileDesc = fopen(FileName, "w");
  if (FileDesc != (FILE *)0 ) {

    fprintf(FileDesc, "psp_cpu_clock=%d\n"  , GGO.psp_cpu_clock);
    fprintf(FileDesc, "go_handicap=%d\n"    , GGO.go_handicap);
    fprintf(FileDesc, "go_player_color=%d\n", GGO.go_player_color);
    fprintf(FileDesc, "go_board_width=%d\n" , GGO.board_width);

    fclose(FileDesc);

  } else {
    error = 1;
  }

  return error;
}

int
psp_parse_configuration(void)
{
  char  FileName[MAX_PATH + 1];
  char  Buffer[512];
  char *Scan;
  unsigned int Value;
  FILE* FileDesc;

  sprintf(FileName, psp_homedir);
  strcat(FileName, "/pspggo.cfg");

  FileDesc = fopen(FileName, "r");
  if (FileDesc == (FILE *)0 ) return 0;

  while (fgets(Buffer,512, FileDesc) != (char *)0) {

    Scan = strchr(Buffer,'\n');
    if (Scan) *Scan = '\0';
    /* For this #@$% of windows ! */
    Scan = strchr(Buffer,'\r');
    if (Scan) *Scan = '\0';
    if (Buffer[0] == '#') continue;

    Scan = strchr(Buffer,'=');
    if (! Scan) continue;

    *Scan = '\0';
    Value = atoi(Scan+1);

    if (!strcasecmp(Buffer,"psp_cpu_clock")) GGO.psp_cpu_clock = Value;
    else
    if (!strcasecmp(Buffer,"go_handicap")) GGO.go_handicap = Value;
    else
    if (!strcasecmp(Buffer,"go_player_color")) GGO.go_player_color = Value;
    else
    if (!strcasecmp(Buffer,"go_board_width")) GGO.board_width = Value;
  }

  fclose(FileDesc);

  return 0;
}


