/*   -*- coding: utf-8 -*-  */
/***********************************************************************
*
*               Module   : Tei
*               Fichier  : TeiPutRefToIdno.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"
#include "Hcs.h"
#include "BufferParser.h"

    int getopt();
    extern char *optarg;


SxmlNode *docInput;

int mode;   /*  i : insert ; u : update */

int main (int argc, char **argv) {
  Buffer *bKey;
  char bPrint[20];
  char *beginKey;

  int modeHcs;
  int numRec;
  int lKeyNum;
  int cod_arg;
  char *type;
  SxmlNode *hcs;
  char *stream;
  char *step;
  char *corpus;

  beginKey=NULL;
  numRec=0;
  lKeyNum=6;
  bKey=BufferCreate(10,10);
  modeHcs=1;
  stream=NULL;
  step=NULL;
  corpus=NULL;
  
  while ((cod_arg = getopt(argc,argv,"c:S:s:b:t:u:"))!=EOF)
    {switch(cod_arg) 
      {
      case 'b':
	modeHcs=0;
	beginKey=optarg;
	break;
      case 'c':
	corpus=optarg;
	break;
      case 'S':
	stream=optarg;
	mode='i';
	break;
      case 's':
	step=optarg;
	break;
      case 't':
	mode ='i';
	type=optarg;
	break;
      case 'u':
	mode ='u';
	type=optarg;
	break;
      }
    }

  if (modeHcs==1)
    {
      char *ret;
      hcs=HcsImplicitHcs();
      ret=HcsGetFirstKeyBuffer(hcs, bKey);
      if(!ret)
	{
	  perror("****  PB in TeiPutRetToIdno (hcs)  ****");
	  exit (EXIT_FAILURE);
	}
    }

  while ((docInput=SxmlInputNextDocumentElement()))
     {   
       SxmlNode *idno;
       char *newKey;

       TeiHandlerInit(docInput);
       if (modeHcs==0)
	 {
	   if (beginKey)BufferStrcpy(bKey,beginKey);
	   else BufferReset(bKey);
	   sprintf(bPrint,"06%d",numRec++);
	   BufferStrcat(bKey,bPrint);
	 }
       newKey=BufferString(bKey);
       if (strlen(newKey)<5)
	 {
	  perror("****  PB in TeiPutRetToIdno 1  ****");
	  fprintf(stderr, "====%s===\n", newKey);
	  fprintf(stderr, "%s\n", SxmlToString(hcs));
	  exit (EXIT_FAILURE);
	 }
       if (mode=='i') 
	 {
	   SxmlAppendChild (Tei_publicationStmt, idno=SxmlLeafCreate ("idno", newKey)); 
	   SxmlSetAttribute(idno,"type",type); 
	   if (stream)
	     {
	       SxmlAppendChild (Tei_publicationStmt, idno=SxmlLeafCreate ("idno", newKey)); 
	       SxmlSetAttribute(idno,"type","wicri:explorRef");
	       SxmlSetAttribute(idno,"wicri:stream",stream);
	       SxmlSetAttribute(idno,"wicri:step",step);
	       if (corpus) SxmlSetAttribute(idno,"wicri:corpus", corpus);
	     }
	 }
       else
	 {
	   SxmlNode *oldIdno;
	   oldIdno=SxmlGetFirstChildTagAtt(Tei_publicationStmt, "idno", "type" , type);
	   if (!oldIdno) 
	     {
	       perror("****  PB  TeiPutRetToIdno with m option  ****");
	       exit (EXIT_FAILURE);
	     }
	   SxmlLeafSetText(oldIdno, newKey);
	 }
       SxmlPrint (SxmlInputRecord);
       if (modeHcs==1)HcsGetNextKeyBuffer(hcs, bKey);
     }
  exit(EXIT_SUCCESS);
}
