/*
 * startup.c
 *
 * THIS ROUTINE IST INTENDED FOR MAXON C/C++ COMPILER ONLY !!!
 *  + faster startup
 *  + smaller code
 *  - disables C++ mode (not really a drawback ;-)
 *  - disables startup modules (library auto-opening, etc.)
 *
 * MAKE SURE TO LINK THIS MODULE WITH HIGHEST PRIORITY,
 * AND TO LINK THE PROGRAM WITHOUT STARTUP CODE
 *
 * DO NOT USE IT WITH ANY OTHER COMPILER THAN MAXON C/C++
 */

#ifdef __MAXON__

#include <pragma/exec_lib.h>
#include <exec/alerts.h>
#include <dos/dos.h>
#include <dos/dosextens.h>

struct Library *SysBase = NULL, *DOSBase = NULL;
const STRPTR __dosname__ = "dos.library";

extern LONG main(LONG c, APTR p);

/****************************************************************
	the first routine found by the linker

	  	- make sure to link without startup code
 		- make sure to link this module with highest priority

 		- don't forget, you're limited to ANSI-C now !
 ****************************************************************/
long __launch__(register __d0 LONG Argc, register __a0 APTR Argv)
{
	LONG rw = RETURN_FAIL;
	
	/* fetch ExecBase */
	SysBase = (struct Library *) *((ULONG *) 4);
	
	/* open DOS (KS2.04 minimum) */
	DOSBase = OpenLibrary(__dosname__, 37U);
	if(DOSBase)
	{
		struct Process *thistask;
		
		/* check start-mode */
		thistask = (struct Process *) FindTask(NULL);
		if(thistask->pr_CLI)
		{
			/* run from shell - WARNING: NO ARGUMENT PARSING HERE !!! */
			rw = main(Argc, Argv);
		}
		else
		{
			struct MsgPort *mp = &(thistask->pr_MsgPort);
			struct Message *sm;
			
			/* fetch WBStartupMsg */
			WaitPort(mp);
			sm = GetMsg(mp);
			
			/* run from Workbench - standard arguments */
			main(0L, sm);
			
			/* release WBStartupMsg */
			Forbid();
			ReplyMsg(sm);
		}
		
		/* close DOS */
		CloseLibrary(DOSBase);
	}
	else
	{
		Alert(AG_OpenLib | AO_DOSLib);
	}
	
	/* set return code for shell */
	return(rw);
}

#endif
