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

/* TeiRejectOnIdno */

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

#include "SxPath.h"
#include "Buffer.h"
#include "StrSearch.h"
#include "StrDict.h"

SxPathResult *teiPathIdnoForISSN;
SxPathResult *teiPathIdnoForRBID;

extern char *optarg;
extern int   optind;
int getopt();

/* StrSearchTable *tableReject;
   StrSearchTable *tableKeep; */

StrDict  *tableReject;
StrDict  *tableKeep;

int wicriMode;
int nKeep;

SxmlNode *docInput;

Buffer *bufKey;

int main (int argc, char **argv) {
  int cOption;
  char *nextOptArg;
  char *typeIdno;
  char *strPathIdnoForISSN;
  char *strPathIdnoForRBID;
  bufKey=BufferCreate(10,10);

  strPathIdnoForISSN="TEI/teiHeader/fileDesc/publicationStmt/idno";
  strPathIdnoForRBID="TEI/teiHeader/fileDesc/publicationStmt/idno";
  wicriMode=0;
  /*
  tableReject=StrSearchTableCreate(10,5);
  tableKeep=StrSearchTableCreate(10,5);
  */
  tableReject=NewStrDict();
  tableKeep=NewStrDict();
  typeIdno=NULL;
  nKeep=0;

  while((cOption=getopt(argc,argv,"k:i:t:P:T:w:"))!=EOF)
    
    {switch (cOption)
        {
          
        case 't':
	  /*          tableReject=StrSearchAddTable(optarg, 100); */
	  StrDictAddFromFile(tableReject,optarg);
          break;
	case 'P':
	  strPathIdnoForISSN=optarg;
	  break;
	case 'T':
	  typeIdno=optarg;
	  break;
	case 'i':
	  while(optind<=argc)
	    {
	      StrSearchPut(tableReject, optarg, "");
	      nextOptArg=argv[optind];
	      if(!nextOptArg)break;
	      if(nextOptArg[0]=='-')break;
	      else
		{
		  optind++;
		  /* StrSearchPut(tableReject, nextOptArg, ""); */
		  StrDictAddNewDatum(tableReject, nextOptArg, ""); 
		}
	    }
	  break;
	case 'k':
	  while(optind<=argc)
	    {
	      nKeep++;
	      StrSearchPut(tableKeep, optarg, "");
	      nextOptArg=argv[optind];
	      if(!nextOptArg)break;
	      if(nextOptArg[0]=='-')break;
	      else
		{
		  optind++;
		  /* StrSearchPut(tableKeep, nextOptArg, ""); */
		  StrDictAddNewDatum(tableKeep, nextOptArg, ""); 
		}
	    }
	  break;
	case 'w':
	  strPathIdnoForISSN="TEI/teiHeader/fileDesc/sourceDesc/biblStruct/series/idno";
	  wicriMode=1;  /* ISSN - to be improved */
	  break;
        }
    }

  teiPathIdnoForISSN=SxPathSetCompile(strPathIdnoForISSN);
  teiPathIdnoForRBID=SxPathSetCompile(strPathIdnoForRBID);

   while ((docInput=SxmlInputNextDocumentElement()))
     {  
       SxmlNode *listIdno;
       SxmlNode *listIdnoForRBID;
       SxmlNode *nodeIdno;
       int reject;
       int toKeep;
       listIdno=SxPathSetResultListCreate(teiPathIdnoForISSN, docInput);
       listIdnoForRBID=SxPathSetResultListCreate(teiPathIdnoForRBID, docInput);
       if(!listIdno)
	 {
	   SxmlPrint (SxmlInputRecord);
	   continue;
	 }
       reject=0;
       toKeep=0;
       if ((nKeep>0)&&listIdnoForRBID)
	 {
	   SxmlNode *rbidNode;
	   /*
	   rbidNode=SxmlGetFirstChildTagAtt(listIdno, "idno", "type", "RBID");
	   if (rbidNode&&(rbid=SxmlLeafText(rbidNode)))
	     {
	       if (StrDictSearch(tableKeep,rbid))
		 {
		   SxmlPrint (SxmlInputRecord);
		   continue;
		 }    
	     }
	   */
	   SxmlReset(listIdnoForRBID);
	   while ((rbidNode=SxmlNodeListNextNode(listIdnoForRBID)))
	     {
	       char *curIdno;
	       if ((curIdno=SxmlLeafText(rbidNode)))
		 {
		   char *type;
		   type=SxmlGetAttribute(rbidNode, "type");
		   if ((SxmlHasAttribute(rbidNode, "type", "RBID")))
		     {
		       if (StrDictSearch(tableKeep,curIdno))
			 {
			   toKeep=1;
			   break;
			 }
		     }
		 }
	     }
	 }
       if (toKeep==1)
	 {
	   SxmlPrint (SxmlInputRecord);
	   continue;
	 }
       SxmlReset(listIdno);
       while ((nodeIdno=SxmlNodeListNextNode(listIdno)))
         {
	   char *curIdno;
	   curIdno=SxmlLeafText(nodeIdno);

	   if (wicriMode>0)
	     {
	       if ((SxmlHasAttribute(nodeIdno, "type", "ISSN"))
		 ||(SxmlHasAttribute(nodeIdno, "type", "eISSN")))
		 {
		   /* if( StrSearch(tableReject, curIdno)) */
		   if( StrDictSearch(tableReject, curIdno))
		     {
		       reject=1; 
		       break;
		     }
		 }
	     }
	   else if ((!typeIdno)||SxmlHasAttribute(nodeIdno, "type", typeIdno))
	     {
	       /* if( StrSearch(tableReject, curIdno)) */
	       if( StrDictSearch(tableReject, curIdno))
		 {
		   reject=1; 
		   break;
		 }
	     }
         }
       if (reject) continue;
        SxmlPrint (SxmlInputRecord);
     }
  exit (EXIT_SUCCESS);
}


