/****************************************************************************
*
* $RCSfile: Main.c $
* $Revision: 1.6 $
* $Date: 1997/09/13 11:22:54 $
* $Author: ssolie $
*
*****************************************************************************
*
* Copyright (c) 1997 Software Evolution.  All Rights Reserved.
*
*****************************************************************************
*
* Main.c -- Main entry point
*
* This file contains the source for the main entry point which handles
* setting up the various objects and starting the controller.
*/

#include <exec/alerts.h>
#include <dos/dos.h>
#include <intuition/intuitionbase.h>
#include <workbench/startup.h>

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

#include "Debug.h"
#include "LibraryManager.h"
#include "LocaleManager.h"
#include "Controller.h"
#include "ArgParser.h"


/*** Global variables ***/
LocaleManager			locale_mgr;
struct IntuitionBase	*IntuitionBase;
struct Library			*CxBase;
struct Library			*UtilityBase;
struct Library			*IFFParseBase;
struct Library			*IconBase;


/*** Local constants ***/
#define APPNAME				"docket"
#define VERSION				"2"
#define REVISION			"0"
#define DATE				"(13.9.97)"

#define INTUITION_LIB		"intuition.library"
#define INTUITION_VER		39
#define COMMODITIES_LIB		"commodities.library"
#define COMMODITIES_VER		39
#define UTILITY_LIB			"utility.library"
#define UTILITY_VER			39
#define IFFPARSE_LIB		"iffparse.library"
#define IFFPARSE_VER		39
#define ICON_LIB			"icon.library"
#define ICON_VER			39

const STRPTR VERSION_STR	 = "$VER: "APPNAME" "VERSION"."REVISION" "DATE;
const STRPTR DOCKET_CATALOG	 = "docket.catalog";
const STRPTR SHELL_TEMPLATE	 = "CX_PRIORITY/K/N,CX_POPKEY/K,CX_POPUP/K";

const STRPTR ARG_CX_PRIORITY = "CX_PRIORITY";	/* CX priority */
const STRPTR ARG_CX_POPKEY	 = "CX_POPKEY";		/* CX popup key */
const STRPTR ARG_CX_POPUP	 = "CX_POPUP";		/* CX popup flag */

const LONG   DEF_CX_PRIORITY = 0;				/* default priority */
const STRPTR DEF_CX_POPKEY	 = "ctrl alt d";	/* default popkey */
const BOOL   DEF_CX_POPUP	 = FALSE;			/* default popup flag */


/*** Local data types ***/
typedef struct {
	struct Library **base;	/* pointer to library base pointer */
	STRPTR name;			/* library name */
	ULONG version;			/* library version */
} LibraryTemplate;


/*** Local function prototypes ***/
LONG main(struct Process *process, struct WBStartup *wbstartup);


/*
 * main -- Main entry point
 *
 * This function sets up Docket for processing and acts as a driver for the
 * lower level functions.  Returns a DOS error code on exit.
 */
LONG main(struct Process *process, struct WBStartup *wbstartup)
{
	STATIC LibraryTemplate libs[] = {
		{(struct Library**)&IntuitionBase, INTUITION_LIB, INTUITION_VER},
		{&CxBase, COMMODITIES_LIB, COMMODITIES_VER},
		{&UtilityBase, UTILITY_LIB, UTILITY_VER},
		{&IFFParseBase, IFFPARSE_LIB, IFFPARSE_VER},
		{&IconBase, ICON_LIB, ICON_VER},
		{NULL}
	};
	LibraryManager lib_mgr;
	ArgParser parser;
	Controller controller;
	struct Library *library;
	STRPTR cx_popkey;
	UWORD i;
	BYTE cx_priority;
	BOOL cx_popup;

	D(bug("main(%lx, %lx)...\n", process, wbstartup));

	/*** Main library manager for libraries that are always needed ***/
	lib_mgr = newLibraryManager(NULL);
	if ( lib_mgr == NULL )  {
		Alert(AT_Recovery | AG_NoMemory);
		return(RETURN_FAIL);
	}

	/*** Create optional locale ***/
	locale_mgr = newLocaleManager(DOCKET_CATALOG, NULL);

	/*** Open libraries needed for the lifetime of the program ***/
	for ( i = 0; libs[i].base != NULL; i++ )  {
		library = openLibrary(lib_mgr, libs[i].name, libs[i].version);
		if ( library != NULL )
			*(libs[i].base) = library;
		else  {
			D2(bug(" failed to open %s\n", libs[i].name));
			deleteLocaleManager(locale_mgr);
			deleteLibraryManager(lib_mgr);
			return(RETURN_FAIL);
		}
	}

	/*** Parse the arguments ***/
	parser = newArgParser(SHELL_TEMPLATE, wbstartup);
	cx_priority =
		(BYTE)getArgParserInt(parser, ARG_CX_PRIORITY, DEF_CX_PRIORITY);
	cx_popkey = getArgParserStr(parser, ARG_CX_POPKEY, DEF_CX_POPKEY);
	cx_popup = getArgParserBool(parser, ARG_CX_POPUP, DEF_CX_POPUP);

	/*** Run the docket! ***/
	controller = newController(cx_priority, cx_popup, cx_popkey);
	handleController(controller);
	deleteController(controller);

	/*** Cleanup and exit ***/
	deleteArgParser(parser);
	deleteLocaleManager(locale_mgr);
	deleteLibraryManager(lib_mgr);

	D(bug("done.\n\n"));

	return(RETURN_OK);
}
