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

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>
#include "SxPath.h"
#include "Buffer.h"
#include "MediaWiki.h"

int getopt();
extern char *optarg; 

char outputCode; /* parameter o : p: wiki page 
                                  m: modele page
                                  s: source page
                                  a: all
                                  M: m+s */ 

SxmlNode *docInput;
SxmlNode *inistPart;
SxmlNode *standardPart;
SxmlNode *inistPA;
SxmlNode *serverPart;

SxmlNode *InistFA08;

MediaWikiParser *parserTable;

StrDict *InistToWikiParamTable;

char     *refPageName;
char     *refModeleName;
char     *refSourceName;
char     *refModeleSource;
char     *operatorName;

Buffer *bufModeleName=NULL;
Buffer *bufSourceName=NULL;

int flagPage;
int flagModele;
int flagSource;

SxmlNode *listCategory;
SxmlNode *listCategoryModele;
SxmlNode *listCategorySource;
SxmlNode *listCategoryPage;

char *bandeauModele;
char *bandeauSource;
char *bandeauPage;
char *bandeau;

void generPageModele()
{
  SxmlNode *serverField;
  MediaWikiExportPageBegin(refModeleName, operatorName, NULL);
  
  printf("&lt;noinclude&gt;\n");
  if (bandeauModele)
    {
      printf("{{%s\n", bandeauModele);
      printf(" |texte=");
      printf("Ce modèle est généré par des moyens automatiques.\n\n");
      printf("Il est utilisé par la page [[%s]].\n\n", refPageName);
      printf("}}\n");
    }
  if (listCategoryModele)
    {
      SxmlNode *cat;
      SxmlReset(listCategoryModele);
      while((cat=SxmlNextNode(listCategoryModele)))
	{
	  printf("[[Catégorie:%s]]\n", SxmlLeafText(cat));
	}
    }
  printf("&lt;/noinclude&gt;&lt;includeonly&gt;");
  printf("{{#if:{{{ks|}}}|{{#switch:{{{ks}}}\n");
  SxmlReset(serverPart);
  while((serverField=SxmlNextNode(serverPart)))
    {
      printf("  |%s=", SxmlNodeName(serverField));
      MediaWicriExplorPrintElementContent(serverField);
      printf("\n");
    }
  printf("}}}}");
  printf("&lt;/includeonly&gt;");
  
  MediaWikiExportPageEnd();
}

void generPageWiki()
{
      MediaWikiExportPageBegin(refPageName, operatorName, NULL);

      if ((InistFA08)&&(SxmlHasAttribute(InistFA08, "l", "FRE")))
	{
	  printf("{{Titre page article\n  |titre=%s\n}}\n", SxmlLeafText(SxmlGetFirstChildByTagName(InistFA08,"s1")));
	}
      printf("{{Wicri démonstration|texte=Cette page est initialisée par des moyens automatiques.\n\n");
      printf("Elle utilise la page [[%s]].}}\n\n", refModeleName);
      
      printf("{{Boîte bibliographique droite|\n");
      printf(";Titre:{{%s|ks=FT}}\n", refPageName);
      printf(";Auteurs:{{%s|ks=AU}}\n", refPageName);
      printf(";Source:{{%s|ks=SO}}\n", refPageName);
      printf("}}\n");
      printf("==Résumé==\n");
      printf("{{%s|ks=FA}}\n", refPageName);
      printf("==Indexation Francis==\n");
      printf("{{%s|ks=FD}}\n", refPageName);
      printf ("==Voir aussi==\n;Sur ce wiki:[[Wicri:%s|la fiche source de référence]]\n", refPageName);


  if (listCategoryPage)
    {
      SxmlNode *cat;
      SxmlReset(listCategoryPage);
      while((cat=SxmlNextNode(listCategoryPage)))
	{
	  printf("[[Catégorie:%s]]\n", SxmlLeafText(cat));
	}
    }
      MediaWikiExportPageEnd();
}

