/**********************************************************************
 * lucy_post.c -- post-answer 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_post_c[] = "$Header$";
#endif /* lint */

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

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

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

  /* XXX use better way of getting username */
  dsc_get_mtg(getenv("USER"), LUCY_BROWSER, &nb2, &code);
  if (code) {
    com_err(argv[0], code, LUCY_BROWSER);
    return;
  }

  code = lucy_qnb(&nb);
  if (code) {
    com_err(argv[0], code, LUCY_INBOX);
    return;
  }

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

  if (!fp) {
    perror(argv[0]);
    return;
  }

  tf = unix_tfile(fp->_file);
  dsc_get_trn(nb, i, tf, &code);

  fclose(fp);
  fp = fopen("/tmp/lucydraft", "r");
  if (!fp) {
    perror(argv[0]);
    return;
  }
  code = lucy_trn_info(i, &l);
  if (code) {
    com_err(argv[0], code, "getting subject line");
    return;
  }
  tf = unix_tfile(fp->_file);
  dsc_add_trn(&nb2, tf, l->info.subject, 0, &j, &code);
  if (code) {
    com_err(argv[0], code, "while posting");
    return;
  }
  printf("Posted transaction %d\n", j);
  lucy_mark(i, LUCY_POSTED);
  lucy_mark(l->info.fref, LUCY_POSTED);
  
  return;
}
