
#include "querydb.h"

int get_params (char *file, unsigned int *timestamp)
{
	char filebuffer [1024];
	char date [20];
	char buf [20];
	char *p;
	unsigned int day, month, year;
	struct tm timestr;
		
	/* Filename format: /ncc/ttpro/raw_data/2000/07/16/SNDP.osdorp.ripe.net.20000716.gz */
	
	/* Extract date from the filename */
	strcpy(filebuffer,file);
	p = strtok(filebuffer,".");	
	p = strtok(NULL,".");	
	p = strtok(NULL,".");	
	p = strtok(NULL,".");	
	p = strtok(NULL,"");
	
	/* Split yyyymmdd date in year, month and day */	
	strcpy (date,p);
	strcpy (buf,date);
	buf[4] = '\0';
	year = atoi(buf);
	
	strcpy(date,&date[4]);
	strcpy (buf,date);
	buf[2] = '\0';
	month = atoi(buf);
	
	strcpy(date,&date[2]);
	date[2]='\0';
	day = atoi(date);
	
	/* Transform into GMT timestamp */
	timestr.tm_sec   = 0;
	timestr.tm_min   = 0;
	timestr.tm_hour  = 0;
	timestr.tm_mday  = day;
	timestr.tm_mon   = month - 1;
	timestr.tm_year  = year - 1900;
        timestr.tm_isdst = -1;
	
	if ((*timestamp = mktime(&timestr))==-1)
	{
		printf ("Cannot compute date for %s\n",file);
		return(0);
	};
	
	return (1);
};

void testquery (MYSQL *mysql, char *sourcefile, char *path_boxes)
{
	char *files[1024];
	unsigned int routeID,Nhops;
	unsigned int i,n,gz,timestamp;
	GHashTable *ranges;
	GHashTable* Data;

	char *p,*filename;
	
	char buf[1024];
	char src[256];
	char dst[256];

	int *srcid;
	int *dstid;

	gzFile fdz;
	FILE *fd;
	
	fd = fdz = NULL;

	gz = 0;
	
	ranges  = g_hash_table_new (g_str_hash,g_str_equal);
	
	n = get_filenames (sourcefile,files);

	get_ranges_by_boxname (ranges, path_boxes);
	
	printf ("%s: Found %u files\n",sourcefile,n);

	for (i=0;i<n;i++)
	{
		printf ("%s\n",files[i]);
				
		filename = strdup(files[i]);		
		
		/* Check if filename ends with gz */
		p = strrchr(filename, '.');
		if (p && strcmp(p+1,"gz") == 0) gz = 1;
		
		p = strtok (filename,".");
		p = strtok (NULL,".");
		strcpy (src,p);
		strcat(src,".");
		p = strtok (NULL,".");
		strcat(src,p);
		strcat(src,".");
		p = strtok (NULL,".");
		strcat(src,p);	

		srcid = g_hash_table_lookup (ranges,src);
			
		filename = strdup(files[i]);
		
		if (!get_params (filename,&timestamp))
		{
			fprintf (stderr,"Error: Cannot get timestamp for %s\n",filename);
			continue;
		};
		
		if (!(Data = build_data (mysql,*srcid,timestamp)))
		{
			fprintf (stderr,"Could not build Data Hash for file %s\n",filename);
			continue;
		};
		
		if(gz)
		{
			/* use gzopen */
			if((fdz = gzopen(filename, "rb")) == NULL)
			{
				fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
				exit (1);
			};
		}
		else
		{
			/* use regular open */
			if((fd = fopen(filename,"rb")) == NULL)
			{
				fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
				exit (1);
			};
		};		
		
		while((gz && !gzeof(fdz)) || (!gz && !feof(fd)))
		{
			if(gz)
			{
				if(gzgets(fdz, buf, sizeof(buf)) == Z_NULL) continue;
			}
			else
			{
				if(fgets(buf, sizeof(buf), fd) == NULL) continue;
			};
			
			//printf ("%s",buf);
			
			p = strtok (buf," ");
			p = strtok (NULL," ");
			p = strtok (NULL," ");
			
			timestamp = atoi(p);
			p = strtok (NULL," ");
			p = strtok (NULL," ");
			
			strcpy (dst,p);
			
			dstid = g_hash_table_lookup (ranges,dst);
			
			printf ("%s[%02u]-%s[%02u] %u ",src,*srcid,dst,*dstid,timestamp);
	
			if (match_vector(Data, *dstid, timestamp, &routeID, &Nhops))
			{
				printf ("routeid: %u nhops: %u\n",routeID,Nhops);
			}
			else
			{
				printf ("Not found\n");
			};

		};
		
		if (gz) gzclose(fdz);
		else fclose (fd);
		
		reset_data (Data);
	};
};


int main (int argc, char *argv[])
{
	MYSQL mysql;

	putenv("TZ=GMT+0");  /* enforce GMT timezone */

	open_user_connection (&mysql, MYSQL_HOST);

	testquery(&mysql, argv[1], PATHBOXES);
	
	mysql_close(&mysql);

	return (0);
};

	
