#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "socklib.h"
#include "pullers.h"

#define WWWHOST		"linuxtoday.com"
#define REQUEST		"GET /lthead.txt HTTP/1.1\r\nHost: linuxtoday.com\r\nUser-Agent: freecgi.com-puller/1.0\r\nConnection: close\r\n\r\n"
#define DELIM		"&&"

int main(void) {
	FILE *fp;
	char rx[256];
	tArticle a;
	char *cp;
	FILE *netin, *netout;

	if(!fpopensocket(WWWHOST,80,30,&netin,&netout)) {
		fprintf(stderr,"Error on connect\n");
		exit(1);
	}

	fprintf(netout,REQUEST);
	fflush(netout);

	/* skip to first record.. %% */
	while(fgets_strip(rx,255,netin)) {
		if(!strcmp(rx,DELIM)) break;
	}

	fp=fopen(LTDATA,"wb");

	/* now read the records */
	while(fgets_strip(rx,255,netin)) {

		/* I hate how LT puts "Internet Week: blah blah blah", "CZN: Blah blah blah".. Get rid of it */
		cp=strchr(rx,':');
		if(!cp) cp=rx; else cp++;

		strncpy(a.title,cp,255);
		fgets_strip(a.link,255,netin);
		fgets_strip(a.time,255,netin);
		fgets(rx,255,netin); /* terminator */

		fwrite(&a,sizeof(a),1,fp);
	}

	fclose(netin); fclose(netout);

	fclose(fp);

	return(0);
}
