/*
**	$VER: accountslib.c 1.5 (23.02.94)
**
**	accounts.library standard functions
**
**	© Copyright 1994 by Norbert Püschel
**	All Rights Reserved
*/

#include <proto/exec.h>
#include <proto/dos.h>

#include <dos/dostags.h>

#include <debug.h>

struct DosLibrary *DOSBase;

volatile struct Process *guardian = 0;

extern void guardian_func(void);

BOOL __saveds __asm LIBLocalAlloc(register __d0 struct Library *Base)

{
  Forbid();
  if(guardian == 0) { /* restart guardian process */
    guardian = CreateNewProcTags(NP_Entry,guardian_func,
                                 NP_Name,"Accounts Daemon",
                                 NP_Priority,0,
                                 NP_WindowPtr,-1,
                                 TAG_DONE);
    Permit();
    if(guardian) {
      D(bug("Guardian started\n"));
      if(DoPkt0(&(((struct Process *)guardian)->pr_MsgPort),0)) return(TRUE);
      D(bug("Guardian failed\n"));
    }
    return(FALSE);
  }
  else {
    Permit();
  }
  return(TRUE);
}

BOOL __saveds __asm LIBLocalFree(register __d0 struct Library *Base) 

{
  return(TRUE);
}

BOOL __saveds __asm LIBGlobalAlloc(register __d0 struct Library *Base)

{
  DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",37);
  if(DOSBase) {
    D(bug("dos.library opened\n"));
    return(TRUE);
  }
  D(bug("dos.library could not be opened\n"));
  return(FALSE);
}

BOOL __saveds __asm LIBGlobalFree(register __d0 struct Library *Base) 

{
  if(guardian) {
    D(bug("Trying to kill guardian ...\n"));
    Signal((struct Task *)guardian,SIGBREAKF_CTRL_C);
    return(FALSE);
  }

  D(bug("Expunging accounts.library\n"));

  CloseLibrary((struct Library *)DOSBase);
  return(TRUE);
}
