
/* testm.c tests out the lucy meetings interface.
   1st it gets the info from the first transaction in the
   lucyb meeting and displays the subject of this transaction.

   then, it creates a tfile, and uses dsc_get_trn and tread to
   get the contents of this first transaction.

   it uses a real kludge to access the contents of the
   tfile.  kludge is using freopen to associate the stdout
   with a disk file.  and to read the disk file to find out what
   it is we have written to it!  because tread doesn't seem to
   work on a tfile created from stdout, and whenever i use a stream
   as an argument to unix_tfile(), I get a segmentation violation.
   Oh, i need to use a file descriptor argument, which is an integer,
   but how do you get the file descriptor for a disk file?
   anyways, this WORKS.  and it's taken me critical THESIS time, to
   get to this point.  this is good enough for now.
   -- ukim */


#include <stdio.h>   
#include <discuss/discuss.h>
#include "lucy.h"  /* **** ukim *****/

extern tfile unix_tfile();


main()
{
  ltrn *tran_info;  /* transaction info structure */
  char subject[255];  /* subject of transaction */
  int next;
  int tnum; /* transaction number */
  tfile tf;  /* need a tfile store contents of transaction in */
  int terror; /* place to hold error numbers that meetings server
		 might return */
  char buffer[4096]; /* place to hold contents of transaction */
  char text[4096]; /* buffer for tfile */
  char tmpfname[100]; /* name of temporary file */
  FILE *tempfile; /* temporary file */
  
  luc_init();  /* initialize lucy discuss meetings */
  luv_curmtgp = &luv_bmtg; /* the current meeting will be the browser */
  luc_next(luv_curmtgp, 0, &next);  /* get first (next) transaction. */
  luc_trn_info(luv_curmtgp, next, &tran_info);

  strcpy(subject, tran_info->subject);
  tnum = tran_info->current; /* transaction number of current transaction */
  fprintf(stderr, "Subject of transaction: %s \n", subject);
  fprintf(stderr, "Transaction # is %d \n", tnum);


  /* creat a tfile :
     create a temporary file, and open it.
     use unix_tfile to translate it into a tfile.
     call get_trn, then read the temporary file 
     to output to fprintf */

  tmpnam(tmpfname); /* create a temporary file name */

  tempfile = freopen(tmpfname, "w+", stdout); 

  if (tempfile == NULL) {
    fprintf(stderr, "could not create a temporary file.\n");
    exit(1);
  }

  fprintf(stderr, "creating unix wrapper \n");
  tf = unix_tfile(1); /* put a tfile wrapper around the tempfile */
  fprintf(stderr, "finished creating unix wrapper \n");

  dsc_get_trn(&(luv_curmtgp->nb), tnum, tf, &terror); 
  fprintf(stderr, "get_trn returned error code %d \n", terror);


  tdestroy(tf); /* free the memory allocated to the tfile */
  
  fclose(tempfile);  /* flush out to disk before reading it back in */
  /* need to close the file, to flush out the data, so that we
     can read it back in. */

  /* read from buffer */
  
  tempfile = fopen(tmpfname, "r");
  fread(buffer, sizeof(char), 4000, tempfile); 
  
  /* display transaction contents */
  fprintf(stderr, "Contents of transaction are \n%s\n--End of File--\n",
	  buffer);  

  fclose(tempfile); 
  unlink(tmpfname); /* get rid of our temporary file */

}


  

