/*   -*- coding: utf-8 -*-  */

#include "SxmlNode.h"
#include "MediaWiki.h"
#include "Explor.h"
#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>
#include <time.h>

SxmlDocumentReader *ExplorSortReader;
SxmlNode *sortPays;
SxmlNode *sortRegion;

void ExplorGenerTemplateIndexList(SxmlNode *indexRoot, char *indexCode)
{
 SxmlNode *itemNode;
 char *kw;
 char *f;
 SxmlReset(indexRoot);
  while((itemNode=SxmlNextNode(indexRoot)))
    {
      kw=SxmlLeafText(SxmlGetFirstChildByTagName(itemNode,"kw"));
      f=SxmlLeafText(SxmlGetFirstChildByTagName(itemNode,"f"));
      printf("* [[%s]] ", kw);
      printf("[{{Explor lien/Wiki|%s}}/%s/Site/fr/Main/Exploration/indexItem.php?index=%s", ExplorWikiCode, ExplorAreaCode, indexCode);
      printf("&amp;key=%s ",MediaWikiNameToUrlLink(kw));
      printf("%s]\n", f);
    }
}

int main(int argc, char **argv)
{
  char *pathParamFile;
  int cOption;
  Buffer *bufPath;
  SxmlNode *sortNode;
  int i;
  ExplorStreamCode="Main";
  pathParamFile=NULL;
  bufPath=NewBuffer();
  ExplorSortReader=SxmlRecordReaderCreate();
   while((cOption=getopt(argc,argv,"s:p:"))!=EOF)
     {switch (cOption)
	 {
	 case 's':
	   ExplorStreamCode=optarg;
	   break;
	 case 'p':
	   pathParamFile=optarg;
	   break;
	 }
     }
   if (!pathParamFile)
     {
       BufferStrcpy(bufPath, getenv("EXPLOR_AREA"));
       BufferStrcat(bufPath, "/Input/AreaParam.data.tab");
       pathParamFile=BufferString(bufPath);
     }
   ExplorParamInit(pathParamFile);
  MediaWikiExportFileBegin();
  BufferStrcpy(bufPath,"Modèle:Explor plateforme ");
  BufferStrcat(bufPath, ExplorAreaCode);
  if (strcmp(ExplorStreamCode, "Main")!=0)
    {
      BufferStrcat(bufPath,", ");
      BufferStrcat(bufPath, ExplorStreamCode);
    }
  BufferStrcat(bufPath,"/Top 10");
  MediaWikiExportPageBegin(BufferString(bufPath), getenv("USER"), NULL);
  printf("&lt;noinclude&gt;\n");
  sortPays=SxmlElementCreate("pays");
  BufferStrcpy(bufPath,ExplorAreaDir);
  BufferStrcat(bufPath, "/Data/Main/Exploration/AffPays.i.sort");
  SxmlRecordReaderOpen(ExplorSortReader, BufferString(bufPath));
  i=10;
  while((i--)&& (sortNode=SxmlRecordReaderNextDocumentElement(ExplorSortReader)))
    SxmlAppendChild(sortPays, SxmlRemoveChild(sortNode));
  SxmlRecordReaderClose(ExplorSortReader);
  sortRegion=SxmlElementCreate("region");
  BufferStrcpy(bufPath,ExplorAreaDir);
  BufferStrcat(bufPath, "/Data/Main/Exploration/AffRegion.i.sort");
  SxmlRecordReaderOpen(ExplorSortReader, BufferString(bufPath));
  i=10;
  while((i--)&& (sortNode=SxmlRecordReaderNextDocumentElement(ExplorSortReader)))
    SxmlAppendChild(sortRegion, SxmlRemoveChild(sortNode));
  printf("{|\n|-\n|Pays\n|Région\n|-\n|\n");
  /*
  SxmlReset(sortPays);
  while((sortNode=SxmlNextNode(sortPays)))
    {
      printf("* [[%s]] [%s]\n", SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"kw")), 
	     SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"f")));
    }
  */
  ExplorGenerTemplateIndexList(sortPays, "AffPays.i");
  printf("|\n");
  SxmlReset(sortRegion);
  while((sortNode=SxmlNextNode(sortRegion)))
    {
      printf("* [[%s]] [%s]\n", SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"kw")), 
	     SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"f")));
    }
  printf("|-\n|}\n");
  printf("&lt;/noinclude&gt;&lt;includeonly&gt;");
  printf("[{{Explor lien/Wiki|%s}}/%s//Site/fr/Main/Exploration/indexItem.php?index=",ExplorWikiCode, ExplorAreaCode );
  printf("{{#switch:{{{clé|}}}\n");
  SxmlReset(sortPays);
  while((sortNode=SxmlNextNode(sortPays)))
    {
      char *kw;
      kw=SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"kw"));
      printf("   |%s=AffPays.i&amp;key=%s %s\n", kw, MediaWikiNameToUrlLink(kw), SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"f")));
    }
  SxmlReset(sortRegion);
  while((sortNode=SxmlNextNode(sortRegion)))
    {
      char *kw;
      kw=SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"kw"));
      printf("   |%s=AffRegion.i&amp;key=%s %s\n", kw, MediaWikiNameToUrlLink(kw), SxmlLeafText(SxmlGetFirstChildByTagName(sortNode,"f")));
    }
  printf("}} {{{après|}}}]");
  printf("&lt;/includeonly&gt;&lt;noinclude&gt;");
  MediaWikiExportPageEnd();
  MediaWikiExportFileEnd();
  exit(EXIT_SUCCESS);
}
