/*
							-----------------------
								  PPMC_Setup.c
								© 1992 REELSOFT
							-----------------------
*/

#include	"PPMC_Setup.h"

VOID	Setup (VOID)
{
//	Opening reqtools.library...
	ReqToolsBase = (struct ReqToolsBase *)OpenLibrary ( REQTOOLSNAME, REQTOOLSVERSION ) ;
	if ( ReqToolsBase == NULL )
		exit ( RETURN_FAIL ) ;

//	Opening powerpacker.library...
	PPBase = (struct PPBase *)OpenLibrary ( PPNAME, PPVERSION ) ;
	if ( PPBase == NULL ) {
		DisplayWarning ( "You need powerpacker.library V35+ !" ) ;
		Cleanup ( FROM_REQTOOLS, RETURN_FAIL ) ;
	}

//	Opening intuition.library...
	IntuitionBase = OpenLibrary ( "intuition.library", 37L ) ;
	if ( IntuitionBase == NULL ) {
		DisplayWarning ( "You need KickStart V37+ !" ) ;
		Cleanup ( FROM_PPACKER, RETURN_FAIL ) ;
	}

//	Opening graphics.library...
	GfxBase = OpenLibrary ( "graphics.library", 37L ) ;
	if ( GfxBase == NULL )
		Cleanup ( FROM_INTUITION, RETURN_FAIL ) ;

//	Opening gadtools.library...
	GadToolsBase = OpenLibrary ( "gadtools.library", 37L ) ;
	if ( GadToolsBase == NULL )
		Cleanup ( FROM_GFX, RETURN_FAIL ) ;

//	Opening utility.library...
	UtilityBase = OpenLibrary ( "utility.library", 37L ) ;
	if ( UtilityBase == NULL )
		Cleanup ( FROM_GADTOOLS, RETURN_FAIL ) ;

//	Opening workbench.library...
	WorkbenchBase = OpenLibrary ( "workbench.library", 37L ) ;
	if ( WorkbenchBase == NULL )
		Cleanup ( FROM_UTILITY, RETURN_FAIL ) ;

//	Opening commodities.library...
#ifdef	PPMC_CX
	CxBase = OpenLibrary ( "commodities.library", 37L ) ;
	if ( CxBase == NULL )
		Cleanup ( FROM_WORKBENCH, RETURN_FAIL ) ;
#endif

//	Initializing the custom font...
	FontInit_PPMC () ;
	AddFont ( &PPMCFont ) ;

//	Locking the Workbench screen...
	if ( SetupScreen () ) {
		DisplayWarning ( "Unable to Lock\nWorkbench Screen !" ) ;
		Cleanup ( FROM_SCREEN, RETURN_WARN ) ;
	}

//	Opening PPMC Window...
	if ( OpenPPMCWindow () ) {
		DisplayWarning ( "Unable to open\nthe PPMC Window !" ) ;
		Cleanup ( FROM_WINDOW, RETURN_WARN ) ;
	}

//	Creating a Message Port...
	msgport = CreateMsgPort () ;
	if ( msgport == NULL ) {
		DisplayWarning ( "Unable to Create Message Port !" ) ;
		Cleanup ( FROM_WINDOW, RETURN_WARN ) ;
	}

//	Adding an AppWindow...
	appwin = AddAppWindow ( 1,0, PPMCWnd, msgport, NULL ) ;
	if ( appwin == NULL ) {
		DeleteMsgPort( msgport );
		msgport = NULL;
	}

//	Creating a Commodities Broker Port...
#ifdef	PPMC_CX
	BrokerMP = CreateMsgPort () ;
	if ( BrokerMP == NULL ) {
		DisplayWarning ( "Unable to Create\na Broker Port" ) ;
		Cleanup ( FROM_APPWINDOW, RETURN_FAIL ) ;
	}
	newbroker.nb_Port = BrokerMP ;

//	Adding a Broker to the Commodities master list...
	broker = CxBroker ( &newbroker, NULL ) ;
	if ( broker == NULL ) {
		DisplayWarning ( "Unable to Create a Broker" ) ;
		Cleanup ( FROM_CXPORT, RETURN_FAIL ) ;
	}
	ActivateCxObj ( broker, 1L ) ;
#endif
}

//	»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
VOID	Cleanup ( WORD from, int return_code )
{
#ifdef	PPMC_CX
	register	struct	Message	*msg ;
#endif

	switch (from) {
		case FROM_CLI		:
#ifdef	PPMC_CBACK
								if (_Backstdout) {
									Write (_Backstdout, PPMC_CLI, strlen (PPMC_CLI)) ;
#ifndef	_DCC
									Close (_Backstdout) ;
#endif
								}
#endif
		case FROM_WB		:
#ifdef	PPMC_CX
		case FROM_BROKER	:	DeleteCxObjAll (broker) ;
		case FROM_CXPORT	:	while (msg = GetMsg (BrokerMP))
									ReplyMsg (msg) ;
								DeletePort (BrokerMP) ;
#endif
		case FROM_APPWINDOW	:	if (appwin)
									RemoveAppWindow (appwin) ;
		case FROM_MSGPORT	:	if (msgport)
									DeleteMsgPort (msgport) ;
		case FROM_WINDOW	:	ClosePPMCWindow () ;
		case FROM_SCREEN	:	CloseDownScreen () ;
		case FROM_FONT		:	RemFont ( &PPMCFont ) ;
#ifdef	PPMC_CX
		case FROM_COMMODITY	:	CloseLibrary (CxBase) ;
#endif
		case FROM_WORKBENCH	:	CloseLibrary (WorkbenchBase) ;
		case FROM_UTILITY	:	CloseLibrary (UtilityBase) ;
		case FROM_GADTOOLS	:	CloseLibrary (GadToolsBase) ;
		case FROM_GFX		:	CloseLibrary (GfxBase) ;
		case FROM_INTUITION	:	CloseLibrary (IntuitionBase) ;
		case FROM_PPACKER	:	CloseLibrary ((struct Library *)PPBase) ;
		case FROM_REQTOOLS	:	CloseLibrary ((struct Library *)ReqToolsBase) ;
								break ;
	}
	exit ( return_code ) ;
}

//	...