/*
**	Start.c
**
**	The program entry point
**
**	Copyright © 1990-1995 by Olaf `Olsen' Barthel
**		All Rights Reserved
*/

#include <intuition/intuitionbase.h>

#include <workbench/workbench.h>

#include <exec/execbase.h>

#include <dos/dosextens.h>

#include <clib/intuition_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

#include <pragmas/intuition_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>

#include <string.h>

	/* Standard topaz.font/8. */

extern struct TextAttr	 DefaultFont;

	/* exec.library base pointer. */

extern struct ExecBase	*SysBase;

	/* The appropriate machine type check. */

#if defined(CPU_ANY) || defined(CPU_any)
#define MACHINE_OKAY (1)
#endif

#if defined(CPU_68020) || defined(CPU_68030)
#define MACHINE_OKAY (SysBase -> AttnFlags & AFF_68020)
#endif

#if defined(CPU_68040)
#define MACHINE_OKAY (SysBase -> AttnFlags & AFF_68040)
#endif

	/* Start():
	 *
	 *	Program entry point, checks for the right CPU type and
	 *	runs the main program if possible.
	 */

LONG __saveds
Start(VOID)
{
		/* Get SysBase. */

	SysBase = *(struct ExecBase **)4;

		/* Is the minimum required processor type and Kickstart 2.04 (or higher)
		 * available?
		 */

	if(MACHINE_OKAY && (SysBase -> LibNode . lib_Version > 37 || (SysBase -> LibNode . lib_Version == 37 && SysBase -> SoftVer >= 175)))
		return(main());
	else
	{
		register struct Process *ThisProcess = (struct Process *)SysBase -> ThisTask;

			/* Is a Shell window available? */

		if(ThisProcess -> pr_CLI)
		{
			register struct DosLibrary *DOSBase;

				/* Show the message. */

			if(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",0))
			{
				STRPTR String;

				if(MACHINE_OKAY)
					String = "Kickstart 2.04 or higher required.\a\n";
				else
					String = "MC68020 or higher required.\a\n";

				Write(ThisProcess -> pr_COS,String,strlen(String));

				CloseLibrary(DOSBase);
			}

			return(RETURN_FAIL);
		}
		else
		{
			register struct IntuitionBase	*IntuitionBase;
			register struct WBStartup	*WBenchMsg;

				/* Wait for startup message. */

			WaitPort(&ThisProcess -> pr_MsgPort);

				/* Get the message. */

			WBenchMsg = (struct WBStartup *)GetMsg(&ThisProcess -> pr_MsgPort);

				/* Show the message. */

			if(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0))
			{
				STATIC struct IntuiText SorryText = {0,1,JAM1,6,3,&DefaultFont,(UBYTE *)"Sorry",NULL};

				struct IntuiText	*Body;
				UWORD			 Width;

				if(MACHINE_OKAY)
				{
					STATIC struct IntuiText BodyText = {0,1,JAM1,5,3,&DefaultFont,(STRPTR)"Kickstart 2.04 or higher required.",NULL};

					Body	= &BodyText;
					Width	= 309;
				}
				else
				{
					STATIC struct IntuiText BodyText = {0,1,JAM1,5,3,&DefaultFont,(UBYTE *)"MC68020 or higher required.",NULL};

					Body	= &BodyText;
					Width	= 253;
				}

				AutoRequest(NULL,Body,NULL,&SorryText,NULL,NULL,Width,46);

				CloseLibrary(IntuitionBase);
			}

				/* Return the startup message and exit. */

			Forbid();

			ReplyMsg((struct Message *)WBenchMsg);
		}
	}
}
