/*
**    GrabKEY
**
**        © 1996 by Timo C. Nentwig
**        All Rights Reserved !
**
**        Tcn@techbase.in-berlin.de
**
**
*/

/// #include

#include "gk_GST.h"
#include "gk_Protos.h"

///
/// proto

static BOOL    OpenLibs     (VOID);
static VOID    CloseLibs    (VOID);

///

	// System libraries

struct    Library         *CxBase        = NULL;
struct    Library         *DataTypesBase = NULL;
struct    Library         *GadToolsBase  = NULL;
struct    GfxBase         *GfxBase       = NULL;
struct    Library         *IconBase      = NULL;
struct    IntuitionBase   *IntuitionBase = NULL;

	// External libraries

struct    Library         *GTLayoutBase;
struct    ReqToolsBase    *ReqToolsBase;

	// Settings structure

struct    Settings        *Set;

	// C:Version string

static const STRPTR    __ver = "$VER: " PRG_TITLE " " PRG_VERSION " " __AMIGADATE__;

	// Libraries

static const struct { STRPTR Name; ULONG Version; APTR *Library; } Table [] =
{

	"commodities.library", 39, (APTR *) &CxBase,
	"datatypes.library",   39, (APTR *) &DataTypesBase,
	"dos.library",         39, (APTR *) &DOSBase,
	"gadtools.library",    39, (APTR *) &GadToolsBase,
	"graphics.library",    39, (APTR *) &GfxBase,
	"gtlayout.library",    27, (APTR *) &GTLayoutBase,
	"icon.library",        39, (APTR *) &IconBase,
	"intuition.library",   39, (APTR *) &IntuitionBase,
	"layers.library",      39, (APTR *) &LayersBase,

	NULL,

};

/// main ()

LONG
main (VOID)
{

	LONG    Result = RETURN_FAIL;

		// Open system libraries

	if (OpenLibs())
	{

			// Cannot be opened by OpenLibs()

		if (ReqToolsBase = (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))
		{

				// Try to open locale stuff

			LocaleOpen (PRG_TITLE ".catalog", "english", 2);

				// Setup settings structure

			if (Set = AllocStruct (Settings))
			{

					// Initialize settings

				InitSettings();

					// Evaluate ToolTypes

				EvalTTypes();

					// Evaluate Shell arguments

				EvalArgs();

					// Setup commodity

				if (SetupCx())
				{

					BOOL     Done = FALSE;
					ULONG    Signals;

					Result = RETURN_OK;

					if (Set -> Cx . PopUp)
					{

						CreateGUI();

					}

					Signals = PortMask (CxPort) | GUIMask;

					do
					{

							// Got signal from GUI

						if (Signals & GUIMask)
						{

							Done = HandleGUI();

						}

							// Got commodity signal

						if (Signals & PortMask (CxPort))
						{

							CxMsg   *Message;

							while (Message = (CxMsg *) GetMsg (CxPort))
							{

								if (HandleCx (Message))
								{

									Done = TRUE;
									break;

								}

							}

						}

							// Got DOS signal: CTRL-C

						if (Signals & SIGBREAKF_CTRL_C)
						{

							Done = TRUE;

						}

							// Wait for signals

						if ( ! (Done))
						{

							Signals = Wait (GUIMask | SIGBREAKF_CTRL_C | PortMask (CxPort));

						}

					}
					while ( ! (Done));

					RemoveGUI();

				}

					// Quit program

				QuitCx();

					// Free settings structure

				FreeStruct (Set);

			}
			else
			{

				ErrorRequest ("Quit", LocaleString (MSG_ERR_OUT_OF_MEMORY));

			}

				// Close locale stuff

			LocaleClose();

			CloseLib (ReqToolsBase);

		}
		else
		{

			FPrintf (Output(), PRG_TITLE ": Failed to open reqtools.library v%ld+ !\n", REQTOOLSVERSION);

		}

	}

	CloseLibs();

#if MWDEBUG

	MWReport (PRG_TITLE " statistics:", MWR_FULL);

#endif

	return (Result);

}

///
/// OpenLibs ()

	/*
	 *    FUNCTION    Open a required libraries.
	 *
	 *    NOTE        <Table> is defined above.
	 *
	 *    EXAMPLE     OpenLibs ();
	 *
	 */


static BOOL
OpenLibs (VOID)
{

	LONG    i;

		// Open libraries

	for (i = 0; Table [i] . Name != NULL; i++)
	{

			// Failed to open a library

		if ( ! (*Table [i] . Library = OpenLibrary (Table [i] . Name, Table [i] . Version)))
		{

			FPrintf (Output(), PRG_TITLE ": Failed to open \"%s\" V%ld+ !\n", Table [i] . Name, Table [i] . Version);
			return (FALSE);

		}

	}

	return (TRUE);

}

///
/// CloseLibs ()

	/*
	 *    FUNCTION    Close all opened libraries.
	 *
	 *    NOTE        <Table> is defined above.
	 *
	 *    EXAMPLE     CloseLibs ();
	 *
	 */


static VOID
CloseLibs (VOID)
{

	LONG    i;

		// Close libraries

	for (i = 0; Table [i] . Name != NULL; i++)
	{

		CloseLib (*Table [i] . Library);

	}

}

///

