/**********************************************************************
 * do_post.c  -- SS command for posting a transaction
 * tty lucy client using SS library
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_post.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_post.c,v 1.1 91/09/24 14:45:32 brlewis Exp Locker: brlewis $
 *
 * Copyright 1991 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_do_post_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_post.c,v 1.1 91/09/24 14:45:32 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <strings.h>
#include <ss/ss.h>
#include "lucy/lucy.h"
#include "lucy/memory.h"
#include "config.h"

void
do_post(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  int n, i;
  ltrn *t1, *t2;
  char repls[32];
  int repln = 0;
  LMEETING *mtg;
  char subj[256];
  char *fname;
  int probl;
  FILE *fp;

  /* Check usage */
  if (argc < 2 || !atoi(argv[1])) {
    printf("Usage: %s number.\n");
    return;
  }

  /* get info on trn to be posted */
  if (code=luc_trn_info(&(luv_qmtg), (n=atoi(argv[1])), &t2)) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }
  if (n == t2->fref) {
    printf("That is the first transaction in a chain.\n");
    printf("Type `scan %d' and find the transaction you want to post.\n", n);
    return;
  }
  if (t2->num_chars == 0) {
    printf("There is no text in transaction %d.\n", n);
    return;
  }

  /* Get file of text to audit */
  if (code=luc_trn_file(&(luv_qmtg), n, &fname)) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }
  if (!(fp = fopen(fname, "r"))) {
    perror(fname);
    luc_free_file(&fname);
    return;
  }

  /* Audit */
  probl = audit(fp);
  if (probl) printf("Problem lines: %d\n", probl);
  if (!say_yes("Is this what you want to post?", !probl)) return;

  /* Get subject line from first transaction */
  if (code=luc_trn_info(&(luv_qmtg), t2->fref, &t1)) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }
  strcpy(subj, t1->subject);

  if (say_yes("Is question a response to another posting?", 0)) {
    for (i=0; i<2; i++) {
      promptfor("Which one? ", repls, 31);
      if ((repln = atoi(repls)) != 0) break;
      mtg = luv_curmtgp;
      luv_curmtgp = &luv_bmtg;
      do_scan(1, argv);
      luv_curmtgp = mtg;
    }
    if (repln == 0) {
      printf("Type `go browser', then `scan' to find the previous posting.\n");
      return;
    }
    /* list trn info */
    if (code = luc_trn_info(&(luv_bmtg), repln, &t2)) {
      com_err(argv[0], code, "(%s)", luv_context);
      printf("Type `go browser', then `scan' to find the previous posting.\n");
      return;
    }
    printf("%s\n", list_trn(t2));
    if (!say_yes("Is this the right one?", 1)) {
      printf("Try again when you find the previous posting.\n");
      return;
    }
    strcat(strcpy(subj, "Re: "), t2->subject);
  }
  printf("Subject: %s\n", subj);
  if (!say_yes("Use this subject?", 1))
    promptfor("Type new subject: ", subj, 255);

  /* call luc_post */
  code = luc_post(&(luv_qmtg), n, subj, repln);
  if (code) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }
  printf("Posted in the browser.\n");
#ifdef LOG_USAGE
      log_view("post");
#endif
  return;
}
