//MailParser v2.0
//Part of the GuildTools package
//Parses the data saved in SavedVariables.lua by IncomingMail WoW AddOn
//into data text file.
//
//Author: Roman Tarakanov (RTE/Arthas)
//Date: Sep 13 '05

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv){
  int i, j, desc_len=0;
  FILE *in, *out;
  char buf[1000], num[100], name[100], desc[1000], pic[100], tmp[10], from[60], when[12];
  int quality, money;
  char link[10000];

  char VER[7]="2.0";
  char LUAFILE[101]="../SV/GuildTools.lua";
  char OUTPUT[101]="./report.dat";

  link[0]= '\0';

  if (argc > 1)
  {
    printf("\nTextMailParser v%s \nParses the data saved in SavedVariables.lua by IncomingMail into text format.\n\n", VER);
  } else {
    printf("\nTextMailParser v%s \nParses the data saved in SavedVariables.lua by IncomingMail into text format.\n\n", VER);
    printf("Takes string as parameter.\n");
    printf("String can contain any text. \n\%<char> combinations will be raplaced by representative strings.\n");
    printf("Parser will generate output file acording to the schema provided.\n");
    printf("\%");printf("n - \'name\', name of the item.\n");
    printf("\%");printf("d - \'description\', description of the item.\n");
    printf("\%");printf("a - \'amount\', Number of items.\n");
    printf("\%");printf("q - \'quality\', quality of the item.\n");
    printf("\%");printf("p - \'pic\', name of the picture of the item.\n");
    printf("\%");printf("m - \'money\', money amount included in mail.\n");
    printf("\%");printf("f - \'from\', sender of the mail.\n");
    printf("\%");printf("w - \'when\', date when the mail was received.\n");
    printf("\nEx: \"TextBankParser insert into Table values ( \%");printf("n , \%");printf("a )\" will produce file\n");
    printf("report.dat, where every row will look like \n\"insert into Table values ( '<name>' , '<#>' )\".\n");

    return 0;
  }

  //Open SavedVariables.lua file for reading
  if ((in=fopen(LUAFILE,"rt")) == NULL)
  {
    if (argc>1) printf("Error: Could not find input file. Make sure that you specified proper file.\n");
    else fprintf(stderr,"Error: Could not find input file. Make sure that you copied SavedVaribles.lua to the AddOn folder.\n");
    fclose(in);
    printf("Press Enter to exit.");
    getc(stdin);
    return 1;
  }
  //Create and open index.html for write
  if ((out=fopen(OUTPUT,"wt")) == NULL)
  {
    if (argc>1) printf("Error: Could not create output file. Make sure that you specified proper file.\n");
    else fprintf(stderr, "Error: Could not create output file (./html/index.html). Make sure you have permision to create files.\n");
    fclose(in);
    fclose(out);
    printf("Press Enter to exit.");
    getc(stdin);
    return 1;
  }

  //Scan for the GT_IM_Saved saved variables
  while ((fscanf(in, "%s", buf)!=EOF) && (strncmp(buf,"GT_IM_Saved",12)!=0)) ;
  //If variables are not found - halt
  if (strncmp(buf,"GT_IM_Saved",12) != 0)
  {
    fprintf(stderr, "Error: Data was not found in SavedVariables.lua. Make sure you run IncomingMail script first.\n");
    fclose(in);
    fclose(out);
    printf("Press Enter to exit.");
    getc(stdin);
    return 1;
  }

  fscanf(in, "%s", buf);
  //untill the end of variables reached do:
  while (strncmp(buf, "}", 1)!=0)
  {
    //if new set of variables do:
    if (strncmp(buf, "{", 1))
    {
      quality=0;
      name[0]='\0';
      num[0]='\0';
      pic[0]='\0';
      desc[0]='\0';
      link[0]='\0';
      money=0;
      from[0]='\0';
      when[0]='\0';
      //untill the end of the set of variables do:
      while (strncmp(buf, "},", 2)!=0)
      {
	//if the variable is "number" write it's value to num
	if (strncmp(buf, "[\"number\"]", 10)==0)
	{
	  fscanf(in, " = \" %s \",", buf);
	  strncpy(num, buf, strlen(buf)+1);
	}
	//if the variable is "quality" write it's value to num
	if (strncmp(buf, "[\"quality\"]", 11)==0)
	{
	  fscanf(in, " = \" %d \",", &quality);
	}
	//if the variable is "coin" write it's value to money
	if (strncmp(buf, "[\"coin\"]", 8)==0)
	{
	  fscanf(in, " = \" %d \",", &money);
	}
	//if the variable is "pic" write it's value to pic
	if (strncmp(buf, "[\"pic\"]", 7)==0)
	{
	  fscanf(in, " = \" %s \",", buf);
	  strncpy(pic, buf, strlen(buf)+1);
	}
	//if the variable is "name" write it's value to name
	//with apropriate modifications
	if (strncmp(buf, "[\"name\"]", 8)==0)
	{
	  fscanf(in, " = \" %s", buf);
	  while (strncmp(buf, "\",", 2)!=0)
	  {
	    if (name[0]!='\0')
	    {
	      strncat(name, " ", 1);
	    }
	    strncat(name, buf, strlen(buf)+1);
	    fscanf(in, "%s", buf);
	  }
	}
	//if the variable is "from" write it's value to from
	if (strncmp(buf, "[\"from\"]", 8)==0)
	{
	  fscanf(in, " = \" %s", buf);
	  while (strncmp(buf, "\",", 2)!=0)
	  {
	    strncat(from, buf, strlen(buf)+1);
	    fscanf(in, "%s", buf);
	  }
	}
	//if the variable is "when" write it's value to when
	if (strncmp(buf, "[\"when\"]", 8)==0)
	{
	  fscanf(in, " = \" %s \",", buf);
	  strncpy(when, buf, strlen(buf)+1);
	}
	//if the variable is "description" write it's value to desc
	//with apropriate modifications
	if (strncmp(buf, "[\"description\"]", 15)==0)
	{
	  fscanf(in, " = \" %s", buf);
	  while (strncmp(buf, "\",", 2)!=0)
	  {
	    if (strncmp(buf, "<n>", 3)==0)
	    {
	      strncat(desc, "\n", 2);
	      desc_len=0;
	    }
	    else
	    if (strncmp(buf, "<t>", 3)==0)
	    {
	      for (i = desc_len; i<40; i++)
		strncat(desc, " ", 2);
	      strncat(desc, "\0", 1);

	    }
	    else
	    {
	      strncat(desc, " ", 1);
	      strncat(desc, buf, strlen(buf)+1);
	      desc_len+=strlen(buf)+1;
	    }
	    fscanf(in, "%s", buf);
	  }
	}
	fscanf(in, "%s", buf);
      }
      //end of the set of variables
      if (strncmp(buf, "},", 2)==0) buf[0]='\0';

      link[0]='\0';
      for (i=1; i<argc; i++)
      {
	  if (argv[i][0] == '%')
        {
          strcat(link, "'");
	    if (argv[i][1] == 'a') strcat(link, num);
	    if (argv[i][1] == 'd') strcat(link, desc);
	    if (argv[i][1] == 'q') { sprintf(tmp, "%d", quality); strcat(link, tmp); }
	    if (argv[i][1] == 'n') strcat(link, name);
	    if (argv[i][1] == 'p') strcat(link, pic);
	    if (argv[i][1] == 'm') { sprintf(tmp, "%d", money); strcat(link, tmp); }
	    if (argv[i][1] == 'f') strcat(link, from);
	    if (argv[i][1] == 'w') strcat(link, when);
          strcat(link, "' ");
        } else {
	  strcat(link, argv[i]);
        strcat(link, " ");
        }
      }
      strcat(link, "\n");
    
      //Put link into index.html
      fprintf(out, link);
      fscanf(in, "%s", buf);
    }
  }

  printf("Parsing is complete. Your html file is: %s\n", OUTPUT);
  fclose(in);
  fclose(out);
  return 0;
}

