/**********************************************************************
 * lucy_reply.c -- reply-question routine for lucy
 *
 * $Source$
 * $Author$
 * $Header$
 *
 * 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_reply_c[] = "$Header$";
#endif /* lint */

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

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

  if (argc != 3 || !(i = atoi(argv[1]))) {
    printf("Usage:  %s QuestionNumber address\n", argv[0]);
    return;
  }

  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;
  }
  code = lucy_chain_to_file(i, fp, 0);
  fclose(fp);

  system("emacs /tmp/lucydraft");	/* XXX do right */
  sprintf(call, "mhmail %s -from lucy -subject 'your question' < /tmp/lucydraft", argv[2]);
  system(call);			/* XXX check exit status */
  printf("Mail sent to %s.\n", argv[2]);
  lucy_mark(i, LUCY_REPLIED);
  sprintf(call, "RPL %s: %s", argv[2], l->info.subject);
  fp = fopen("/dev/null", "r");	/* XXX error chk */
  tf = unix_tfile(fp->_file);
  dsc_add_trn(nb, tf, call, i, &foo, &code);
  if (code)
    com_err(argv[0], code, "while noting replying");
  lucy_mark(foo, LUCY_REPLIED);
  lucy_trn_info(0, NULL);
  return;
}
