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

/**********************************************************************
*
*  module   : Nlm
*  fichier  : NlmCurationCountry.c 
*  Auteur   : Jacques DUCLOY
*  Date     : fin 2013
*
***********************************************************************/

#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>
#include "SxPath.h"
#include "Buffer.h"
#include "WicriPaysFromEn.h"
#include "WicriRegionFromEn.h"
#include "WicriFrance.h"
#include "WicriAllemagne.h"
#include "Wicri.h"
#include "RegExp.h"

SxmlNode * affiliation;
SxmlNode *docInput;
SxmlNode *listAffiliations;

/* RegExp *WicriRegExpZip=NULL; */

SxPathResult *teiPathAffiliation;
SxPathResult *teiPathAffiliationOld;

void proceedAffiliation()
{
  SxmlNode *countryNode;
  SxmlNode *regionArea;
  char *strRegionArea;
  if((countryNode=SxmlGetFirstChildByTagName(affiliation,"country"))
     &&(regionArea=SxmlGetFirstChildByTagName(affiliation,"wicri:regionArea"))
     &&(strRegionArea=SxmlLeafText(regionArea)))
    {

      char *posC1;
      char *beginZip;
      SxmlNode *retNode;
      SxmlNode *retNode1;
      char *beginRegion;
      /*
      if (!WicriRegExpZip)
	{
	  WicriRegExpZip=RegExpCreate("[0-9][0-9][0-9][0-9][0-9]");
	}
      */  
      posC1=strrchr(strRegionArea,',');
      if (!posC1)return;
      beginRegion=posC1+1;
      while (*beginRegion==' ')beginRegion++;
      retNode=WicriGetRegionFromSubField(affiliation, beginRegion, strRegionArea, SxmlLeafText(countryNode));
      retNode1=WicriAffiliationSetUnivFromField(affiliation, strRegionArea);
      if (retNode||retNode1)return;
  

      /*   end deprecated ??? */

      if (strcmp(SxmlLeafText(countryNode),"France")==0)
	{
	  /* beginZip=RegExpFind(WicriRegExpZip, posC1); */
	  beginZip=WicriFindZip5(posC1);
	  if ((!beginZip)
	      && ((posC1-strRegionArea) > 10))
	    {
	      /* beginZip=RegExpFind(WicriRegExpZip, posC1-10); */
	      beginZip=WicriFindZip5(posC1-10);
	    }
	  if (beginZip) 
	    {
	      retNode=WicriFranceGetCityRegionFromCodeInsee(affiliation, beginZip);
	      if (retNode) return;
	      SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:noRegion",beginZip));
	      return;
	    }
	  retNode=WicriFranceGetCityRegionFromTableVilles(affiliation, posC1+2);
	  if (retNode) return;
	  if (posC1[1]==' ') SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:noRegion",posC1+2));
	  else SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:noRegion",posC1+1));
	  return;
	}
      if (strcmp(SxmlLeafText(countryNode),"Allemagne")==0)
	{
	  retNode=WicriAllemagneGetRegionFromField(affiliation, beginRegion, strRegionArea);
	  if (retNode) return;
	}
      SxmlAppendChild(affiliation, SxmlLeafCreate("wicri:noRegion", beginRegion));
    }
}

int main()
{
   teiPathAffiliationOld=SxPathSetCompile("TEI/teiHeader/fileDesc/titleStmt/author/affiliation");
   teiPathAffiliation=SxPathSetCompile("TEI/teiHeader/fileDesc/sourceDesc/biblStruct/analytic/author/affiliation");

   while ((docInput=SxmlInputNextDocumentElement()))
     { 
       if((listAffiliations=SxPathSetResultListCreate(teiPathAffiliation, docInput)))
	 {
	   while((affiliation=SxmlNodeListNextNode(listAffiliations)))
	     {
	       proceedAffiliation();       
	     } 
	 }
       if((listAffiliations=SxPathSetResultListCreate(teiPathAffiliationOld, docInput)))
	 {
	   while((affiliation=SxmlNodeListNextNode(listAffiliations)))
	     {
	       proceedAffiliation();       
	     } 
	 }
       SxmlPrint (SxmlInputRecord);
     }
   exit(EXIT_SUCCESS);
}
