/**********************************************************************
 * luc_archive.c -- archive a chain of transactions
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_archive.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_archive.c,v 1.1 91/09/24 14:32:27 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_luc_archive_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_archive.c,v 1.1 91/09/24 14:32:27 brlewis Exp Locker: brlewis $";
#endif /* lint */

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

/**********************************************************************
 * luc_archvive(lmeeting *mp, int n, char *title, int *np)
 *	caller sets mp (usually luv_curmtgp) to specify meeting
 *	caller sets title to subj of question or anything else desired
 *	caller sets n to first transaction number
 *
 * - writes summary of archived transactions
 * - chains copies of transactions to summary
 * - points np at summary transaction in archive
 * - returns error code
 **********************************************************************/

long
luc_archive(mp, n, title, np)
     LMEETING *mp;
     int n;
     char *title;
     int *np;
{
  long code;
  char summaryfname[256], *fname;
  FILE *fp;
  int i, foo;
  ltrn *t;

  /* open temporary file for transaction summary */
  sprintf(summaryfname, "%s/summaryXXXXXX", luv_topdir);
  if ((fp = fopen(mktemp(summaryfname), "w")) == NULL) {
    strcpy(luv_context, summaryfname);
    return((long) errno);
  }

  /* header to indicate that this is an archive summary */
  fprintf(fp, "%s\n", LUCY_ARCHIVE);

  /* print information on each transaction */
  if (!(code=luc_trn_info(mp, n, &t)))
    for(i=n; i; i=t->nref) {
      if (i != n) if (code=luc_trn_info(mp, i, &t)) break;
      /* summary line */
      fprintf(fp, "\n%d %s %6.6s  %s\n", t->current,
	      luc_flgstr(t->flags), (ctime((time_t *) &(t->date_entered))+4),
	      t->subject);
      /* date */
      fprintf(fp, "\t  Date: %s", ctime((time_t *) &(t->date_entered)));
      /* author */
      fprintf(fp, "\tAuthor: %s\n", t->author);
      /* signature */
      if (t->signature && *(t->signature) != '\0' &&
	  strcmp(t->author, t->signature))
	fprintf(fp, "\t\t(a.k.a. %s)\n", t->signature);
    }

  /* close the file */
  if (fclose(fp) == EOF) {
    code = (long) errno;
    strcpy(luv_context, summaryfname);
  }
  if (code) return(code);

  /* put summary in archive */
  if (code=luc_add_trn(&luv_amtg, summaryfname, title,
		       luc_fullname(mp->nb.user_id), 0, np))
    return(code);

  /* append copies of transactions to chain beginning with summary */
  for(i=n; i; i=t->nref) {
    /* get info on trn */
    if (code=luc_trn_info(mp, i, &t)) break;
    /* get filename */
    if (code=luc_trn_file(mp, i, &fname)) return(code);
    /* copy to archive */
    code=luc_add_trn(&luv_amtg, fname, t->subject, t->signature,
		     *np, &foo);
    luc_free_file(&fname);
    if (code) break;
    /* mark with appropriate flags, ignore error */
    if (t->flags) {
      if (code=luc_mark(&luv_amtg, foo, t->flags)) break;;
      if (i==n) if (code=luc_mark(&luv_amtg, *np, t->flags)) break;
    }
  }
  if (code) return(code);

  /* delete old stuff */
  for(i=n; i; i=t->nref) {
    /* get info on trn */
    if (code=luc_trn_info(mp, i, &t)) break;
    if (code=luc_delete(mp, i)) break;
  }
  return(code);
}
