/*							----------------------
									setup.c

							 © 1992,1993 REELSOFT
							----------------------
*/

#include	<proto/asl.h>
#include	<proto/dos.h>
#include	<proto/exec.h>
#include	<proto/gadtools.h>
#include	<proto/graphics.h>
#include	<proto/intuition.h>
#include	<proto/powerpacker.h>
#include	<proto/reqtools.h>
#include	<proto/utility.h>
#include	<proto/wb.h>

#include	<clib/locale_protos.h>
#include	<pragmas/locale_pragmas.h>

#include	<libraries/dos.h>
#include	<libraries/ppbase.h>
#include	<libraries/reqtools.h>
#include	<graphics/gfxbase.h>
#include	<intuition/classes.h>

#include	<stdlib.h>

#include	"protos.h"
#include	"macros.h"
#include	"debug.h"

#include	"locale.h"
#include	"window.h"
#include	"setup.h"

//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
//	opening the locale.library if available, and reading the translated strings
VOID	Translate (STRPTR language)
{
	if (LocaleBase = (struct LocaleBase *)OpenLibrary ("locale.library",38L)) {
		struct	Catalog	*catalog ;
		ULONG	loctags[] = {OC_Language,NULL,TAG_DONE} ;

		loctags[1] = (ULONG)language ;
		if (catalog = OpenCatalogA (NULL,"ppmc.catalog",(struct TagItem *)loctags)) {
			UBYTE	n ;

			for (n=0 ; n<=MSG_PER_PACKED ; n++)
				Translated[n] = GetCatalogStr (catalog,n,Translated[n]) ;
			CloseCatalog (catalog) ;
		}
	}
}
//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
BOOL	Setup (STRPTR pubscreen)
{
	ULONG	objtags[] = {GT_VisualInfo,NULL,TAG_DONE} ;
	STRPTR	abort = LOC(MSG_OK_ABORT) ;

	//	opening reqtools.library :
	if (NOT(mcPrfs & CLI)) {
		if (NOT(ReqToolsBase = (struct ReqToolsBase *)OpenLibrary (REQTOOLSNAME,REQTOOLSVERSION)))
			return FALSE ;
		RT_TITLE (MSG_TITLE_WARNING) ;
	}

	//	opening the powerpacker.library & utility.library :
	if (NOT(UtilityBase	= OpenLibrary ("utility.library",37L))
	 || NOT(PPBase		= (struct PPBase *)OpenLibrary (PPNAME,PPVERSION))) {
		STRPTR	msg = LOC(UtilityBase?MSG_PPACKER_VER:MSG_OS_VER) ;

		if (mcPrfs & CLI) {
			PutStr (_HEADLINE_) ;
			PutStr ("›1m") ;
			PutStr (msg) ;
			PutStr ("›0m\n") ;
		}
		else rtEZRequestA (msg,abort,NULL,NULL,(struct TagItem *)rtTags) ;
		return FALSE ;
	}

	mcPwd[0] = '\0' ;
	mcPara[EFF] = mcPara[EFF]>0 && mcPara[EFF]<=5 ? mcPara[EFF]-1 : 4 ;
	mcPara[BUF] = mcPara[BUF]>0 && mcPara[BUF]<=3 ? mcPara[BUF]-1 : 0 ;
	mcPara[DEC] = mcPara[DEC]>0 && mcPara[DEC]<=5 ? mcPara[DEC]-1 : 4 ;

	//	invoked from CLI? well, we have all we need, let's return to CLIinit()..
	if (mcPrfs & CLI) return TRUE ;

	//	otherwise, we have to open several other libraries :
	if ( NOT(IntuitionBase = (struct IntuitionBase *)OpenLibrary ("intuition.library",37L))
	  || NOT(GfxBase	   = (struct GfxBase *)OpenLibrary (GRAPHICSNAME,37L))
	  || NOT(WorkbenchBase = OpenLibrary (WORKBENCH_NAME,37L))
	  || NOT(GadToolsBase  = OpenLibrary ("gadtools.library",37L)) )
		return FALSE ;

	//	try to open the asl.library for the file requester :
	if (mcPrfs & ASL) AslBase = OpenLibrary (AslName,37L) ;

	//	initializing and Adding the custom font :
	FontInit() ;
	mcPrfs |= FONT ;

	//	locking the `pubscreen' screen (Workbench by default) :
	if ( NOT(Scr = LockPubScreen (pubscreen))
	  && NOT(Scr = LockPubScreen ("Workbench"))
	  || NOT(VInfo = GetVisualInfoA (Scr,TAG_DONE)))
	{
		rtEZRequestA (LOC(MSG_LOCK_SCREEN),abort,NULL,NULL,(struct TagItem *)rtTags) ;
		return FALSE ;
	}

	//	opening PPMC Window :
	objtags[1] = (ULONG)VInfo ;
	if ( NOT(getClass = initGet())
	  || NOT(getImage = NewObjectA (getClass,NULL,(struct TagItem *)objtags))
	  || NOT SetupWindow()) {
		rtEZRequestA (LOC(MSG_OPEN_WINDOW),abort,NULL,NULL,(struct TagItem *)rtTags) ;
		return FALSE ;
	}

	// setting pr_WindowPtr :
	mcWPtr = (mcProc = (struct Process *)FindTask(NULL))->pr_WindowPtr ;
	mcProc->pr_WindowPtr = Wnd ;

	//	creating a Message Port :
	if (NOT(MsgPort = CreateMsgPort())) {
		DEBUG_MSG (MSG_MSGPORT_DEBUG) ;
		return FALSE ;
	}

	//	adding an AppWindow :
	if (NOT(AppWin = AddAppWindowA (1,0,Wnd,MsgPort,TAG_DONE))) {
		DEBUG_MSG (MSG_APPWIN_DEBUG) ;
		DeleteMsgPort (MsgPort) ;
		MsgPort = NULL ;
	}

	//	getting the Gadgets short-cut key code :
	Key.save	= ShCutKey (MSG_SAVE_GAD) ;
	Key.pack	= ShCutKey (MSG_PACK_GAD) ;
	Key.unpack	= ShCutKey (MSG_UNPACK_GAD) ;
	Key.encrypt	= ShCutKey (MSG_ENCRYPT_GAD) ;

	Name.old[0] = Name.new[0] = '\0' ;
	aslfrTags[1] = (ULONG)Wnd ;

	return TRUE ;
}
//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
VOID	Cleanup (BOOL retcode)
{
	FreeCrMem() ;
	if (NOT(mcPrfs & CLI)) {
		if (AppWin)			RemoveAppWindow (AppWin) ;
		if (MsgPort)		DeleteMsgPort (MsgPort) ;
		if (Menus) {
							ClearMenuStrip (Wnd) ;
							FreeMenus (Menus) ;
		}

		mcProc->pr_WindowPtr = mcWPtr ;

		if (Wnd)			CloseWindow (Wnd) ;
		if (GList)			FreeGadgets (GList) ;
		if (getImage)		DisposeObject (getImage) ;
		if (getClass)		FreeClass (getClass) ;
		if (VInfo)			FreeVisualInfo (VInfo) ;
		if (Scr)			UnlockPubScreen (NULL,Scr) ;

		if (mcPrfs & FONT)	RemFont (&Font) ;

		if (GadToolsBase)	CloseLibrary (GadToolsBase) ;
		if (AslBase)		CloseLibrary (AslBase) ;
		if (WorkbenchBase)	CloseLibrary (WorkbenchBase) ;
		if (GfxBase)		CloseLibrary ((struct Library *)GfxBase) ;
		if (IntuitionBase)	CloseLibrary ((struct Library *)IntuitionBase) ;
	}
	else if (RArgs)			FreeArgs (RArgs) ;

	if (UtilityBase)		CloseLibrary (UtilityBase) ;
	if (PPBase)				CloseLibrary ((struct Library *)PPBase) ;
	if (ReqToolsBase)		CloseLibrary ((struct Library *)ReqToolsBase) ;
	if (LocaleBase)			CloseLibrary ((struct Library *)LocaleBase) ;

	exit (retcode?EXIT_SUCCESS:EXIT_FAILURE) ;
}

//	Tab size 4