/**********************************************************************
 * lucy library's initialization routine
 *
 * $Author: brlewis $
 * $Source$
 * $Header$
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/

#include <mit-copyright.h>
#include <stdio.h>
#include <strings.h>
#include <sys/errno.h>
#include "lucy.h"
#include "memory.h"

#ifndef lint
static char rcsid_luc_init_c[] = "$Header$";
#endif /* lint */

/**********************************************************************
 * luc_init()
 *
 * - initializes everything for lucy library
 * - returns error code
 **********************************************************************/

long
luc_init()
{
  long code;
  static char *template = "/tmp/dirXXXXXX";
  char *s;

/*  init_dsc_err_tbl();  --- this line seems to cause a compile error, ukim */

  s = getenv("LUCYHOST");
  if (s) luv_host = s;
  /* XXX add code for hesiod */

  s = getenv("LUCYQ");
  if (s) luv_qdirname = s;

  s = getenv("LUCYB");
  if (s) luv_bdirname = s;

  s = getenv("LUCYA");
  if (s) luv_adirname = s;

  s = getenv("LUCYMAIL");
  if (s) luv_email = s;

  luv_topdir = newstring(template);
  if (!luv_topdir) {
    luv_context[0] = '\0';	/* error context = "" */
    return((long) errno);
  }
  strcpy(luv_topdir, template);
  if (mkdir(mktemp(luv_topdir), 0700)) {
    sprintf(luv_context, "making directory \"%s\"", luv_topdir);
    return((long) errno);
  }

  /* Zero meeting structures */
  bzero(&luv_qmtg, sizeof(struct lmeeting));
  bzero(&luv_bmtg, sizeof(struct lmeeting));
  bzero(&luv_amtg, sizeof(struct lmeeting));

  /* Fill in browser meeting */
  if (code = luc_fill_nb(&(luv_bmtg.nb), luv_host, luv_bdirname)) return(code);
  if (code = luc_fill_mtg(&luv_bmtg)) return(code);
  luv_curmtgp = &luv_bmtg;

  /* Fill in queue meeting.  If successful, make queue current mtg. */
  if (luc_fill_nb(&(luv_qmtg.nb), luv_host, luv_qdirname)) return(0L);
  if (luc_fill_mtg(&luv_qmtg)) return(0L);
  luv_curmtgp = &luv_qmtg;

  /* Maybe fill in archive meeting */
  if (luc_fill_nb(&(luv_amtg.nb), luv_host, luv_adirname)) return(0L);
  luc_fill_mtg(&luv_amtg);

  return(0L);
}
