/**********************************************************************
 * do_emacs.c  -- SS command for editing various things
 * tty lucy client using SS library
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_emacs.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_emacs.c,v 1.1 91/09/24 14:43:58 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_emacs_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_emacs.c,v 1.1 91/09/24 14:43:58 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <strings.h>
#include <ss/ss.h>
#include <sys/file.h>
#include <sys/wait.h>
#include "lucy/lucy.h"
#include "config.h"

void
do_emacs(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  int n, n2;
  ltrn *t;
  char subj[257];
  int pid;
  char *filename, newfname[128];

  if (argc != 2) goto USAGE;

  if (argv[1][0] == 'c' || argv[1][0] == 'C') {
    printf("An emacs window should appear shortly.\n");
    code = luc_edit_csp();
    if (code) com_err(argv[0], code, "(%s)", luv_context);
    return;
  }

  if (argv[1][0] == 'r' || argv[1][0] == 'R') {
    printf("An emacs window should appear shortly.\n");
    sprintf(newfname, "%s/report.tex", luv_topdir);
    code = luc_edit_file(newfname, 1);
    if (code) com_err(argv[0], code, "(%s)", luv_context);
    return;
  }

  /* if non-numeric, then we don't know what to do with it */
  if ((n = atoi(argv[1])) == 0) goto USAGE;

  /* interpret as transaction number to edit */
  if (code = luc_trn_info(luv_curmtgp, n, &t)) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }
  printf("%s\n", list_trn(t));
  promptfor("Reason for edit: ", subj, 256);

  /* get file to edit */
  if (code = luc_trn_file(luv_curmtgp, n, &filename)) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }
  (void) strcat(strcpy(newfname, filename), ".new");

  /* copy the file if it isn't there already */
  if (access(newfname, F_OK)) {
    if (code = luc_copy_file(filename, newfname)) {
      luc_free_file(&filename);
      com_err(argv[0], code, "(%s)", luv_context);
      return;
    }
  }
  luc_free_file(&filename);

  /* fork if necessary */
  if (luc_shouldfork()) {
    pid = fork();
    if (pid == -1) {
      perror(argv[0]);
      return;
    }
    /* handle parent process */
    if (pid) {
      printf("A window should appear shortly.\n");
      return;
    }
  }

  /* edit the file */
  if (code = luc_edit_file(newfname, 0)) {
    com_err(argv[0], code, "(%s)", luv_context);
    if (luc_shouldfork()) exit(1);
    return;
  }

  /* add the edited version to the chain */
  if (code = luc_add_trn(luv_curmtgp, newfname, subj,
			 luc_fullname(luv_curmtgp->nb.user_id), n, &n2)) {
    com_err(argv[0], code, "(%s)", luv_context);
    if (luc_shouldfork()) exit(1);
    return;
  }

  /* list new trn info */
  if (code = luc_trn_info(luv_curmtgp, n2, &t)) {
    com_err(argv[0], code, "(%s)", luv_context);
    if (luc_shouldfork()) exit(1);
    return;
  }
  printf("%s\n", list_trn(t));

  if (luc_shouldfork()) exit(0);
  return;

 USAGE:
  printf("Usage: %s (number, c or r)\n", argv[0]);
  return;
}
