/*****************************************************************************/
char getz_ID[] = "$Id: getz.c,v 1.18 1995/02/02 16:40:55 etzold Exp $";
/*
**
**    $Source: /kappa1/srs4_0/src/RCS/getz.c,v $
**    $Revision: 1.18 $
**    $Date: 1995/02/02 16:40:55 $
**    $Author: etzold $
**
**    $Locker:  $
**    $State: Exp $
**
**
**                  Copyright by Thure Etzold
**
**             Author: Thure Etzold
**                     EMBL, Meyerhofstrasse 1
**                     69012 Heidelberg, Germany
**                     Tel: 06221 387529  
**                     Email: etzold@embl-heidelberg.de
**
**
**    Requires: 
**
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
**
**    Description
**    =========== 
**
**    command line interface for SRSD
**    getz 'query-string' outputfile
**
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "srsos.h"

#include "message.h"
#include "futil.h"
#include "parser.h"
#include "sm.h"
#include "seq.h"
#include "par.h"
#include "arglist.h"
#include "id.h"
#include "idx.h"
#include "set.h"
#include "entry.h"
#include "library.h"
#include "seqlib.h"
#include "query.h"

#define _CONSTANTS
#define	_SRS      
#define _SLB      
#define _FUNCTION
#define QUERY_M
#define XQUERY_M
#define SEQLIB_M

#include SRSINCLUDE

#if !defined(VMS) && !defined(MAC)
# include "srsenv.h"
#endif

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
**  function used by module "message" for printing error messages
*/

INT4 PrintMessage (MSGo *msg) 
{ 
  if (msg->msg_code == i__readingname)
    putc ('.', stderr); 

  switch (msg->msg_code) {
  case i__wroteset:
    return 0;
    
  default:
    if (msg->msg_t != MSGxINFO) 
      fprintf (stderr, "ERROR: %s, %s\n", msg->primsg, msg->secmsg);
    else
      fprintf (stderr, "%s\n", msg->secmsg);
    return 1;
  }
}




void LibGetStartupInfo () 
{
  SRSoGROUP *group;
  SLBo      *lib;
  LINKo     *link;
  SLBoFIELD *field;
  LIBoINDEX *libInx;
  INT4      c1, c2, c3; 
  char      *tmp;

  for (c1=0; (group = LibNextLibGroup (&c1)); )
    for (c2=0; (lib = LibNextLib (group, &c2)); ) 
      if ((libInx = LibIndexOpen (lib, LibGetIdField (lib), 1))) {

	/* print library */
	printf ("lib: %s, rel: %s, entries: %d, date: %s, group: %s\n", 
		lib->nam, 
		(tmp = IdxGetReleaseName (libInx->idx)) ? tmp : "",
		BtrGetRecordN (libInx->btree),  
		TimeToString (BtrGetTimeCreated (libInx->btree), "date"),
		group->com);

	/* print fields */
	for (c3=0; (field = LibNextField (lib, &c3)); ) 
	  printf ("field: %s, type: %s\n", LibGetFieldName (field), 
		  LibGetFieldIType (field));
      }

  /* print links */
  for (c1=0; (link = LibNextLink (&c1)); ) 
    if (link->lib1 && link->lib2)
      printf ("link: %s %s\n",  link->lib1->nam, link->lib2->nam);

}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
**  main section...
*/

INT4 main(INT4 argc, char *argv[], char *envp[])
{
  FILo        queryFile;
  ARGoLIST    *arglist;
  char        setName[10], *fileName=NULL, *query, *tmp;
  INT4        rv, queryN=1;

  /*
  ** set a function for printing messages and initialize query 
  */


#if !defined(VMS) && !defined(MAC)
  SrsEnv (); /* set environment variables defined in srsenv.h */
#endif
  MsgSetFnct (PrintMessage);
  LibOpen ("srswin");

#ifdef MAC
  do {
  argc = ccommand( &argv);
#endif

  arglist = (ARGoLIST *) LibObjByName ("arglist", "getz");
  argc = ArgGet (arglist, argc, argv);

  if (ParGetNum ("printLibs")) {
    LibPrintLibs ();
    exit (0);
  }
  else if (ParGetNum ("getStartupInfo")) {
    LibGetStartupInfo ();
    exit (0);
  }

  if (argc != 2 && !*(fileName = ParGetStr ("queryFileName"))) {
    ArgUsage (arglist);
    _ErrorExit ();
  }

  if (ParGetNum ("printLibInfo") && argc == 2) {  /* print only info ? */
    LibPrintInfo (argv[1]);
    exit (0);
  }



  ParDefNum ("takeuncompletefeature", 1); /* Operator AFTER, BEFORE allowed */

  if (fileName) {
    rv = FilUOpen (&queryFile, fileName, 512, FILxSTREAM);
    _ErrExit2 (rv, fileName);
  }
      
  do {

    /* 
    ** first check the query - syntax + test operands, transform the query,
    ** ie, add wildcards to search words (optionally) and perform the query;
    */
    
    query = fileName ? queryFile.ln : argv[1];
    SmEdit (query, SMxTRIM);

    if (1) {
      sprintf (setName, "Q%d", queryN++);
      if (QryDo (query, setName)) { 
	

        /* 
        ** the option "-link" libList produces a table of entries with 
        ** a list of corresponding entries from selected libraries
        */

	if ((tmp = ParGetStr ("linkTable")) && *tmp) {
	  SETo     *set, *setLinked;
          IDoENTRY id, idLinked;
	  ENTRYo   *entry, *entryLinked;
	  char     libList[132], *libName, queryStr[255];
	  INT4     k, i, entryN, entryLinkN;

	  set = SetGet (setName);
	  for (entryN=SetSize (setName), k=1; k <= entryN; k++) {
	    SetGetID (set, k, &id);
	    entry = EntryOpen (&id);
	    printf ("%s\n", EntryGetFullName (entry));
	    EntryPrintFields (entry);

	    strcpy (libList, ParGetStr ("linkTable"));
	    tmp = libList;
	    while ((libName = strtok (tmp, " "))) {
	      tmp = NULL;
	      sprintf (queryStr, "[%s-id:%s] > %s", 
		       LibGetName (entry->lib, "full"), EntryGetName (entry), libName);
	      if (QryDo (queryStr, "X")) {
		for (entryLinkN=SetSize ("X"), i=1; i <= entryLinkN; i++) {
		  setLinked = SetGet ("X");
		  SetGetID (setLinked, i, &idLinked);
		  entryLinked = EntryOpen (&idLinked);
		  printf ("%40s %s\n", "", EntryGetFullName (entryLinked));
		  EntryPrintFields (entryLinked);
		  EntryClose (&entryLinked);
		}
		SetDelete ("X");
	      }
	    }
	    EntryClose (&entry);
	  }	  
	}

        /* 
        ** normal output of the set in various forms
        */

	else if (ParGetNum ("printText") || ParGetNum ("printData") ||
	    *ParGetStr ("fieldList")
	    || *ParGetStr("listFSE") /* dgg */
	    ) {
        /*  ParDefNum ("listFOSN", 0); nicht mehr notwendig, neues seqlib */
	    EntryPrintSet (setName, 1, 0);
		}
	else
	  QrySetToList (setName); 
      }
    }  
  } while (fileName && *fileName && FilURead (&queryFile) != e__eof);
#ifdef MAC
  } while (argc);
#endif
  exit(0);
}




