/**************************
**
**	$Id:	main.c
**
**	Main Program File: Amiga Shopper Finder Application.
**	By Toby Simpson
**
**	NOTE FOR DICE USERS:
**		Earlier DICE releases (And some freeware versions) do not support
**		the __aligned keyword. The versions with "Complete Amiga C", and on
**		the March cover disk do.
**
*/

#include	"finder.h"
#include	"finder_rev.h"

/*
**	Library bases:
*/
struct Library *GadToolsBase 		= NULL;
struct Library *IntuitionBase 	= NULL;
struct Library *RexxSysBase 		= NULL;

/*
**	Global variables:
*/
long	files_matched	= 0;					/* Total files found */
char	*VERSION_STR	= VERSTAG;

struct Gadget *first_gadget, *context_gadget, *previous_gadget; 
struct Gadget *sg_Search, *sg_Drawer;
struct Gadget *gadget_list[TOTAL_GADGETS];
struct Window *finder_window = NULL;

char	*button_text[] =
	{
	"_Quit", "_Find", "_Cancel", NULL
	};

struct List find_list;

struct MsgPort	*arexx_port = NULL;

char 	*arexx_commands[] =
	{
	"QUIT", "FIND", "SETSEARCH", "SETDRAWER", NULL
	};

/**************************
**
**	void	main(void)
**
**	Main program entry function.
*/

void	main(void)
{
	/*
	**	Title us and parse arguments:
	*/
	printf("%s\n", VSTRING);

	/*
	**	Open any libraries we might want:
	*/
	if (!(IntuitionBase = OpenLibrary("intuition.library", 37L)))
		{
		printf("Can't open intuition library V37\n");
		cleanexit(RETURN_FAIL);
		}
	if (!(GadToolsBase = OpenLibrary("gadtools.library", 37L)))
		{
		printf("Can't open gadtools.library V37\n");
		cleanexit(RETURN_FAIL);
		}
	if (!(RexxSysBase = OpenLibrary("rexxsyslib.library", 0L)))
		{
		printf("Can't open rexxsyslib.library\n");
		cleanexit(RETURN_FAIL);
		}

	/*
	**	Open our ARexx port:
	*/
	Forbid();
	if (FindPort(AREXX_PORT_NAME) == NULL)
		arexx_port = CreatePort(AREXX_PORT_NAME, 0);
	Permit();
	if (!arexx_port)
		{
		ShowErrorRequester(	"Cannot create ARexx port. An application\n"
												"may already be running with a port name\n"
												"of 'finder'", NULL );
		cleanexit(RETURN_FAIL);
		}

	/*
	**	Open our GUI (Window, buttons, etc)
	*/
	if (!(OpenGUI("", "")))
		{
		printf("Unable to open window\n");
		cleanexit(RETURN_FAIL);
		}

	/*
	**	Call window Event Handler:
	*/
	EventLoop("", "");

	cleanexit(0);				/* Exit with no error code */
}

/**************************
**
**	void	cleanexit(int returnvalue)
**
**	Exits the program, closing any allocated resources.
*/

void	cleanexit(int returnvalue)
{
	struct	RexxMsg	*msg;

	/*
	**	Shut down any GUI components we opened:
	*/
	CloseGUI();
	
	/*
	**	Close libraries:
	*/
	if (IntuitionBase)	CloseLibrary(IntuitionBase);
	if (GadToolsBase)		CloseLibrary(GadToolsBase);
	if (RexxSysBase)		CloseLibrary(RexxSysBase);

	/*
	**	Remove ARexx port, if open:
	*/
	if (arexx_port)	
		{
		/*
		**	We must remove all pending ARexx messages first, this
		**	must be done with no risk of more arriving, hence the Forbid and
		**	Permit:
		*/
		Forbid();
		while ((msg = (struct RexxMsg *) GetMsg(arexx_port)) != NULL)
			{
			msg->rm_Result1 = RC_FATAL;
			msg->rm_Result2 = NULL;
			ReplyMsg((struct Message *) msg);
			}
			
		DeletePort(arexx_port);
		Permit();
		}

	/*
	**	Exit program with correct error code:
	*/
	exit(returnvalue);
}
