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

/*
 NlmPmcGetCorpusById.c
 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Nlm.h"
                         
extern char *optarg;
extern int   optind;
int getopt();

void printDocument(char *id)
{
  NlmPerformFetch("pubmed", id);
}

int main (int argc, char **argv)
{
  int cOption;
  Buffer *inputId;
  Buffer *bufIdList;
  int stdinFlag;
  int nListId;

  stdinFlag=1;

   NlmCurlInit();
  inputId=NewBuffer();
  bufIdList=NewBuffer();
  NlmTraceMode=0;

  while((cOption=getopt(argc,argv,"k:T:"))!=EOF)
    {switch (cOption)
	{
	case 'k':
	  printDocument (optarg);
	  stdinFlag=0;
	  break;
	case 'T':
	  NlmTraceMode=atoi(optarg);
	  break;
	}
    }
      
  if (stdinFlag==0)
    {
      NlmCurlEnd();
      exit(EXIT_SUCCESS);
    }
  nListId=0;
  while (BufferGets(inputId))
    {
      if (BufferTailCmp(inputId, "\0x0D")==0) BufferTailCut(inputId, 1);
      if (nListId==0)
	{
	  BufferStrcpy( bufIdList , BufferString(inputId));
	  nListId=1;
	}
      else
	{
	  BufferStrcat( bufIdList ,",");
	  BufferStrcat( bufIdList ,BufferString(inputId));
	  nListId++;
	  if (nListId==100)
	    {
	      printDocument (BufferString(bufIdList));
	      nListId=0;
	    } 
	}
    }
  if (nListId>0) printDocument (BufferString(bufIdList));
  NlmCurlEnd();
  exit(EXIT_SUCCESS);
}
