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

/*
   This programs converts results from a Query on ArXiv (this file must have been formatted by SxmlUnIndent
 */

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

SxmlNode *docInput;

void computeAuthor(SxmlNode *authorArxiv)
{
  SxmlNode *newAuthor;
  SxmlNode *newAuthorName;
  SxmlNode *authorName;
  SxmlNode *arxivAffiliation;
  char      *authorStr;
  authorName=SxmlGetFirstChildByTagName(authorArxiv, "name");
  if (!authorName)return;
  SxmlAppendChild(Tei_analytic, newAuthor=SxmlElementCreate("author"));
  authorStr=SxmlLeafText(authorName);
  if ((newAuthorName=TeiAuthorNameFromNameString(authorStr)))
    {
      SxmlAppendChild(newAuthor, newAuthorName);
    }
  arxivAffiliation=SxmlGetFirstChildByTagName(authorArxiv, "arxiv:affiliation");
  if (arxivAffiliation)
    {
      SxmlNode *teiAffiliation;
      SxmlAppendChild(newAuthor, teiAffiliation=SxmlElementCreate("affiliation"));
      SxmlAppendChild(teiAffiliation, SxmlClone(arxivAffiliation));
    }
}

void createTeiDoc(SxmlNode *entry)
{
  SxmlNode *outputRecord;
  SxmlNode *teiRoot;
  SxmlNode *titleArxiv;
  SxmlNode *idArxiv;
  SxmlNode *authorArxiv;
  SxmlNode *dateArxiv;
  outputRecord=SxmlElementCreate("record");
  SxmlAppendChild(outputRecord, teiRoot=TeiHandlerNew());
  TeiAppendIdno("wicri:source","ArXiv");
  SxmlSetAttribute(teiRoot, "xmlns:aexiv", "http://axiv.prg");
  if ((titleArxiv=SxmlGetFirstChildByTagName(entry,"title")))
    {
      TeiHandlerSetAnalyticStmtTitleStr(SxmlLeafText(titleArxiv), "en");
    }
  if ((idArxiv=SxmlGetFirstChildByTagName(entry,"id")))
    {
      char *beginId;
      beginId=strstr(SxmlLeafText(idArxiv), "abs/");
      if (beginId) TeiSetIdnoRbid("ArXiv", beginId+4);
    }
  authorArxiv=SxmlGetFirstChildByTagName(entry,"author");
  while (authorArxiv)
    {
      computeAuthor(authorArxiv);
      authorArxiv=SxmlGetNextSiblingByTagName(authorArxiv,"author");
    }
  if ((dateArxiv=SxmlGetFirstChildByTagName(entry, "published")))
    {
      static Buffer* bufCut=NULL;
      SxmlNode *newDate;
      char *dateStr;
      char *posT;
      if (!bufCut)bufCut=NewBuffer();
      BufferStrcpy(bufCut, SxmlLeafText(dateArxiv));
      dateStr=BufferString(bufCut);
      posT=strchr(dateStr,'T');
      if(posT) *posT='\0';
      newDate=TeiSetDatePublication(dateStr);
    }
  
  SxmlRemoveChild(entry);
  SxmlAppendChild(outputRecord, entry);
  SxmlSetNodeName(entry,strdup("ArXiv")); /* strdup pour SxmlFree() */
  SxmlPrint(outputRecord);
  putchar ('\n');
}

int main()
{
  while ((docInput=SxmlInputNextDocumentElement()))
    {
      SxmlNode *entry;
      if (!SxmlNodeHasName(docInput, "feed"))continue;
      entry=SxmlGetFirstChildByTagName(docInput,"entry");
      while (entry) 
	{
	  SxmlNode *nextEntry;
	  nextEntry=SxmlGetNextSiblingByTagName(entry, "entry");
	  createTeiDoc(entry);
	  entry=nextEntry;
	}
    }
  exit (EXIT_SUCCESS);
}
