
// TetiSoft: This file contains the startup code which MUST run on a 68000
// even if compiled for 68060 (it must show an alert and exit).
// If you change something, verify that the 68000 and 68060 versions of
// the generated object file are equal.

/**
 * INCLUDES
 **/
#ifndef T1GST
#include "global.h"
#endif
#include <proto/exec.h>
#include <proto/intuition.h>

/**
 * GLOBAL READ-ONLY VARIABLES
 **/

static const char versionstring[] =
"$VER: type1.library"
#ifdef _M68020
"_68020"
#else
"_68000"
#endif
#ifdef _M68881
"/FPU"
#endif
" 3.0 "
__AMIGADATE__	// TetiSoft: After 29.1.2000 __AMIGADATE__ is one day in the future...
" © Amish S. Dave";

/**
 * GLOBAL READ-WRITE VARIABLES
 **/

struct ExecBase *SysBase;
struct DosLibrary *DOSBase;
struct Library *UtilityBase;
#ifdef _IEEE
struct Library *MathIeeeDoubBasBase;
struct Library *MathIeeeDoubTransBase;
#endif
#ifdef _FFP
struct Library *MathBase;
struct Library *MathTransBase;
#endif
struct IntuitionBase *IntuitionBase;	// Only opened and used for DisplayAlert()



// TetiSoft: Using libinit.o instead of libinitr.o requires the following

static const UBYTE ExecAlert[]      = "\x0\x40\x14type1.library requires kickstart version 39 or higher.\0\0";
static const UBYTE DOSAlert[]       = "\x0\x40\x14type1.library requires dos.library version 36 or higher.\0\0";
static const UBYTE UtilityAlert[]   = "\x0\x40\x14type1.library requires utility.library version 36 or higher.\0\0";
#ifdef _IEEE
static const UBYTE IEEEBasAlert[]   = "\x0\x40\x14type1.library requires mathieeedoubbas.library.\0\0";
static const UBYTE IEEETransAlert[] = "\x0\x40\x14type1.library requires mathieeedoubtrans.library.\0\0";
#endif
#ifdef _FFP
static const UBYTE FFPAlert[]       = "\x0\x40\x14type1.library requires mathffp.library.\0\0";
static const UBYTE FFPTransAlert[]  = "\x0\x40\x14type1.library requires mathtrans.library.\0\0";
#endif
#ifdef _M68020
static const UBYTE CPUAlert[]       = "\x0\x40\x14type1.library requires 68020 CPU or higher.\0\0";
#endif
#ifdef _M68881
static const UBYTE FPUAlert[]       = "\x0\x40\x14type1.library requires FPU.\0\0";
#endif

static void ShowAlert(const UBYTE *AlertString)
{
	if (!IntuitionBase)
		IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
	if (IntuitionBase)
	{
		if (IntuitionBase->LibNode.lib_Version < 39)
			DisplayAlert(RECOVERY_ALERT, AlertString, 40);
		else
			TimedDisplayAlert(RECOVERY_ALERT, AlertString, 40, 500);
	}
}

/**
 * UserLibCleanup
 **/
void __saveds __asm __UserLibCleanup(register __a6 struct Library *Type1Base)
{
	if (IntuitionBase)
		CloseLibrary((struct Library *)IntuitionBase);
#ifdef _FFP
	if (MathTransBase)
		CloseLibrary(MathTransBase);
	if (MathBase)
		CloseLibrary(MathBase);
#endif
#ifdef _IEEE
	if (MathIeeeDoubTransBase)
		CloseLibrary(MathIeeeDoubTransBase);
	if (MathIeeeDoubBasBase)
		CloseLibrary(MathIeeeDoubBasBase);
#endif
	if (UtilityBase)
		CloseLibrary(UtilityBase);
	if (DOSBase)
		CloseLibrary((struct Library *)DOSBase);
}

/**
 * UserLibInit
 **/
long __saveds __asm __UserLibInit(register __a6 struct Library *Type1Base)
{
	SysBase = *((struct ExecBase**)4);
	if(!SysBase)		// this is just a joke ;-)
		return 1;
	if (SysBase->LibNode.lib_Version < 39)	// CreatePool() needs V39
	{
		ShowAlert(ExecAlert);
		return 1;
	}
#ifdef _M68020
	if (!(SysBase->AttnFlags & AFF_68020))	// 020 version needs 020
	{
		ShowAlert(CPUAlert);
		return 1;
	}
#endif
#ifdef _M68881
	if (!(SysBase->AttnFlags & AFF_68881))	// FPU version needs FPU
	{
		ShowAlert(FPUAlert);
		return 1;
	}
#endif
	if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 36)) // FRead()
	    if (UtilityBase = OpenLibrary("utility.library", 36))	   // NextTagItem()
#ifdef _IEEE
		if (MathIeeeDoubBasBase = OpenLibrary("mathieeedoubbas.library", 0))
		    if (MathIeeeDoubTransBase = OpenLibrary("mathieeedoubtrans.library", 0))
#endif
#ifdef _FFP
			if (MathBase = OpenLibrary("mathffp.library", 0))
			    if (MathTransBase = OpenLibrary("mathtrans.library", 0))
#endif
				return 0;	// OK
#ifdef _FFP
			    else ShowAlert(FFPTransAlert);
			else ShowAlert(FFPAlert);
#endif
#ifdef _IEEE
		    else ShowAlert(IEEETransAlert);
		else ShowAlert(IEEEBasAlert);
#endif
	    else ShowAlert(UtilityAlert);
	else ShowAlert(DOSAlert);

	__UserLibCleanup(Type1Base);

	return 1;
}
