/**********************************************************************
 * do_browse.c -- browse-meetings routine for lucy
 *
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_browse.c,v $
 * $Author: brlewis $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_browse.c,v 1.1 91/09/24 14:42:43 brlewis Exp Locker: brlewis $
 *
 * 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_do_browse_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_browse.c,v 1.1 91/09/24 14:42:43 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <sys/types.h>
#include <sys/errno.h>
#include <strings.h>
#include <lucy/lucy.h>
#include "config.h"
char *mktemp();

/*ARGSUSED*/
void
do_browse(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  char mtgfile[64];
  FILE *fp;
  ltrn *trninfo;
  time_t how_long_ago;
  int trncount=0;
  char call[256], xdsclink[64];

  /* find last transaction we want to see */
  how_long_ago = (time_t) time(0) - LUCY_BROWSE_TIME;
  if (code=luc_trn_info(&luv_bmtg, luv_bmtg.info.last, &trninfo)) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }
  while ((trninfo->prev && trncount < LUCY_BROWSE_MIN) ||
	 (trninfo->date_entered>how_long_ago && trncount<LUCY_BROWSE_MAX)) {
    trncount++;
    if (code=luc_trn_info(&luv_bmtg, trninfo->prev, &trninfo)) {
      com_err(argv[0], code, "(%s)", luv_context);
      return;
    }
  }

  /* get meetings file */
  if ((fp=fopen(mktemp(strcpy(mtgfile, "/tmp/.meetingsXXXXXX")), "w"))
      == NULL) {
    perror(mtgfile);
    return;
  }
  /* put discuss contents in there */
  fprintf(fp, "0:0:%d:%s:%s:%s,%s:\n", trninfo->current, luv_host,
	  luv_bmtg.info.location, luv_bmtg.info.long_name,
	  rindex(luv_bmtg.info.location, '/')+1);
  if (fclose(fp) == EOF) {
    fprintf(stderr, "%s: %s (%s)\n", argv[0], error_message((long) errno),
	    mtgfile);
    return;
  }

  /* create system call */
  sprintf(call, "MEETINGS=%s ", mtgfile);

  if (luc_shouldfork()) {
    sprintf(xdsclink, "%s/lucy", luv_topdir);
    symlink("/usr/athena/bin/xdsc", xdsclink);
    strcat(call, xdsclink);
    switch(fork()) {
    case 0:
      exit(system(call));
      break;
    case -1:
      perror(argv[0]);
      return;
    default:
      printf("A window should appear shortly with a HELP button for instructions.\n");
#ifdef LOG_USAGE
      log_view("X-browse");
#endif
      return;
    }
  }

  /* use discuss tty interface */
  strcat(strcat(strcat(call, "discuss "),
		rindex(luv_bmtg.info.location, '/')+1),
	 " -quit -request 'p new'");
  system(call);
#ifdef LOG_USAGE
      log_view("tty-browse");
#endif
  return;
}
    
