/*   -*- coding: utf-8 -*-  */
/**********************************************************************
*
*  projet   : DilibPro
*  fichier  : HfdSelect.c
*  Auteur   : Jacques DUCLOY
*  Date     : Decembre 2013 (ex DamHfdSelect)
*
***********************************************************************/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "Hfd.h"
#include "Buffer.h"

static  Hfd     *hfd;
static  int     flagKey;
static  int     flagHfd;
static  int     flagInsert;
static  int     flagNL;
static  char    *hfdName;
static  char    *input;
static  char    *separators;
static  Buffer  *buf;

/**
   Display the notice which @a key was given as a parameter.

   Seek the notice which @a key is given as a parameter into @a hfd.
   Diplay it on standard output.

   If @a flagKey is true, @a key is also displayed on standard output.

   @param key the @a key of the notice to display
   @return 1 if the notice was retrieved.
           0 else
 */
int traitKey(char *key)
{
  HfdRecord *rec;
  char      *str;
  if((rec=HfdRecordReadKey(hfd,key)))
    {
      str=HfdRecordAfterKey(rec);
      if (flagKey)printf("%s\t",key);
      if (flagNL)printf("%s",str);
      else puts(str);
      return 1;
    }
  return 0;
}

/**
   Display the notices from the key list in @a input.

   Do it without carriage return.
 */
void traitList()
{
  char *key;
  if ((key=strtok(input,separators)))
    {
      traitKey(key);
      while ((key=strtok(NULL,separators)))
	{
	  traitKey(key);
	}
    }
}


void traitInsert()
{
  char *posTab;
  if((posTab=strchr(input,'\t')))
    {
      posTab[0]='\0';
      if (traitKey(input))
	{
	  posTab[0]='\t';
	  puts(posTab);
	}
      else
	{
	  posTab[0]='\t';
	}
    }
  else
    {
      if(traitKey(input))putchar('\n');
    }
}

    int getopt();
    extern char *optarg;

void usage()
{
  perror ("usage: HfdSelect -h hfdName [-Kn] [-k key] [-l list]\n");
  exit(1);
}

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

  int cod_arg;
  int flagInput;
  Buffer *bufHfdName;

  separators=" ,./;:\t\n";

  flagKey=0;
  flagHfd=0;
  flagInput=1;
  flagInsert=0;
  flagNL=0;

  while ((cod_arg = getopt(argc,argv,"k:h:il:Kn"))!=EOF)
    {switch(cod_arg) 
      {
      case 'i':
	flagInsert=1;
	break;

      case 'k' : 
	if(!flagHfd) usage();
	traitKey(optarg);
	break;

      case 'h' :
	hfdName=optarg;
	bufHfdName=BufferFromString(hfdName);
	while((BufferTailCmp(bufHfdName,"/")==0)
	      ||(BufferTailCmp(bufHfdName," ")==0))
	  BufferTailCut(bufHfdName,1);
	if (BufferTailCmp(bufHfdName,".hfd")==0)
	  BufferTailCut(bufHfdName,4);
	if((hfd=HfdOpenReadKey(BufferString(bufHfdName))))
	  {
	    flagHfd=1;
	    break;
	  }
	else
	  {
	    perror("HFD not founded\n");
	    usage();
	  }

      case 'K' :
	flagKey=1;
	break;

      case 'l' :
	if(!flagHfd) usage();
	input=optarg;
	traitList();
	break;

      case 'n':
	flagInput=0;
	break;

      default: usage();
	break;
      }};
  
  if (flagInput==1)
    {
      buf=BufferCreate(132,132);
      if (flagInsert)flagNL=1;
      while ((input = BufferGets(buf)))
	{
	  if (flagInsert)traitInsert();
	  else traitList();
	};
    };
  
  return 0;
}


