/**********************************************************************
 * do_ask.c  -- SS command for asking a question
 * tty lucy client using SS library
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_ask.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_ask.c,v 1.1 91/09/24 14:42:33 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_ask_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_ask.c,v 1.1 91/09/24 14:42:33 brlewis Exp Locker: brlewis $";
#endif /* lint */

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

void
do_ask(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  char fname1[256], fname2[256], subj[256], pers[256], publ[4];
  char buf[BUFSIZ];
  FILE *fp;
  char *dearlucy;

  /* Check usage */
  if (argc > 2) {
    printf("Usage: %s [prepared file].\n");
    return;
  }
  if (argc == 1) {
    sprintf(fname1, "%s/askdraft", luv_topdir);
    if (!(fp=fopen(fname1, "w"))) {
      com_err(argv[0], (long)errno, "(%s)", fname1);
      return;
    }
    /* Type your question now.  End with  period on a line by itself. */
    sprintf(fname2, "%s/%s", LUCY_TEXTDIR, "pre-question.txt");
    luc_insert_file(stdout, fname2);

    dearlucy = getenv("DEARLUCY") ? getenv("DEARLUCY") : "Dear lucy,";
    fprintf(fp, "%s\n", dearlucy);
    printf("%s\n", dearlucy);
    while(fgets(buf, BUFSIZ-1, stdin)) {
      if (buf[0] == '.' && buf[1] == '\n') break;
      fprintf(fp, "%s", buf);
    }
    if (fclose(fp) == EOF) {
      com_err(argv[0], (long)errno, "(%s)", fname1);
      return;
    }
  } else {
    strcpy(fname1, argv[1]);
  }

  /* "Please enter a one line subject ..." */
  sprintf(fname2, "%s/%s", LUCY_TEXTDIR, LUCY_SUBJ_TXT);
  luc_insert_file(stdout, fname2);
  do {
    promptfor("Subject --> ", subj, 254);
    if (!subj[0]) printf("Any subject line will do.\n");
  } while (!subj[0]);

  /* "If you permit it, lucy will publish ..." */
  sprintf(fname2, "%s/%s", LUCY_TEXTDIR, LUCY_PUBL_TXT);
  luc_insert_file(stdout, fname2);
  if (say_yes("Publish question/answer in browser? ", 1)) {
    strcpy(publ, "yes");
  } else {
    strcpy(publ, "no");
  }

  /* "If you would like a personal reply, ..." */
  sprintf(fname2, "%s/%s", LUCY_TEXTDIR, LUCY_PERS_TXT);
  luc_insert_file(stdout, fname2);
  promptfor("Send personal reply to: ", pers, 254);
  if (!pers[0]) strcpy(pers, "nobody");

  code = luc_ask_file(subj, fname1, pers, publ);
  if (code) {
    com_err(argv[0], code, "(%s)", luv_context);
  } else {
    printf("Your question has been sent to %s\n", luv_email);
  }

  return;
}
