/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *  Portions Copyright (C) 1994 Rafael W. Luebbert
 *  Portions Copyright (C) 1995 Jeff Shepherd
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define _KERNEL
#include "ixemul.h"
#include "kprintf.h"

#include <exec/memory.h>
#include <dos/var.h>

#include <string.h>
#include <unistd.h>

/* not changed after the library is initialized */
struct ixemul_base		*ixemulbase = NULL;

struct ExecBase			*SysBase = NULL;
struct DosLibrary		*DOSBase = NULL;
struct MathIeeeSingBasBase	*MathIeeeSingBasBase = NULL;
struct MathIeeeDoubBasBase	*MathIeeeDoubBasBase = NULL;
struct MathIeeeDoubTransBase	*MathIeeeDoubTransBase = NULL;
struct Library                  *muBase = NULL;

static struct
{
  void		**base;
  char		*name;
  ULONG         minver;  /* minimal version          */
  BOOL          opt;     /* no fail if not available */
}
ix_libs[] =
{
  { (void **)&DOSBase, "dos.library" },
  { (void **)&MathIeeeSingBasBase, "mathieeesingbas.library" },
  { (void **)&MathIeeeDoubBasBase, "mathieeedoubbas.library" },
  { (void **)&MathIeeeDoubTransBase, "mathieeedoubtrans.library" },
  { (void **)&muBase, "multiuser.library", 39, TRUE },
  { NULL, NULL }
};

static void ix_task_switcher(void)
{
  for (;;)
    Wait(1 << 31);
}

int open_libraries(void)
{
  int i;

  for (i = 0; ix_libs[i].base; i++)
    if (!(*(ix_libs[i].base) = (void *)OpenLibrary(ix_libs[i].name, ix_libs[i].minver))
        && !ix_libs[i].opt)
      {
	ix_panic("%s required!", ix_libs[i].name);
	return 0;
      }
  return 1;
}

void close_libraries(void)
{
  int i;
  
  for (i = 0; ix_libs[i].base; i++)
    if (*ix_libs[i].base)
      CloseLibrary(*ix_libs[i].base);
}

struct ixemul_base *ix_init (struct ixemul_base *ixbase)
{
  int i;
  char buf[256];
  extern char hostname[];  /* in getcrap.c */

  ixemulbase = ixbase;
  if (!open_libraries())
    {
      close_libraries();
      return 0;
    }

  ixbase->ix_file_tab = (struct file *)AllocMem (NOFILE * sizeof(struct file), MEMF_PUBLIC | MEMF_CLEAR);
  ixbase->ix_fileNFILE = ixbase->ix_file_tab + NOFILE;
  ixbase->ix_lastf = ixbase->ix_file_tab;
  
  memset(&ixbase->ix_notify_request, 0, sizeof(ixbase->ix_notify_request));
  memset(&ixbase->ix_ptys, 0, sizeof(ixbase->ix_ptys));

  /* Read the GMT offset. This environment variable is 5 bytes long. The
     first 4 form a long that contains the offset in seconds and the fifth
     byte is ignored. */
  if (GetVar("IXGMTOFFSET", buf, 6, GVF_BINARY_VAR) == 5 && IoErr() == 5)
    ix_set_gmt_offset(*((long *)buf));
  else
    ix_set_gmt_offset(0);

  if (GetVar(IX_ENV_SETTINGS, buf, sizeof(struct ix_settings) + 1, GVF_BINARY_VAR) == sizeof(struct ix_settings))
    ix_set_settings((struct ix_settings *)buf);
  else
    ix_set_settings(ix_get_default_settings());

  /* Set the hostname if the environment variable HOSTNAME exists. */
  if (GetVar("HOSTNAME", buf, 64, 0) > 0)
    strcpy(hostname, buf);

  /* initialize the list structures for the allocator */
  init_buddy ();

  ixbase->ix_task_switcher = CreateTask("ixemul task switcher", 9, ix_task_switcher, 1000);

  /* initialize port number for AF_UNIX(localhost) sockets */
  ixbase->ix_next_free_port = 1024;

  if (ixbase->ix_file_tab)
    {
      InitSemaphore (& ixbase->ix_semaph);

      configure_context_switch ();

      NewList ((struct List *) &ixbase->ix_socket_list);

      for (i = 0; i < IX_NUM_SLEEP_QUEUES; i++)
        NewList ((struct List *) &ixbase->ix_sleep_queues[i]);

      ixbase->ix_global_environment = NULL;

      return ixbase;
    }

  if (ixbase->ix_task_switcher)
    {
      Forbid();
      DeleteTask(ixbase->ix_task_switcher);
      Permit();
    }

  if (ixbase->ix_file_tab)
    FreeMem (ixbase->ix_file_tab, NOFILE * sizeof(struct file));
  else
    ix_panic ("out of memory");

  close_libraries();
  
  return 0;
}      

void ix_lock_base (void)
{
  u.u_oldmask = u.p_sigmask;
  u.p_sigmask = ~0;
  ObtainSemaphore (& ix.ix_semaph);
}

void ix_unlock_base (void)
{
  ReleaseSemaphore (& ix.ix_semaph);
  u.p_sigmask = u.u_oldmask;
}
