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

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>
#include "SxmlNode.h"
#include "StrDict.h"
#include "TeiHandler.h"
#include "Utf8Text.h"

SxmlNode *docInput;
SxmlNode *corpusRecord;
SxmlNode *teiRoot;

char *bufNum;

char *pubDate;
void computeAbstract()
{
  SxmlNode *abstractNode;
  if((abstractNode=SxmlGetFirstChildByTagName(docInput, "abstract")))
    {
       int scoreLang;
       SxmlNode *targetAbs;
       char *lang;
       if (!SxmlIsLeaf(abstractNode)) return;
       scoreLang=Utf8LangEvalEngFre(SxmlLeafText(abstractNode));
       if (scoreLang>0) lang="en"; else lang="fr";
       targetAbs=TeiHandlerStoreAbstract(abstractNode, lang);
       sprintf(bufNum, "%d", scoreLang);
       SxmlSetAttribute(targetAbs, "wicri:score", bufNum);
    }
}

void computeOneAuthor(char *strAut)
{
  SxmlNode *newAuthor;
  SxmlNode *newName;
  newName=TeiAuthorNameFromBibString(strAut);
  if (!newName)return;
  SxmlAppendChild(Tei_analytic, newAuthor=SxmlElementCreate("author"));
  SxmlAppendChild(newAuthor, newName);
}

void computeAuthors()
{
  SxmlNode *authorNode;
  if((authorNode=SxmlGetFirstChildByTagName(docInput, "author")))
    {
      SxmlNode *eNode;
      SxmlReset(authorNode);
      while ((eNode=SxmlNextNode(authorNode)))
	{
	  computeOneAuthor(SxmlLeafText(eNode));
	}
    }
}

void computeOneKeywords(char *kw)
{
   TeiKeywordsPutTerm("KwdEn", NULL, NULL, "en", kw);
}

void computeKeywords()
{
  SxmlNode *keywordsNode;
  if((keywordsNode=SxmlGetFirstChildByTagName(docInput, "keywords")))
    {
      SxmlNode *eNode;
      SxmlReset(keywordsNode);
      while ((eNode=SxmlNextNode(keywordsNode)))
	{
	  computeOneKeywords(SxmlLeafText(eNode));
	}
    }
}

void computeDate()
{
  SxmlNode *yearNode;
  if(( yearNode=SxmlGetFirstChildByTagName(docInput, "year")))
    TeiSetDatePublication(pubDate=SxmlLeafText(yearNode));
}
void computeId()
{
  SxmlNode *refNode;
  refNode=SxmlGetFirstChildByTagName(docInput, "ref");
  TeiSetIdnoRbid("CRIN", SxmlLeafText(refNode));
}
void computeTitle()
{
  SxmlNode *bibTexTitleNode;
  char *bibTexTitle;
  int scoreLang;
  SxmlNode *targetTitle;
  bibTexTitleNode=SxmlGetFirstChildByTagName(docInput, "title");
  bibTexTitle=SxmlLeafText(bibTexTitleNode);
  scoreLang=Utf8LangEvalEngFre(bibTexTitle);
  if (scoreLang>0) targetTitle=TeiHandlerSetAnalyticStmtTitleStr(bibTexTitle, "en");
  else targetTitle=TeiHandlerSetAnalyticStmtTitleStr(bibTexTitle, "fr");
  sprintf(bufNum, "%d", scoreLang);
  SxmlSetAttribute(targetTitle, "wicri:score", bufNum);
}

void computeArticle()
{
  SxmlNode *journalNode;
  SxmlNode *teiTitle;
  if ((journalNode=SxmlGetFirstChildByTagName(docInput, "journal")))
    {
      if (!Tei_series)SxmlAppendChild(Tei_biblStruct, Tei_series=SxmlElementCreate("series"));
      SxmlAppendChild(Tei_series, teiTitle=SxmlLeafCreate("title", SxmlLeafText(journalNode)));
      SxmlSetAttribute(teiTitle, "level", "j");
      TeiSeriesSetDatePublication(pubDate);
    }
}

void computeBiblio()
{
  char *type;
  type=SxmlGetAttribute(docInput,"type");
  if (strcmp(type, "article")==0) computeArticle();
}

int main(int argc, char **argv)
{
  bufNum=malloc(10);
  while ((docInput=SxmlInputNextDocumentElement()))
    {
      SxmlNode *docu;
      corpusRecord=SxmlElementCreate("record");
      SxmlAppendChild(corpusRecord, teiRoot=TeiHandlerNew());
      docu=SxmlClone(docInput);
      SxmlSetNodeName(docu,strdup("BibTex")); /* strdup pour SxmlFree() */
      SxmlAppendChild(corpusRecord,docu);
      computeId();
      computeAbstract();
      computeTitle();
      computeDate();
      computeAuthors();
      computeKeywords();
      computeBiblio();
      SxmlPrint(corpusRecord);
      putchar('\n');
      SxmlFree(corpusRecord);
    }  
}
