/**********************************************************************
 * lucy_answer.c -- answer-question routine for lucy
 *
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/RCS/lucy_answer.c,v $
 * $Author: brlewis $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/RCS/lucy_answer.c,v 1.1 91/02/06 16:59:01 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_lucy_answer_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/RCS/lucy_answer.c,v 1.1 91/02/06 16:59:01 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include "lucy/lucy.h"
tfile unix_tfile();		/* XXX should be in discuss .h file */

void
lucy_answer(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  FILE *fp;
  tfile tf;
  lucy_qident i, j;
  name_blk *nb;
  lucyqlist l;
  int oumask;

  i = atoi(argv[1]);		/* XXX more error checking */

  oumask = umask(077);		/* protect temporary file */
  fp = fopen("/tmp/lucydraft", "w"); /* XXX need unique name */
  umask(oumask);

  if (!fp) {
    perror(argv[0]);
    return;
  }
  code = lucy_trn_info(i, &l);
  if (code) {
    com_err(argv[0], code, "");
    return;
  }
  code = lucy_qnb(&nb);
  if (code) {
    com_err(argv[0], code, "");
    return;
  }

  fprintf(fp, "QUESTION:\n");
  fflush(fp);
  tf = unix_tfile(fp->_file);
  code = lucy_chain_to_file(i, fp, 0);
  fprintf(fp, "\nANSWER:\n");
  fclose(fp);

  system("emacs /tmp/lucydraft");	/* XXX do right */

  fp = fopen("/tmp/lucydraft", "r");
  if (!fp) {
    perror(argv[0]);
    return;
  }
  tf = unix_tfile(fp->_file);
  dsc_add_trn(nb, tf, l->info.subject, i, &j, &code);
  if (code) com_err(argv[0], code, "");
  else {
    printf("Entered transaction %d\n", j);
    lucy_mark(i, LUCY_ANSWERED);
    lucy_mark(j, LUCY_ANSWERED);
  }
  lucy_trn_info(0, NULL);
  return;
}
