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

/*
           InistExodic2Tei.c

 */

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


#include "Buffer.h"
#include "Utf8Converter.h"
#include "Explor.h"

SxmlNode *inputDocument;
SxmlNode *inputClone;
SxmlNode *corpusRecord;
SxmlNode *teiRoot;
static Buffer *bufName;

SxmlNode *exodicRights;
SxmlNode *exodicSource;
SxmlNode *exodicDescription;
int codeBase;
/*
    B : BHA
 */

void exodicComputeCodeBase()
{
  SxmlNode *sourceItem;
  if ((exodicRights=SxmlGetFirstChildByTagName(inputDocument, "rights"))
      && (sourceItem=SxmlGetFirstChildByTagName(exodicRights, "sourceitem"))
      && (strcmp (SxmlLeafText(sourceItem), "BHA")==0))
    {
      codeBase='B';
    }
  else codeBase='\0';
}

void exodicComputeIdno()
{
  SxmlNode *exodicIdentifier;
  char *inistId;
  static Buffer *bufIdno=NULL;

  if (!bufIdno)bufIdno=NewBuffer();
  if ((exodicIdentifier=SxmlGetFirstChildByTagName(inputDocument, "identifier")))
    {
      if ((inistId=SxmlGetAttribute(exodicIdentifier, "inist")))
	{
	  if (codeBase=='B')
	    {
	      BufferStrcpy(bufIdno,"InistBha:");
	      BufferStrcat (bufIdno, inistId);
	      TeiAppendIdno("RBID", BufferString(bufIdno));
	    }
	}
    }
}

void exodicComputeISSN()
{
  SxmlNode *exodicIdentifier;
  char *issn;

  if ((exodicIdentifier=SxmlGetFirstChildByTagName(inputDocument, "identifier")))
    {
      if ((issn=SxmlGetAttribute(exodicIdentifier, "issn")))
	{
	  TeiHandlerSetIssn(issn);
	}
    }
}

void exodicComputeTitle()
{
  SxmlNode *exodicTitle;
  SxmlNode *exodicTtl;
  SxmlNode *titleTei;
  if ((exodicTitle=SxmlGetFirstChildByTagName(inputDocument,"title")))
    {
      if ((exodicTtl=SxmlGetFirstChildByTagName(exodicTitle, "ttl")))
	{
	  char *lang;
	  /* SxmlAppendChild(Tei_titleStmt, titleTei=SxmlLeafCreate ("title", SxmlLeafText(exodicTtl))); */
	  lang=SxmlGetAttribute(exodicTtl, "lang");
	  TeiHandlerSetAnalyticStmtTitleStr(SxmlLeafText(exodicTtl), TeiLangC3ToC2(lang));
	}
    }
}

void exodicComputeCreator()
{
  SxmlNode *exodicCreator;
  SxmlNode *exodicAuthor;
  SxmlNode *teiName;
  SxmlNode *teiAuthor;
  if ((exodicCreator=SxmlGetFirstChildByTagName(inputDocument,"creator")))
    {
      while ((exodicAuthor=SxmlNextNode(exodicCreator)))
	{
	  SxmlNode *firstname;
	  SxmlNode *surname;
	  if ((firstname=SxmlGetFirstChildByTagName(exodicAuthor, "firstname")))
	    {
	      if ((surname=SxmlGetFirstChildByTagName(exodicAuthor, "surname")))
		{
		  teiAuthor=SxmlElementCreate("author");
		  SxmlAppendChild(Tei_titleStmt,teiAuthor);
		  BufferStrcpy(bufName, SxmlLeafText(firstname));
		  BufferStrcat(bufName, " ");
		  BufferStrcat(bufName, SxmlLeafText(surname));
		  SxmlAppendChild(teiAuthor, teiName=SxmlLeafCreate("name", BufferString(bufName)));
		  SxmlAppendChild(Tei_analytic, SxmlClone(teiAuthor));
		}
	    }
	}
    }
}

void exodicComputeSubject()
{
  SxmlNode *exodicSubject;
  SxmlNode *keyOrClass;
  if ((exodicSubject=SxmlGetFirstChildByTagName(inputDocument,"subject")))
    {
      while ((keyOrClass=SxmlNextNode(exodicSubject)))
	{
	  if (SxmlNodeHasName(keyOrClass,"keyword"))
	    {
	      SxmlNode *kwd;
	      while ((kwd=SxmlNextNode(keyOrClass)))
		{
		  char *lang;
		  lang=SxmlGetAttribute(kwd, "lang");
		  if (lang[0]=='f') TeiKeywordsPutTerm("BHA",  NULL, NULL, "fr", SxmlLeafText(kwd));
		  else TeiKeywordsPutTerm("KwdEn", NULL , NULL, "en", SxmlLeafText(kwd));
		}
	    }	      
	}
    }
}

