/****************************************************************************

 modinit.c - standard initialisation for Opus 5 modules

 ****************************************************************************/

#define _DOPUS_MODULE_DEF
#include <dopus/modules.h>

#ifndef CLIB_EXEC_PROTOS_H
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>
#endif

#ifndef EXEC_MEMORY_H
#include <exec/memory.h>
#endif

#ifndef CLIB_LOCALE_PROTOS_H
#include <clib/locale_protos.h>
#include <pragmas/locale_pragmas.h>
#endif

// SAS/C stuff
int __saveds __UserLibInit(void);
void __saveds __UserLibCleanup(void);

// The libraries we open
struct Library *DOSBase;
struct Library *DOpusBase;
struct Library *IntuitionBase;
struct Library *GfxBase;
struct Library *IconBase;
struct Library *LocaleBase;
struct Library *UtilityBase;
struct Library *LayersBase;
struct Library *WorkbenchBase;
struct Library *GadToolsBase;
struct Library *AslBase;
struct Library *DiskfontBase;
struct Library *TimerBase;
struct Library *RexxSysBase;

// Locale pointer
struct DOpusLocale *locale;

// Library initialisation code
__saveds __UserLibInit()
{
	// Initialise pointers
	DOpusBase=0;
	IntuitionBase=0;
	GfxBase=0;
	IconBase=0;
	LocaleBase=0;
	UtilityBase=0;
	LayersBase=0;
	WorkbenchBase=0;
	GadToolsBase=0;
	DiskfontBase=0;
	AslBase=0;
	RexxSysBase=0;
	locale=0;

	// Get DOS library (can't really fail)
	DOSBase=OpenLibrary("dos.library",0);

	// Open other libraries we need
	if (!(DOpusBase=OpenLibrary("dopus5.library",55)) ||
		!(IntuitionBase=OpenLibrary("intuition.library",37)) ||
		!(GfxBase=OpenLibrary("graphics.library",37)) ||
		!(IconBase=OpenLibrary("icon.library",37)) ||
		!(LayersBase=OpenLibrary("layers.library",37)) ||
		!(GadToolsBase=OpenLibrary("gadtools.library",37)) ||
		!(AslBase=OpenLibrary("asl.library",37)) ||
		!(DiskfontBase=OpenLibrary("diskfont.library",37)) ||
		!(TimerBase=GetTimerBase()) ||
		!(UtilityBase=OpenLibrary("utility.library",37))) return 1;

	// Libraries we don't need but want
	WorkbenchBase=OpenLibrary("workbench.library",0);
	RexxSysBase=OpenLibrary("rexxsyslib.library",0);

	// Allocate and open locale data
	if (!(locale=AllocVec(sizeof(struct DOpusLocale),MEMF_CLEAR)))
		return 1;
	init_locale_data(locale);

	// Open locale library
	if (LocaleBase=OpenLibrary("locale.library",38))
	{
		// Initialise catalog
		locale->li_LocaleBase=LocaleBase;
		if (module_info.locale_name)
			locale->li_Catalog=OpenCatalogA(NULL,module_info.locale_name,0);
		locale->li_Locale=OpenLocale(0);
	}

	// Succeeded
	return 0;
}


// Clean up
void __saveds __UserLibCleanup()
{
	// Free locale stuff
	if (locale)
	{
		if (LocaleBase)
		{
			CloseLocale(locale->li_Locale);
			CloseCatalog(locale->li_Catalog);
			CloseLibrary(LocaleBase);
		}
		FreeVec(locale);
	}

	// Close libraries
	CloseLibrary(DOpusBase);
	CloseLibrary(IntuitionBase);
	CloseLibrary(GfxBase);
	CloseLibrary(IconBase);
	CloseLibrary(LayersBase);
	CloseLibrary(UtilityBase);
	CloseLibrary(WorkbenchBase);
	CloseLibrary(DiskfontBase);
	CloseLibrary(RexxSysBase);
	CloseLibrary(AslBase);
	CloseLibrary(DOSBase);
}


// This routine is called by Opus to find out what the module does
ModuleInfo *__asm __saveds L_Module_Identify(register __d0 int num)
{
	// Return module information
	if (num==-1) return &module_info;

	// Valid function number?
	if (num>module_info.function_count || !(module_info.function[num].desc)) return 0;

	// Return function description
	return (ModuleInfo *)DOpusGetString(locale,module_info.function[num].desc);
}
