/*   -*- coding: utf-8 -*-  */
/**********************************************************************
*
*  module   : Hfd
*  commande : HfdIndexOr
*  fichier  : HfdIndexOr.c
*  Auteur   : Jacques DUCLOY
*  Date     : 2015
*******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "StrDict.h"
#include "SxPath.h"
#include "HfdBase.h"

    int getopt();
    extern char *optarg;

int main(int argc, char **argv)
{
  int cod_arg;
 int flagInput;
  SxmlNode *queryNode;
  SxmlNode *orNode;
  SxmlNode *termNode;
  SxmlNode *resultNode;
  Buffer *HfdBufInput;
  HfdIndex *hfd1;
  SxmlNode *l1;
  HfdBase  *boolBase;

  HfdIndexBoolInit();
  boolBase=HfdBaseCreate();
  resultNode=SxmlElementCreate("result");
  orNode=SxmlElementCreate("or");
  queryNode=SxmlElementCreate("query");
  SxmlAppendChild(queryNode, orNode);
  SxmlAppendChild(resultNode, queryNode);
  flagInput=0;

  while ((cod_arg = getopt(argc,argv,"b:i:k:st:I"))!=EOF)
    {switch(cod_arg) 
	{
	case 'k':
	case 't':
	  termNode=HfdBaseTermFromStr(boolBase, optarg);
	  SxmlAppendChild(orNode, termNode);
	  break;
	case 'i':
	  hfd1=HfdBaseNewIndex(boolBase, optarg);
	  break;
	case 'I':
	  flagInput=1;
	  break;
	case 'b':
	  HfdBaseSetBaseName(boolBase, optarg);
	  break;
	}
    }
  if (flagInput==1)
    {
      char *input;
      HfdBufInput=NewBuffer();
      while ((input = BufferGets(HfdBufInput)))
	{
	  SxmlAppendChild(orNode, termNode=SxmlLeafCreate("term", input));
	}
    };
  l1=HfdBaseOr(boolBase ,orNode);
  SxmlAppendChild(resultNode, l1);
  SxmlPrint (resultNode);
  putchar ('\n');
  HfdBaseFree(boolBase);
  exit (EXIT_SUCCESS);
}
