/**********************************************************************
 * discuss.c -- discuss routines for lucy
 *
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/RCS/discuss.c,v $
 * $Author: brlewis $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/RCS/discuss.c,v 1.1 90/12/18 15:10:58 brlewis Exp Locker: brlewis $
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 * For copying and distribution information, see the file
 * "mit-copyright.h".
 **********************************************************************/
#include "mit-copyright.h"

#ifndef lint
static char rcsid_discuss_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/RCS/discuss.c,v 1.1 90/12/18 15:10:58 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include "lucy/lucy.h"

long
lucy_qnb(nbpp)
     name_blk **nbpp;
{
  static name_blk nb;
  static short initialized = 0;
  long code = 0L;
  static char mtgfile[128];

  /* XXX use better way of getting username */
  if (!initialized) {
    /*
    sprintf(mtgfile, "%s/.meetings.lucy", getenv("HOME"));
    setenv("MEETINGS", mtgfile);
    dsc_get_mtg(getenv("USER"), LUCY_INBOX, &nb, &code);
    printf("dsc_get_mtg returns %s\n", error_message(code));
    if (code == NO_MTGS_FILE || code == NO_SUCH_MTG) {
      code = lucy_append_file("/afs/athena/astaff/project/lucydev/.meetings",
		       mtgfile);
      if (code) return(code);
    }
    */
    dsc_get_mtg(getenv("USER"), LUCY_INBOX, &nb, &code);
    initialized = 1;
  }
  if (nbpp) *nbpp = &nb;
#ifdef DEBUG
  if (code) LUCY_DEBUG(("lucy_qnb", code, ""));
#endif /* DEBUG */
  return(code);
}

long
lucy_qinfo(mipp)
     mtg_info **mipp;
{
  static mtg_info mi;
  name_blk *nb;
  long code = 0L;

  code = lucy_qnb(&nb);
  if (code) {
    LUCY_DEBUG(("lucy_qinfo/lucy_qnb", code, ""));
    return(code);
  }
  dsc_get_mtg_info(nb, &mi, &code);
  *mipp = &mi;
  LUCY_DEBUG(("lucy_qinfo/dsc_get_mtg_info", code, ""));
  return(code);
}

long
lucy_trn_info(n, lqpp)
     int n;
     lucyqlist *lqpp;
{
  static lucyqlist lq = NULL;
  name_blk *nb;
  lucyqlist parent, qnode;
  long code;
  extern int errno;

  /* free old list if asked */
  if (!lqpp) {
    for (qnode = lq; qnode; qnode = qnode->next)
      free((char *) qnode);	/* XXX free strings in info struct? */
    lq = NULL;
    return(0L);
  }

  /* Get name block */
  code = lucy_qnb(&nb);
  if (code) {
    LUCY_DEBUG(("lucy_trn_info/lucy_qnb", code, ""));
    return(code);
  }

  /* Get transaction info for transaction n */
  parent = qnode = lq;
  for (qnode = lq; qnode; qnode = qnode->next) {
    if (qnode->info.current == n) {
      *lqpp = qnode;
      return(0L);
    }
    parent = qnode;
  }

  /* Cache new node */
  qnode = New(struct lucyqnode);
  qnode->next = NULL;
  if (!(qnode)) return((long) errno);
  dsc_get_trn_info3(nb, n, &(qnode->info), &code);
  if (code) {
    LUCY_DEBUG(("lucy_trn_info/dsc_get_trn_info", code, ""));
    /* XXX free strings in info struct? */
    free((char *) qnode);
    return(code);
  }
  if (!lq) lq = qnode;
  if (parent) parent->next = qnode;
  *lqpp = qnode;
  return(0L);
}

long
lucy_nextq(n, resp)
     lucy_qident n, *resp;
{
  lucyqlist lq;
  mtg_info *mi;
  long code;

  /* If n < LOWQ, find the first question in the queue */
  if (n < LUCY_LOWQ) {
    code = lucy_qinfo(&mi);
    if (code) {
      LUCY_DEBUG(("lucy_nextq/lucy_qinfo", code, ""));
      return(code);
    }
    n = mi->first;
    /* The first non-dl trans in queue may be first question. */
    if (n >= LUCY_LOWQ) {
      *resp = n;
      return(0L);
    }
  }

  /* Find the next trans that starts a chain */
  code = lucy_trn_info(n, &lq);
  if (code) {
    LUCY_DEBUG(("lucy_nextq/lucy_trn_info", code, ""));
    return(code);
  }
  do {
    n = lq->info.next;
    if (!n) break;
    code = lucy_trn_info(n, &lq);
    if (code) {
      LUCY_DEBUG(("lucy_nextq/lucy_trn_info", code, ""));
      return(code);
    }
  } while (lq->info.pref);
  *resp = n;
  return(0L);
}

long
lucy_mark(i, flag)
     int i, flag;
{
  long code;
  name_blk *nb;

  code = lucy_qnb(&nb);
  if (code) return(code);
  return(lucy_mark2(nb, i, flag));
}
	 
long
lucy_mark2(nb, i, flag)
     name_blk *nb;
     int i, flag;
{
  long code;
  trn_info3 info;

  dsc_get_trn_info3(nb, i, &info, &code);
  if (code) return(code);
  dsc_set_trn_flags(nb, i, info.flags|flag, &code);
  return(code);
}
