/**********************************************************************
 * lucy library's initialization routine
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_init.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_init.c,v 1.2 91/08/01 14:31:45 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>
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <hesiod.h>
#include "lucy/lucy.h"
#include "lucy/memory.h"

#ifndef lint
static char rcsid_luc_init_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/lib/RCS/luc_init.c,v 1.2 91/08/01 14:31:45 brlewis Exp Locker: brlewis $";
#endif /* lint */

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

long
luc_init()
{
  long code;
  register char *s;
  char **hinfo;
  int i;

  init_dsc_err_tbl();
  init_lucy_err_tbl();

  /* get server name from hesiod */
  hinfo = hes_resolve("lucy", "sloc");
  if (hinfo && *hinfo) luv_host = *hinfo;

  /* let environment variables override defaults */
  if (s = getenv("LUCYQ")) luv_qdirname = s;
  if (s = getenv("LUCYB")) luv_bdirname = s;
  if (s = getenv("LUCYA")) luv_adirname = s;
  if (s = getenv("LUCYMAIL")) luv_email = s;
  if (s = getenv("LUCYHOST")) luv_host = s;

  if (s = getenv("LUCYPROG"))  { luv_prog = atoi(s); }
  else {
    /* determine RPC program number from lucy's email address */
    /* (see RPC manual for more info on RPC program numbers) */
    for(i=0 ; i < (sizeof(luv_prog) << 3) && luv_email[i] != '\0' ; i++)
      luv_prog += ((int) luv_email[i]) << i;
    luv_prog &= 0x5fffffff;	/* maximum transient prog number */
    luv_prog |= 0x40000000;	/* minimum transient prog number */
  }

  /* create lucy directory */
  luv_topdir = malloc(15);
  if (!luv_topdir) {
    luv_context[0] = '\0';	/* error context = "" */
    return((long) errno);
  }
  sprintf(luv_topdir, "/tmp/dir%d", getuid());
  if (mkdir(luv_topdir, 0700) && errno != EEXIST) {
    sprintf(luv_context, "making directory \"%s\"", luv_topdir);
    return((long) errno);
  }

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

  /* 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);
}
