/*   -*- coding: utf-8 -*-  */
/***********************************************************************
*
*               Module   : Tei
*               Fichier  : TeiPutLinkByRbid.c
*               Auteur   : Ducloy
*               Date     : Février 2013
*
************************************************************************
*
* Copyleft 2013 Jacques Ducloy 
* 
************************************************************************/

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


    int getopt();
    extern char *optarg;

StrDict *linkTable;

SxmlNode *docInput;

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

  while ((cod_arg = getopt(argc,argv,"t:"))!=EOF)
    {switch(cod_arg) 
      {
      case 't':linkTable=StrDictFromFile(optarg);
	  break;
      }
    }

  while ((docInput=SxmlInputNextDocumentElement()))
     {   
       SxmlNode *idno;
       char *rbid;
       char *linkStr;
       TeiHandlerInit(docInput);
       rbid= TeiGetIdnoRbid();
       if ((linkStr =StrDictSearch(linkTable, rbid)))
	 {
	   SxmlAppendChild (Tei_publicationStmt, idno=SxmlLeafCreate ("idno", linkStr));
	   SxmlSetAttribute(idno,"type","url"); 
	 }
       SxmlPrint (SxmlInputRecord);
     }
  exit (EXIT_SUCCESS);
}
