/*
**      $VER: LibInit.c 43.3 (30.11.96)
**
**      Library initializers and functions to be called by StartUp.c
**
**      (C) Copyright 1996 Andreas R. Kleinert
**      All Rights Reserved.
*/

#define __USE_SYSBASE

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <exec/execbase.h>
#include <exec/resident.h>
#include <exec/initializers.h>
#include <datatypes/pictureclass.h>

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/datatypes.h>

#include <class/classbase.h>
#include "libfuncs.h"


ULONG __saveds __stdargs L_OpenLibs(void);
void  __saveds __stdargs L_CloseLibs(void);

extern struct ClassBase *ClassBase;


struct ExecBase      *SysBase        = NULL;
struct DosLibrary    *DOSBase        = NULL;
struct IntuitionBase *IntuitionBase  = NULL;
struct GfxBase       *GfxBase        = NULL;
struct Library       *UtilityBase    = NULL;
struct Library       *DataTypesBase  = NULL;
struct Library       *SuperClassBase = NULL;
struct Library       *IFFParseBase   = NULL;


#define VERSION  43
#define REVISION 3

char __aligned ExLibName [] = "samplePNM.datatype";
char __aligned ExLibID   [] = "samplePNM 43.3 (30.11.96)";
char __aligned Copyright [] = "(C)opyright 1996 by Andreas R. Kleinert. All rights reserved.";

extern ULONG InitTab[];

extern APTR EndResident; /* below */

struct Resident __aligned ROMTag =
{
 RTC_MATCHWORD,
 &ROMTag,
 &EndResident,
 RTF_AUTOINIT,
 VERSION,
 NT_LIBRARY,
 REVISION,
 &ExLibName[0],
 &ExLibID[0],
 &InitTab[0]
};

APTR EndResident;

struct MyDataInit
{
 UWORD ainit1; UWORD binit1; UWORD ln_type;
 UBYTE ainit2; UBYTE binit2; ULONG ln_name2;
 UWORD ainit3; UWORD binit3; UWORD lib_flags;
 UWORD ainit4; UWORD binit4; UWORD lib_version;
 UWORD ainit5; UWORD binit5; UWORD lib_revision;
 UBYTE ainit6; UBYTE binit6; ULONG lib_idstring2;
 ULONG end;
} DataTab =
{
 INITBYTE(OFFSET(Node,         ln_Type),      NT_LIBRARY),
 0x80, (UBYTE) OFFSET(Node,    ln_Name),      (ULONG) &ExLibName[0],
 INITBYTE(OFFSET(Library,      lib_Flags),    LIBF_SUMUSED|LIBF_CHANGED),
 INITWORD(OFFSET(Library,      lib_Version),  VERSION),
 INITWORD(OFFSET(Library,      lib_Revision), REVISION),
 0x80, (UBYTE) OFFSET(Library, lib_IdString), (ULONG) &ExLibID[0],
 (ULONG) 0
};

ULONG __saveds __stdargs L_OpenLibs(void)
{
 SysBase = (*((struct ExecBase **) 4));

 DOSBase = (APTR) OpenLibrary("dos.library", 39);
 if(!DOSBase) return(FALSE);

 IntuitionBase = (APTR) OpenLibrary("intuition.library", 39);
 if(!IntuitionBase) return(FALSE);

 GfxBase = (APTR) OpenLibrary("graphics.library", 39);
 if(!GfxBase) return(FALSE);

 UtilityBase = (APTR) OpenLibrary("utility.library", 39);
 if(!UtilityBase) return(FALSE);

 DataTypesBase = (APTR) OpenLibrary("datatypes.library", 39);
 if(!DataTypesBase) return(FALSE);

 SuperClassBase = (APTR) OpenLibrary("datatypes/picture.datatype", 39);
 if(!SuperClassBase) return(FALSE);

 IFFParseBase = (APTR) OpenLibrary("iffparse.library", 37);
 if(!IFFParseBase) return(FALSE);

 ClassBase->cb_DOSBase        = (APTR) DOSBase;
 ClassBase->cb_IntuitionBase  = (APTR) IntuitionBase;
 ClassBase->cb_GfxBase        = (APTR) GfxBase;
 ClassBase->cb_UtilityBase    = (APTR) UtilityBase;
 ClassBase->cb_DataTypesBase  = (APTR) DataTypesBase;
 ClassBase->cb_SuperClassBase = (APTR) SuperClassBase;
 ClassBase->cb_IFFParseBase   = (APTR) IFFParseBase;

 if(ClassBase->cb_Class = initClass(ClassBase)) return(TRUE);

 return(FALSE);
}

void __saveds __stdargs L_CloseLibs(void)
{
 if(ClassBase->cb_Class) FreeClass(ClassBase->cb_Class);

 if(IFFParseBase)   CloseLibrary((APTR) IFFParseBase);
 if(SuperClassBase) CloseLibrary((APTR) SuperClassBase);
 if(DataTypesBase)  CloseLibrary((APTR) DataTypesBase);
 if(UtilityBase)    CloseLibrary((APTR) UtilityBase);
 if(GfxBase)        CloseLibrary((APTR) GfxBase);
 if(IntuitionBase)  CloseLibrary((APTR) IntuitionBase);
 if(DOSBase)        CloseLibrary((APTR) DOSBase);
}
