/* ??? Uses non-ANSI mkdir */

/* General Athena lens routines */

#include <assert.h>
#include <stdio.h>
#include <errno.h>

#include "unixprotos.h"

#include "memory.h"
#include "FTreg.h"
#include "verbrgy.h"
#include "bool.h"
#include "mh_folder.h"
#include "rm_folder.h"
#include "RSreg.h"
#include "MTreg.h"
#include "verbrgy.h"
#include "data.h"
#include "sysreg.h"
#include "profile.h"
#include "al.h"
#include "text.h"

int Alwarn = 1;

static Bool Al_initialized = Bool_FALSE;

static char msg_buf[200];

NORET Al_suppress_warnings(NOARGS)
{
  Alwarn = 0;
}

NORET Al_enable_warnings(NOARGS)
{
  Alwarn = 1;
}

NORET Al_fatal_error(message)
     const char *message;
{
  (NORET)fprintf(stderr, "%s\n", message);
  exit(1);
}

NORET Al_fatal_error1(message, arg1)
     const char *message;
     const char *arg1;
{
  (NORET)sprintf(&msg_buf[0], message, arg1);
  Al_fatal_error(&msg_buf[0]);
}

NORET Al_fatal_error2(message, arg1, arg2)
     const char *message;
     const char *arg1;
     const char *arg2;
{
  (NORET)sprintf(&msg_buf[0], message, arg1, arg2);
  Al_fatal_error(&msg_buf[0]);
}

NORET Al_warning(message)
     const char *message;
{
  if (Alwarn == 1) 
    (NORET)fprintf(stderr, "%s\n", message);
}

NORET Al_warning1(message, arg1)
     const char *message;
     const char *arg1;
{
  (NORET)sprintf(&msg_buf[0], message, arg1);
  Al_warning(&msg_buf[0]);
}

NORET Al_warning2(message, arg1, arg2)
     const char *message;
     const char *arg1;
     const char *arg2;
{
  (NORET)sprintf(&msg_buf[0], message, arg1, arg2);
  Al_warning(&msg_buf[0]);
}

/* Calls all the required init procedures */

static long init_leaked_bytes = 0;

NORET Al_init(NOARGS)
{
  assert(Al_initialized == Bool_FALSE);

  AlProfile_init();
  AlProfile_setup();

  AlText_init();
  {
    const char *lens_dir;

    if (AlProfile_has_value(ALPROF_LENSDIR) == Bool_FALSE)
      Al_fatal_error("System lens profile is invalid (no LensDirectory)");
    lens_dir = AlProfile_get_CP_val(ALPROF_LENSDIR) ;
    if (mkdir(lens_dir,0777) != 0 &&
	errno != EEXIST) {
      assert(errno != EFAULT);
      Al_fatal_error1("Could not create %s", lens_dir);
    }
  }

  AlFTreg_init();
  AlMHfolder_init();
  AlRMFolder_init();

  AlRSreg_init();
/*  if (AlRSreg_init() == Bool_FALSE)
    Al_fatal_error("Ruleset registry initialization failed.");
*/
  AlVerbReg_init();

  if (AlData_init() == Bool_FALSE)
    Al_fatal_error("Data collection initialization failed.");

  UserRegisterRequests();
  SystemRegisterRequests();
  if (AlMTreg_init() == Bool_FALSE) /* N.B. THIS MUST GO AFTER FT'S ARE DEFINED! */
    Al_fatal_error("Message type registry initialization failed.");

  init_leaked_bytes = Memory_unfreed_bytes();

  /* set up appropriate X resource file paths */
  if (AlProfile_has_value(ALPROF_APP_XRESOURCES)==Bool_TRUE) {
    setenv("XAPPLRESDIR",
	   AlProfile_get_CP_val(ALPROF_APP_XRESOURCES),
	   1);
  }

  Al_initialized = Bool_TRUE;
}

NORET Al_cleanup(NOARGS)
{
  long num_bytes_leaked;	/* Always 0 if memory.c compiled without
				   leak checking */

  /* put cleanup routine calls here */
  AlText_cleanup();
  AlData_save_state();
  if ((num_bytes_leaked = Memory_unfreed_bytes()) != 0)
    fprintf(stderr, 
	    "Leaked storage: %ld (%ld init)\n", 
	    num_bytes_leaked, 
	    init_leaked_bytes);
}
