/* Input : A file containing lines, each with a word count,
      white space, and path to an HTML files, starting with "./" 
   Output: same, but with a prepended ISO language code in front,
      and an appended title. 
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/stat.h>
#include "getfhtml.h"

int main (int argc, char **argv)
{
   static char count_and_path[1024];
   FILE *fpi = stdin;
   FILE *fpo = stdout;

   while (fgets(count_and_path, sizeof count_and_path, fpi))
   {
      char lang[3+1+2+1+4+1];
      char *p, *htmlpath;

      p = strrchr(count_and_path, '\n');
      if (p)
         *p = '\0';

      htmlpath = strstr(count_and_path, "./");
      if (!htmlpath || htmlpath < count_and_path + 1 || !isspace(htmlpath[-1]))
         continue;

      strcpy(lang, "??");
      p = gfh_GetFromPath(htmlpath, 1 << 10, "html", "lang");
      if (p)
         strcpy(lang, p);

      p = gfh_GetFromPath(htmlpath, 1 << 10, "title", "");

      fprintf(fpo, "\t<tr> <td>%s</td> <td><a href=\"../%s\">%s</a></td> <td>%s</td></tr>\n",
         lang, htmlpath + 2, p ? p : "NO TITLE FOUND", count_and_path);
   }

   return 0;
}
