
/*
 *
 *	Copyright (C) 1988, 1989 by the Massachusetts Institute of Technology
 *    	Developed by the MIT Student Information Processing Board (SIPB).
 *    	For copying information, see the file mit-copyright.h in this 
release.
 *
 */
/*
 *
 * pmtg.c  -- Program to print out a entire meeting.
 *
 */

#ifndef macintosh
#include <discuss/discuss.h>
#include <sys/file.h>
#include <stdio.h>
#include <string.h>
#else
#include <bsd-mac-compat.h>
#include <discuss.h>
#endif

tfile unix_tfile();
mtg_info minfo;

#ifndef	lint
static char rcsid_pmtg_c[] =
    "$Header: pmtg.c,v 1.3 89/06/03 00:31:53 srz Exp $";
#endif

main (argc,argv)
	int argc;
	char **argv;
{
	int result;
	trn_nums trn;
	trn_info2 tinfo;
	tfile tfstdout;
	name_blk nb;
	int code;

	init_dsc_err_tbl();
	argc--; argv++;

#define WHOAMI "pmtg"

	if (argc != 1)
#ifdef ndef
		goto lusage;
#endif
	{
	fprintf(stderr, "usage: %s mtg\n", WHOAMI); 
	exit(2);
      }

	dsc_get_mtg ("jon", *argv, &nb, &code);
	if (code != 0) {
	  fprintf(stderr, "%s: %s\n", WHOAMI, error_message(code));
	  exit(1);
	}
    
	dsc_get_mtg_info (&nb, &minfo, &code);
	if (code != 0) {
	  fprintf(stderr, "%s: %s\n", WHOAMI, error_message(code));
	  exit(1);
	}

	printf("Meeting info for %s@%s\n", minfo.long_name, minfo.location);
	printf("\tchairman: %s, first %d, last %d\n\n", minfo.chairman, 
	       minfo.first, minfo.last);

	fflush(stdout);

	/* set up stdout tfile */
	tfstdout = unix_tfile (1);

	trn = minfo.first;
	while (trn != 0) {
		dsc_get_trn_info2 (&nb, trn, &tinfo, &result);
		if (result != 0) {
			if (result != DELETED_TRN) {
				(void) fprintf (stderr, "%s\n",
						error_message (result));
				exit (1);
			}
		} else {
			write_header (&tinfo, tfstdout);
			dsc_get_trn (&nb, trn, tfstdout, &result);
			if (result != 0) {
				(void) fprintf (stderr, "%s\n",
						error_message(result));
				exit (1);
			}
			write_trailer (&tinfo, tfstdout);
		}
		trn = tinfo.next;
		free (tinfo.author);
		free (tinfo.subject);
	}

	tdestroy (tfstdout);
	return 0;

 lusage:
	(void) fprintf (stderr, "Usage: pmtg {mtg_name}\n");
	exit (1);
}

char *ctime();

write_header(info, tf)
	trn_info2 *info;
	tfile tf;
{
	char line [255];
	char newtime [26];
	char *plural;
	int dummy;

	(void) strcpy (newtime, ctime (&(info -> date_entered)));
	newtime [24] = '\0';	/* get rid of \n */

	if (info -> num_lines != 1)
		plural = "s";
	else
		plural = "";
     
	(void) sprintf (line, "[%04d] %s %s %s (%d line%s)\n",
			info -> current, info -> author, minfo.long_name,
			&newtime[4], info -> num_lines, plural);
	twrite (tf, line, strlen (line),&dummy);
	if (info -> subject [0] != '\0') {
		twrite (tf, "Subject: ", 9, &dummy);
		twrite (tf, info -> subject, strlen (info -> subject), &dummy);
		twrite (tf, "\n", 1, &dummy);
	}
	return;
}

write_trailer (info, tf)
trn_info *info;
tfile tf;
{
     char line [255];
     int dummy;

     if (info -> pref == 0 && info -> nref == 0)
	  sprintf (line, "--[%04d]--\n\n", info -> current);
     else if (info -> pref == 0)
	  sprintf (line, "--[%04d]-- (nref = [%04d])\n\n", info -> current,
		   info -> nref);
     else if (info -> nref == 0)
	  sprintf (line, "--[%04d]-- (pref = [%04d])\n\n", info -> current,
		   info -> pref);
     else
	  sprintf (line, "--[%04d]-- (pref = [%04d], nref = [%04d])\n\n",
		   info -> current, info -> pref, info -> nref);
     twrite (tf, line, strlen (line),&dummy);
}