void exodicComputeDatePub()
 {
   SxmlNode *issue;
   SxmlNode *year;
   SxmlNode *teiDate;
   if (exodicSource)
     {
       if ((issue=SxmlGetFirstChildByTagName(exodicSource,"issue")))
	 {
	   if ((year=SxmlGetFirstChildByTagName(issue,"year")))
	     {
	       SxmlAppendChild(Tei_publicationStmt, teiDate=SxmlLeafCreate("date", SxmlLeafText(year)));
	       SxmlSetAttribute(teiDate, "when", SxmlLeafText(year));
	     }
	 }

     }
 }

void exodicComputeAbstract()
{
  SxmlNode *absNode;
  if (!exodicDescription) return;
  SxmlReset(exodicDescription);
  while ((absNode=SxmlNextNode(exodicDescription)))
    {
      char *lang;
      if (!SxmlNodeHasName(absNode,"abstract")) continue;
      lang=SxmlGetAttribute(absNode,"lang");
      TeiHandlerStoreAbstract(absNode, TeiHandlerLang(lang));
    }
}

void exodicComputeSeriesStmt()
{
  SxmlNode *source;
  exodicComputeISSN();
  if ((source= SxmlGetFirstChildByTagName(inputDocument, "source")))
    {
      SxmlNode *journal;
      SxmlNode *issue;
      SxmlNode *imprint;

      imprint=NULL;
      journal= SxmlGetFirstChildByTagName(source, "journal");
      while (journal)
	{
	  char *tit;
	  char *type;
	  tit=SxmlLeafText(journal);
	  type=SxmlGetAttribute(journal, "type");
	  TeiHandlerSetSeriesTitle(tit, "j", type);
	  journal= SxmlGetNextSiblingByTagName(journal, "journal");
	}
      issue= SxmlGetFirstChildByTagName(source, "issue");
      if (issue)
	{
	  SxmlNode *year;
	  year= SxmlGetFirstChildByTagName(issue, "year");
	  if (year)
	    {
	      SxmlNode *teiDate;
	      char *dateYear;
	      dateYear=SxmlLeafText(year);
	      if (!imprint) SxmlAppendChild (Tei_series, imprint=SxmlElementCreate("imprint"));
	      SxmlAppendChild (imprint, teiDate=SxmlLeafCreate("date", dateYear ));
	      SxmlSetAttribute(teiDate, "when", dateYear);
	      SxmlSetAttribute(teiDate, "type", "published");
	    }
	}
    }
}

int main (int argc, char **argv) {
  int  cOption;
  char *pathParamFile;

  bufName=BufferCreate(10,10);

   while((cOption=getopt(argc,argv,"s:t:"))!=EOF)
     {switch (cOption)
	 {
	 case 's':
	   ExplorStreamCode=optarg;
	   break;
	 case 't':
	   pathParamFile=optarg;
	   break;
	 }
     }

   ExplorParamInit(pathParamFile);  

   while ((inputDocument=SxmlInputNextDocumentElement()))
     {
       corpusRecord=SxmlElementCreate("record");
       SxmlAppendChild(corpusRecord, teiRoot=TeiHandlerNew());
       SxmlSetAttribute(teiRoot, "xmlns:inist", "http://www.inist.fr");
       inputClone=SxmlClone(inputDocument);
       SxmlSetNodeName(inputClone ,strdup("exodic")); /* strdup pour SxmlFree() */
       SxmlAppendChild(corpusRecord, inputClone);

       exodicSource=SxmlGetFirstChildByTagName(inputDocument,"source");
       exodicDescription=SxmlGetFirstChildByTagName(inputDocument,"description");

       exodicComputeCodeBase();
       exodicComputeIdno();
       exodicComputeTitle();
       exodicComputeCreator();
       exodicComputeSubject();
       exodicComputeAbstract();
       exodicComputeDatePub();
       exodicComputeSeriesStmt();

       SxmlPrint(corpusRecord);
       putchar('\n');
       SxmlFree(corpusRecord);
     }
   exit(EXIT_SUCCESS);
}
