/*							----------------------
									cli.c

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

#include	<proto/dos.h>
#include	<proto/utility.h>

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>

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

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

extern	BPTR	Outfh ;
#define	WriteChar(c)	FPutC(Outfh,(c))
#define	NOT	!

struct	RDArgs	*RArgs = NULL ;
LONG	mcRArgs[TEMPLATE_ITEMS] ;

//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
VOID	__regargs _CXBRK (VOID)
{
	WriteChar ('\n') ;
	PutStr (_HEADLINE_) ;
	PutStr ("\a›1m") ;
	PutStr (LOC(MSG_CLI_BREAK)) ;
	PutStr ("...›0m\n") ;
	Cleanup (TRUE) ;
}
//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
BOOL	CLIinit (VOID)
{
	mcPrfs = CLI ;
	RArgs  = ReadArgs (TEMPLATE,mcRArgs,NULL) ;

	if (mcRArgs[OPT_VER]) {	//	show the PPMC version and exit..
		PutStr (&_VERSION_[6]) ;
		WriteChar ('\n') ;
		return FALSE ;
	}
	if (mcRArgs[OPT_HELP]) {	//	show the help message and exit..
		const UBYTE	*s = "EFFBUFDEC" ;
		UBYTE	p, g ;

		Translate (NULL) ;
		for (p = 0 ; p < 3 ; p++) {
			Printf ("›1m%.3s (%s):›0m\n",&s[3*p],LOC(MSG_PARAMETERS_EFFICIENCY+p)) ;
			for (g = 0 ; g < (p==1?3:5) ; g++)
				Printf ("\t%1ld = %s\n",g+1,LOC(MSG_EFFICIENCY0_GAD+g)) ;
		}
		return FALSE ;
	}

	if (NOT RArgs) {
		PrintFault (IoErr(),"›32mPPMC›31m") ;
		return FALSE ;
	}

	Name.max  = 0 ;
	mcPara[EFF]	= (UBYTE)mcRArgs[OPT_EFF] ;
	mcPara[BUF]	= (UBYTE)mcRArgs[OPT_BUF] ;
	mcPara[DEC]	= (UBYTE)mcRArgs[OPT_DEC] ;
	mcPass		= (BOOL)mcRArgs[OPT_ENCRYPT] ;
	mcPrfs	   |= (BOOL)mcRArgs[OPT_QUIET]? 0: VERB ;

	Translate(NULL) ;
	return (Setup(NULL)) ;
}
//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
VOID	CLIonly	(VOID)
{
	UBYTE	**argptr = (UBYTE **)mcRArgs[OPT_FROM] ;

	PutStr ("›7;42m ") ;
	PutStr (_TITLE_) ;
	PutStr (" ›0;33m\n ") ;
	PutStr (_COPYRIGHT_) ;
	PutStr ("›31m\n") ;

	while (*argptr++) Name.max++ ;
	if (Name.max>1) mcRArgs[OPT_TO] = '\0' ;
	argptr = (UBYTE **)mcRArgs[OPT_FROM] ;

	for (Name.num = 1 ; *argptr ; Name.num++) {
		strcpy (Name.old,*argptr++) ;
		switch (AnalyseFile()) {
			case 1 :	//	unable to open the file!..
						PrintFault (IoErr(),"›32mPPMC›31m") ;
			//			break ;
			case 2 :	//	file is empty!..
			//			PutStr (_HEADLINE_) ;
			//			Printf ("%s %s\n",Name.fpart,LOC(MSG_EMPTY_FILE)) ;
			//			break ;
			case 3 :	//	user doesn't want to pack an executable file..
						break ;
			case 4 :	//	file is powerpacked and encrypted..
						mcStat |= CRYPT ;
			case 5 :	//	file is powerpacked..
						if (Decrunch()) Save(FALSE) ;
						break ;
			case 0 :	//	file seems OK..
						if (Crunch()) Save(FALSE) ;
						else mcStat ^= mcStat & CRYPT ;
						break ;
		}
		WriteChar('\n') ;
	}
}
//""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
BOOL	CLIRequest (VOID)
{
	STRPTR	yes = LOC(MSG_CLI_YES_NO) ;
	UBYTE	ret ;

	PutStr (yes) ;
	Flush (Outfh) ;
	ret = getch() ;
	PutStr ("\r›K") ;

	if (ret == 0x1b) _CXBRK() ;
	else if (ret == ToLower(yes[2]) || ret == ToUpper(yes[2])) return FALSE ;

	return TRUE ;
}

//	Tab size 4