#include <stdio.h>
#include "stringops.h"
#include "globals.h"
#include "room.h"

static char tempstr[1024];

globals *globals_load (char *name)
{
	FILE *f;
	globals *g;
	int room;

	#ifdef FUNCTIONS
	puts ("**globals_load");
	#endif

	g = allocate(globals);
	copystring (g->globals_file, name);

	f = fopen (g->globals_file, "r");

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->site, tempstr);

	fgets (tempstr, 199, f);
	g->port = atoi (tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->password, tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->help_file, tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->player_file, tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->group_file, tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->room_file, tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->command_str, tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	copystring (g->home_str, tempstr);

	fgets (tempstr, 199, f);
	room = atoi (tempstr);

	fgets (tempstr, 199, f);
	g->moveodds = atoi (tempstr);

	fgets (tempstr, 199, f);
	g->movequeuesize = atoi (tempstr);

	fgets (tempstr, 199, f);
	g->actodds = atoi (tempstr);

	fgets (tempstr, 199, f);
	clipret (tempstr);
	g->name = name_parse (tempstr);

	fclose (f);

	g->player_list = player_load (g->player_file);
	g->player_current = g->player_list->head;

	g->group_list = allocate (group_list);
	g->group_list->head = NULL;
	g->group_list->tail = NULL;
	g->group_list->size = 0;
	g->group_list = group_load (g->group_file);
	g->group_current = g->group_list->head;

	g->room_list = room_load (g->room_file);
	g->room_current = g->room_list->head;
	g->room_home = room_find_num (g->room_list, room);

	g->rooms_visited = allocate (room_queue);
	g->rooms_visited->head = g->rooms_visited->tail = NULL;
	g->rooms_visited->size = 0;

	g->socket = socket_allocate (NULL, 0L);
	if (socket_connect (g->socket, g->site, g->port) != 0)
	{
		globals_burn (g);
		exit (1);
	}

	return g;
}

void globals_save (globals *g)
{
	FILE *f;

	#ifdef FUNCTIONS
	puts ("**globals_save");
	#endif

	f = fopen(g->globals_file, "w");

	fprintf (f, "%s\n", g->site);
	fprintf (f, "%d\n", g->port);
	fprintf (f, "%s\n", g->password);
	fprintf (f, "%s\n", g->help_file);
	fprintf (f, "%s\n", g->player_file);
	fprintf (f, "%s\n", g->group_file);
	fprintf (f, "%s\n", g->room_file);
	fprintf (f, "%s\n", g->command_str);
	fprintf (f, "%s\n", g->home_str);
	fprintf (f, "%d\n", room_num (g->room_list, g->room_home));
	fprintf (f, "%d\n", g->moveodds);
	fprintf (f, "%d\n", g->movequeuesize);
	fprintf (f, "%d\n", g->actodds);
	name_save (g->name, f);
	fclose (f);

	player_save (g->player_list, g->player_file);
	group_save (g->group_list, g->group_file);
	room_save (g->room_list, g->room_file);
}

void globals_burn (globals *g)
{
	#ifdef FUNCTIONS
	puts ("**globals_burn");
	#endif

	free (g->password);
	free (g->site);
	free (g->help_file);
	free (g->player_file);
	free (g->group_file);
	free (g->room_file);
	free (g->command_str);
	free (g->home_str);

	player_burn (g->player_list);
	group_burn (g->group_list);
	name_burn (g->name);
	room_burn (g->room_list);
	room_queue_burn (g->rooms_visited);

	socket_disconnect (g->socket);
	g->socket = socket_deallocate (g->socket, g->socket);

	free (g);
}