void generPageSource()
{
  SxmlNode *part;
      MediaWikiExportPageBegin(refSourceName, operatorName, NULL);

      printf("Cette page a été initialisée par des moyens automatiques.\n\n");
      printf("{| class=\"wikitable\"\n");
      
      SxmlReset(standardPart);
      while ((part=SxmlNextNode(standardPart)))
	{
	  SxmlNode *field;
	  while ((field=SxmlNextNode(part)))
	    {	     
	      char *i1;
	      char *i2;
	      char *lang;
	      SxmlNode *sf;
	      i1=SxmlGetAttribute(field, "i1");
	      i2=SxmlGetAttribute(field, "i2");
	      lang=SxmlGetAttribute(field, "l");
	      printf("|-\n");
	      printf("|%s\n|", SxmlNodeName(field)+1);
	      if (i1||i2)
		{
		  if(i1)printf("%s",i1);
		  else printf("..");
		  if(i2)printf("%s",i2);
		}
	      if (lang) printf("\n|%s\n", lang);
	      else  printf("\n|\n");
	      sf=SxmlFirstChild(field);
	      printf("|@%s\n", SxmlNodeName(sf)+1);
	      printf("|%s\n", SxmlLeafText(sf));
	      sf=SxmlNextSibling(sf);
	      if(sf)
		{
		  printf("|");
		  while(sf)
		    {
		      printf("\'\'\'@%s\'\'\' %s", SxmlNodeName(sf), SxmlLeafText(sf));
		      sf=SxmlNextSibling(sf);
		      if (sf) printf(" ; ");
		    }
		  printf("\n");
		}
	      else printf("|\n");
	    }
	}
      printf("|}\n");

  if (listCategorySource)
    {
      SxmlNode *cat;
      SxmlReset(listCategorySource);
      while((cat=SxmlNextNode(listCategorySource)))
	{
	  printf("[[Catégorie:%s]]\n", SxmlLeafText(cat));
	}
    }

      MediaWikiExportPageEnd();
}

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

  int cOption;

  bufModeleName=NewBuffer();
  bufSourceName=NewBuffer();
  parserTable=NewMediaWikiParser();

  operatorName="Wicri";
  bandeauModele=NULL;
  InistToWikiParamTable=NULL;

  outputCode='m';

  while((cOption=getopt(argc,argv,"o:t:"))!=EOF) 
    {
      {switch (cOption)
	  { 
	  case 'o':
	    outputCode=optarg[0];
	    break;
	  case 't':
	    InistToWikiParamTable=StrDictFromFile(optarg);
	    break;
	  }
      }
    }

  listCategoryModele=NULL;
  if(InistToWikiParamTable)
    {
      char *tabVal;
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Opérateur"))) operatorName=tabVal;
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Bandeau"))) 
	{
	  bandeau=tabVal;
	  bandeauModele=bandeau;
	  bandeauSource=bandeau;
	  bandeauPage=bandeau;
	}
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Bandeau (modèle)"))) bandeauModele=tabVal;
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Bandeau (page)")))   bandeauPage=tabVal;
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Bandeau (source)"))) bandeauSource=tabVal;
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Catégorie"))) 
	{
	  listCategory=MediaWikiSxmlListFromStr(parserTable, tabVal);
	  listCategoryPage=listCategory;
	  listCategorySource=listCategory;
	  listCategoryModele=listCategory;
	}
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Catégorie (modèle)"))) 
	{
	  listCategoryModele=MediaWikiSxmlListFromStr(parserTable, tabVal);
	}
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Catégorie (page)"))) 
	{
	  listCategoryPage=MediaWikiSxmlListFromStr(parserTable, tabVal);
	}
      if ((tabVal=StrDictSearch(InistToWikiParamTable,"Catégorie (source)"))) 
	{
	  listCategorySource=MediaWikiSxmlListFromStr(parserTable, tabVal);
	}
    }

  flagModele=0;
  flagSource=0;
  flagPage=0;
  switch(outputCode)
    {
    case 'a': flagPage=1;
    case 'M': flagSource=1;
    case 'm': flagModele=1; break;
    case 'P': flagModele=1;
    case 'p': flagPage=1; break;
    case 's': flagSource=1; break;
    }

  MediaWikiExportFileBegin();

  while ((docInput=SxmlInputNextDocumentElement()))
    {
      inistPart=NULL;
      serverPart=NULL;
      if (SxmlNodeHasName(docInput,"inist")) inistPart=docInput;
      if (!inistPart) inistPart=SxmlGetFirstChildByTagName(docInput,"inist");
      if (!inistPart) exit (EXIT_FAILURE);
      serverPart=SxmlGetFirstChildByTagName(inistPart,"server");
      if (!serverPart) exit (EXIT_FAILURE);
      standardPart=SxmlGetFirstChildByTagName(inistPart,"standard");
      if (!standardPart) exit (EXIT_FAILURE);
      inistPA=SxmlGetFirstChildByTagName(standardPart,"pA");
      if (!inistPA) exit (EXIT_FAILURE);
      refPageName=SxmlLeafText(SxmlGetFirstChildByTagName(serverPart,"NO"));
      if (!refPageName) exit (EXIT_FAILURE);

      InistFA08=SxmlGetFirstChildByTagName(inistPA,"fA08");

      BufferStrcpy (bufModeleName, "Modèle:");
      BufferStrcat (bufModeleName, refPageName);
      refModeleName=BufferString(bufModeleName);
      BufferStrcpy (bufSourceName, "Wicri:");
      BufferStrcat (bufSourceName, refPageName);
      refSourceName=BufferString(bufSourceName);

      if (flagModele==1) generPageModele();
      if (flagPage==1)  generPageWiki();
      if (flagSource==1)  generPageSource();
    }

  MediaWikiExportFileEnd();

  exit(EXIT_SUCCESS);
}
