/**********************************************************************
 * tqm -- see what lucy's lag time is in answering questions
 *
 * $Author: brlewis $
 * $Source$
 * $Header$
 *
 * Copyright 1992 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid_tqm_c[] = "$Header$";
#endif /* lint */

#ifdef POSIX
#include <time.h>
#else
#include <sys/types.h>
#endif
#include <lucy/lucy.h>

long
print_info(tinfo, flag)
     ltrn *tinfo;
     int flag;
{
  long code;
  time_t answer_time=0L, age;
  ltrn *tinfo2;
  int i;

  for (i=tinfo->lref; i>tinfo->current; i=tinfo2->pref)
    {
      if (code=luc_trn_info(&luv_qmtg, i, &tinfo2)) return(code);
      if (!tinfo2->num_chars) continue;
      if (tinfo2->flags & flag)
        {
          answer_time = tinfo2->date_entered;
          break;
        }
    }
  if (answer_time)
    {
      age = answer_time - tinfo->date_entered;
      printf("%5d %5d %5d\n", age / (24 * 60 * 60), tinfo->num_chars,
	     tinfo2->num_chars);
    }
  return(0L);
}

main(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  int curtrn = LUCY_NULLQ;
  ltrn *tinfo;
  time_t right_now, how_early, submit_time, answer_time, age, total=0L;
  char time_string[17];
  int age_count = 0, unanswered_count = 0;

  /* check usage */
  if (argc != 2)
    {
      fprintf(stderr, "Usage: %s days\n", argv[0]);
      exit(1);
    }

  /* get time range; */
  right_now = (time_t) time(0);
  how_early = right_now - atoi(argv[1]) * (24 * 60 * 60);

  /* connect to lucy */
  code = luc_init();
  if (code) {
    com_err(argv[0], code, "(%s)", luv_context);
    /* exit(1); except dsc_get_mtg_info returns bogus error? */
  }

  /* find first question to check */
  do
    {
      if (code = luc_next(luv_curmtgp, curtrn, &curtrn))
	goto ABORT;
      if (!curtrn)
	{
	  fprintf(stderr, "%s: No answers in time range.\n", argv[0]);
	  exit(1);
	}
      if (code = luc_trn_info(&luv_qmtg, curtrn, &tinfo))
	goto ABORT;
    } while (tinfo->date_entered < how_early);

  /* main loop */
  while(curtrn)
    {
      if (code = luc_trn_info(&luv_qmtg, curtrn, &tinfo))
	goto ABORT;

      /* unanswered question */
      if (! (tinfo->flags & (LUCY_REPLIED|LUCY_POSTED)))
	{
  	  unanswered_count++;
	  if (code = luc_next(luv_curmtgp, curtrn, &curtrn))
	    goto ABORT;
	  continue;
	}

      /* get submit time */
      submit_time = (time_t) tinfo->date_entered;

      /* answer time for posting */
      if (tinfo->flags & LUCY_POSTED)
	if (code = print_info(tinfo, LUCY_POSTED))
	    goto ABORT;

      /* answer time for reply */
      if (tinfo->flags & LUCY_REPLIED)
	if (code = print_info(tinfo, LUCY_REPLIED))
	    goto ABORT;

      /* get next info */
      if (code = luc_next(luv_curmtgp, curtrn, &curtrn))
	goto ABORT;
    }

  exit(0);

 ABORT:
  com_err(argv[0], code, luv_context);
  exit(1);
}
