/* 
 * Servers Layer
 * 
 * servers.c
 *
 * Interface to the servers layer.
 * 
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <config.h>
#include "servers.h"


/*
 * servers_filename: the file name that stores the servers data
 */
static char servers_filename[MAX_FILENAME_SIZE];

// V_flag switch
static int V_flag;


int servers_new(char *filename, int v) {
	FILE *fp = NULL;
	V_flag = v;
	strncpy(servers_filename, filename, MAX_FILENAME_SIZE - 1);
	
	fp = fopen(servers_filename, "r+");

	if (fp == NULL) {
		return -1;
	}
	
	fclose(fp);
	
	vprintf("\33[1m[servers_new]\33[0m '%s' loaded.\n", filename);
	return 0;
}


void servers_insert(char *servername, char *address, int port) {
	char temp_line[MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE];
	FILE *fp = fopen(servers_filename, "r+");
	int found = 0;
	
	while (fgets(temp_line, MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE, fp) != NULL) {
		int i;
		
		for (i = 0; i < MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE
			&& temp_line[i] != ' '; i++);
		
		temp_line[i - 1] = '\0';
		
		if (strncmp(servername, temp_line, i - 1) == 0) {
			found = 1;
			break;
		}
	}
	
	if (!found) {
		fseek(fp, 0L, SEEK_END);
		fprintf(fp, "%s: %s %d\n", servername, address, port);
	}
	
	fclose(fp);
}


void servers_get_default(char *servername) {
	char temp_servername[MAX_COUNTER_ID_SIZE + 1];
	FILE *fp = fopen(servers_filename, "r+");
	
	fscanf(fp, "%s", temp_servername);
	
	for (int i = 0; i < MAX_COUNTER_ID_SIZE; i++) {
		if (temp_servername[i] == ':') {
			servername[i] = '\0';
			break;
		} else {
			servername[i] = temp_servername[i];
		}
	}
	
	fclose(fp);
}


void servers_get(char *servername, char *address, int *port) {
	char temp_line[MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE];
	FILE *fp = fopen(servers_filename, "r+");
	
	while (fgets(temp_line, MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE, fp) != NULL) {
		int i, j;
		
		for (i = 0; i < MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE
			&& temp_line[i] != ' '; i++);
		for (j = i + 1; j < MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE
			&& temp_line[j] != ' '; j++);
		
		temp_line[strlen(temp_line) - 1] = '\0';
		temp_line[j] = '\0';
		temp_line[i - 1] = '\0';

		if (strncmp(servername, temp_line, i - 1) == 0) {
			strcpy(address, temp_line + i + 1);
			*port = atoi(temp_line + j + 1);
			break;
		}
	}
	
	fclose(fp);
}


void servers_get_id(char *address, int port, char *servername) {
	char temp_line[MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE];
	FILE *fp = fopen(servers_filename, "r+");
	
	while (fgets(temp_line, MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE, fp) != NULL) {
		int i, j;
		
		for (i = 0; i < MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE
			&& temp_line[i] != ' '; i++);
		for (j = i + 1; j < MAX_COUNTER_ID_SIZE + 2 * MAX_ADDRESS_SIZE
			&& temp_line[j] != ' '; j++);
		
		temp_line[strlen(temp_line) - 1] = '\0';
		temp_line[j] = '\0';
		temp_line[i - 1] = '\0';

		if (strcmp(address, temp_line + i + 1) == 0
		   && port == atoi(temp_line + j + 1)) {
			strcpy(servername, temp_line);
			break;
		}
	}
	
	fclose(fp);
}


void servers_dump() {
	FILE *fp = fopen(servers_filename, "r");
	int c;
	
	vprintf("Servers:\n");
	
	while ((c = fgetc(fp)) != EOF) {
		vprintf("%c", c);
	}
	
	fclose(fp);
}
