
#define _XOPEN_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>

#include <mysql/mysql.h>

#define BDEL "<div class=\"blogPost\">"
#define BD2 "<div"

#define DDEL "/&nbsp;"
#define DD2 "</a>"

int main(int argc, char **argv)
{

	int orig;

	struct tm t1;
	char *cpos,*cpos_e, *bpos;
	
	/* I'm a lazy idiot */
	char buff[1024*1024];
	
	char q1[1024*1024], q2[1024],q3[1024];
	
	if (argc!=2) exit(5);
	argv++;

	if ( (orig=open(*argv,O_RDONLY))==-1) exit(6);

	read(orig, buff, 1024*1024);
	bpos=buff;
	while (42)
	{
		if ( (cpos=strstr(bpos,BDEL))==NULL) break;
		cpos+=strlen(BDEL);
		cpos++;
			
		cpos_e=(strstr(cpos,BD2));

		memset(q1,0,1024*1024);
		mysql_escape_string(q1,cpos, (cpos_e-cpos-11));
//		strncpy(q1,cpos,(cpos_e-cpos));

//		printf("%s \n------------u\n", q1);
			
		bpos=cpos;

		if ( (cpos=strstr(bpos,DDEL))==NULL) break; /* probably unneeded*/ 
		cpos+=strlen(DDEL);

		cpos_e=(strstr(cpos,DD2));

		memset(q2,0,1024);
		strncpy(q2,cpos,(cpos_e-cpos));
//		printf("Date: %s\n", q2);
		strptime(q2,"%m/%d/%Y %r",&t1);
		strftime(q3,1024,"%F %T", &t1);
//		printf("mod.date %s\n",q3);
		
		bpos=cpos;
		printf("INSERT INTO wp_posts (post_author,post_date,post_content, post_title, post_excerpt,post_status, post_password, post_name,to_ping,pinged, language) VALUES \
				             ( 0,            '%s',     '%s'   ,      ''      ,     ''     , 'publish' ,  ''           , ''      , '' ,    '',     'bg');\n",
					                     q3,       q1);
		
	}
	
	return 0;
}
