//************************************
//
// Name : Librarian.c
//
//************************************


//**** Header files

//** OS Include files
#include <workbench/icon.h>
#include <workbench/workbench.h>
#include <workbench/wbstart.h>  // for KS2 handler
#include <libraries/wbstart.h>
#include <dos/dos.h>
#include <dos/dostags.h>

//** OS function prototypes
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

//** OS function inline calls
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>

//** ANSI C includes

//** application include

#include "Librarian.h"
#include "MPMGClass.h"
#include "TBUTClass.h"


//**** Library bases

struct Library *WorkbenchBase=NULL;
struct Library *WBStartBase=NULL;
struct Library *CxBase=NULL;
struct Library *UtilityBase=NULL;
struct Library *IconBase=NULL;
struct Library *AslBase = NULL;
struct Library *GfxBase = NULL;
struct Library *IntuitionBase=NULL;
struct Library *LayersBase=NULL;
struct MsgPort *HandlerReply=NULL;
Class *MPMGClass=NULL;
Class *TBUTClass=NULL;


//**** Private functions

int StartHandler(void) {
  BPTR ifh;
  BPTR ofh;
  int i;
  struct MsgPort *hp=NULL;

  if (ifh=Open("NIL:",MODE_NEWFILE)) {
    if (ofh=Open("NIL:",MODE_OLDFILE)) {
      /* Start handler */
      if (SystemTags(WBS_LOADNAME,SYS_Input,ifh,
	   SYS_Output,ofh,     SYS_Asynch,TRUE,
	   SYS_UserShell,TRUE, NP_ConsoleTask,NULL,
	   NP_WindowPtr,NULL,  TAG_DONE) != -1)
      {
	for (i=0; (i<10) && (NULL==hp); i++, Delay(25)) {
	  Forbid();
	    hp=FindPort(WBS_PORTNAME);
	  Permit();
	} // for
      } /* Handler not started, close file handles */
      else {
	Close(ofh);
	Close(ifh);
      } // if !SystemTags
    } // if open
    else {
      Close(ifh);
    } // if !open ofh
  } // if open

  if (NULL!=hp) { // try to create a msg port
    HandlerReply=CreateMsgPort();
  } //

  return (NULL==HandlerReply)?0:1;
} // int StartHandler


//**** Aux functions


int OpenLibs(void) {
  WBStartBase   = OpenLibrary(WBSTART_NAME,WBSTART_VERSION);
  WorkbenchBase = OpenLibrary(WORKBENCH_NAME,37);
  CxBase        = OpenLibrary("commodities.library",37);
  UtilityBase   = OpenLibrary("utility.library",37);
  IconBase      = OpenLibrary(ICONNAME,37);
  AslBase       = OpenLibrary("asl.library",37);
  GfxBase       = OpenLibrary("graphics.library",37);
  IntuitionBase = OpenLibrary("intuition.library",37);
  LayersBase    = OpenLibrary("layers.library",37);
  DataTypesBase = OpenLibrary("datatypes.library",0);
  MPMGClass     = MPMG_CreateClass();
  TBUTClass     = TBUT_CreateClass();

  return ( ( (WBStartBase) || (1==StartHandler()) ) &&
	   (WorkbenchBase) &&
	   (IconBase)    && (CxBase)     && (UtilityBase) &&
	   (GfxBase)     && (AslBase)    && (IntuitionBase) &&
	   (LayersBase)  && (MPMGClass)  && (TBUTClass) );
} // OpenLibs

void CloseLibs(void) {
  struct Message *msg;

  if (TBUTClass)        TBUT_DisposeClass(TBUTClass);
  if (MPMGClass)        MPMG_DisposeClass(MPMGClass);
  if (WBStartBase)      CloseLibrary(WBStartBase);
  if (WorkbenchBase)    CloseLibrary(WorkbenchBase);
  if (CxBase)           CloseLibrary(CxBase);
  if (UtilityBase)      CloseLibrary(UtilityBase);
  if (IconBase)         CloseLibrary(IconBase);
  if (AslBase)          CloseLibrary(AslBase);
  if (GfxBase)          CloseLibrary(GfxBase);
  if (IntuitionBase)    CloseLibrary(IntuitionBase);
  if (LayersBase)       CloseLibrary(LayersBase);
  if (DataTypesBase)    CloseLibrary(DataTypesBase);

  if (HandlerReply) {
    while (msg=GetMsg(HandlerReply)) ReplyMsg((struct Message *)msg);
    DeleteMsgPort(HandlerReply);
    HandlerReply=NULL;
  }

} // CloseLibs()

//**** End of file

