/*****
	getfile(Window,Hailing_string,file_name,directory_name);

Where:
	Window is a pointer to your window
	Hailing_string is a prompt displayed in the requester
	file_name is a text array which will be altered by getfile,
		it returns the file name.
	directory_name is a text array altered by getfile, it
		is the directory.

    The return value is either a pointer to your buffer, file_name,
	or NULL if the user selected CANCEL.

*****/

#include <exec/types.h>
#include <exec/devices.h>
#include <intuition/intuition.h>
#include <string.h>

extern convert();
struct IntuitionBase *IntuitionBase;	/* Handles for inutition and	*/
struct GfxBase	*GfxBase;		/* graphics libraries		*/

#define ONE 1

TEXT file_name[34];
TEXT dir_name[67];
TEXT portrait[67];
TEXT landscape[67];
TEXT slash[10] = "/";
int  SLASH = '\057'; /* decimal 42, forward slash (UNIX, AMIGA) */

char *LAND  = ".land";
char *temp;

struct Window	*p_Window;		/* Handle for window we open  */

/* Struct to open Window   */
struct NewWindow NewWindow = {
   10,10,400,150, 2,1,		/* Left,Top, Width,Height, Pens  */
   MOUSEBUTTONS | MOUSEMOVE | MENUPICK | CLOSEWINDOW, /* IDCMP flags */
   SMART_REFRESH | ACTIVATE | WINDOWSIZING | WINDOWCLOSE |
   WINDOWDRAG | WINDOWDEPTH | REPORTMOUSE,	/* Window flags	*/
   NULL, NULL,			/* First Gadget, Checkmark image */
   (UBYTE *)"Portrait-to-Landscape", /* Window Title	*/
   NULL, NULL,			/* Screen and BitMap		*/
   100,60, 32767,32767,		/* Window Sizing limits		*/
   WBENCHSCREEN};		/* WBENCH or CUSTOMSCREEN	*/

/* Menu description  */
struct IntuiText menu_text = { 2,1,JAM2, 20,1, NULL, (UBYTE *)"Start",NULL };

struct MenuItem menu_item = {
   NULL, 0,10,100,10,		/* Next item, Left, Top, Width, Height */
   HIGHCOMP | ITEMENABLED | ITEMTEXT | COMMSEQ,
   NULL,(APTR)&menu_text,	/* MutualExclude, ItemFill		*/
   NULL, 'S', NULL };		/* SelectFill, Command , SubItem	*/

struct Menu simple_menu = {
   NULL, 0,0,60,10, MENUENABLED, "Menu", &menu_item };

struct IntuiText SeventhText =
	{
	1,0,				/* FrontPen, BackPen */
	JAM2,				/* DrawMode	     */
	15,90,				/* LeftEdge, TopEdge */
	NULL,				/* Font (std ROM font) */
	(UBYTE *)"Tom Lynch (818)709-2988",
	NULL				/* NextText	     */
};

struct IntuiText SixthText =
	{
	1,0,				/* FrontPen, BackPen */
	JAM2,				/* DrawMode	     */
	15,75,				/* LeftEdge, TopEdge */
	NULL,				/* Font (std ROM font) */
	(UBYTE *)"         100 filenames or system will crash.",
	&SeventhText				/* NextText	     */
};

struct IntuiText FifthText =
	{
	1,0,				/* FrontPen, BackPen */
	JAM2,				/* DrawMode	     */
	15,65,				/* LeftEdge, TopEdge */
	NULL,				/* Font (std ROM font) */
	(UBYTE *)"WARNING: Font directory should have less than",
	&SixthText				/* NextText	     */
};

struct IntuiText FourthText =
	{
	1,0,				/* FrontPen, BackPen */
	JAM2,				/* DrawMode	     */
	15,20,				/* LeftEdge, TopEdge */
	NULL,				/* Font (std ROM font) */
	(UBYTE *)"    This program will convert soft fonts",
	&FifthText				/* NextText	     */
};

struct IntuiText ThirdText =
	{
	1,0,				/* FrontPen, BackPen */
	JAM2,				/* DrawMode	     */
	15,30,				/* LeftEdge, TopEdge */
	NULL,				/* Font (std ROM font) */
	(UBYTE *)"for the HP LaserJet compatibles from Portrait",
	&FourthText				/* NextText	     */
};

struct IntuiText SecondText =
	{
	1,0,				/* FrontPen, BackPen */
	JAM2,				/* DrawMode	     */
	15,40,				/* LeftEdge, TopEdge */
	NULL,				/* Font (std ROM font) */
	(UBYTE *)"to Landscape. Repeat for each font you wish ",
	&ThirdText				/* NextText	     */
};

struct IntuiText FirstText =
	{
	1,0,				/* FrontPen, BackPen */
	JAM2,				/* DrawMode	     */
	15,50,				/* LeftEdge, TopEdge */
	NULL,				/* Font (std ROM font) */
	(UBYTE *)"to convert. Rename each font as desired.",
	&SecondText				/* NextText	     */
};

/****************************************************************/
/* init_window()						*/
/*	Opens Window, sets up menu strip and I/O Port		*/
/****************************************************************/
init_window()
{

struct RastPort *p_WindowRastPort;

if ( ! (IntuitionBase = (struct IntuitionBase *)
	OpenLibrary("intuition.library",29)) ||
     ! (GfxBase =(struct GfxBase *)OpenLibrary("graphics.library",29)) )
         return(2);

/* Open the window, sets handle p_Window   */
if ( ! (p_Window = (struct Window *)OpenWindow( &NewWindow )) ) {
    CloseLibrary(GfxBase);    CloseLibrary(IntuitionBase);
    return(3);
    }

    SetMenuStrip( p_Window, &simple_menu );

    p_WindowRastPort = p_Window->RPort; /* set up text window */

    PrintIText(p_WindowRastPort, &FirstText, 10L, 20L); /* write it! */

    return NULL;
}

/***************************************************************/
/* clear_window                                                */
/*     Clears window and screen set up by init_window          */
/***************************************************************/
clear_window()
{
   ClearMenuStrip(p_Window);
   CloseWindow(p_Window);
	return NULL;
}
delay()		/* delay for 2 seconds to read error codes */
	{
	long t;
	long tptr;
	
	t = time(&tptr);
	while((t + 2) > time(&tptr));
	return NULL;
}

/*** Main:  Open window, process inputs , close window ***/
void main(argc, argv)
int argc;
char *argv[];
{
			
struct IntuiMessage *message;	/* Gets input event	*/
int	class,code;		/* Event information	*/
BOOL	quit_flag;		/* Application flags	*/
extern int n;
int	error;

if (argc > 1)
	{
	if (argc == 3)
		{
		if (error = convert(argv[1],argv[2]) != 0) { /*  process the request */
			printf("Failure to process font through CLI\n");
			printf("\nError message: %d\n ",error); 
			switch(error){
				case '4':
					printf("Failure to open portrait font file\n");
					break;
				case '5':
					printf("Landscape file exists; rename.\n");
					break;
				case '6':
					printf("Not font file?? First char should be 'escape'\n");
					break;
				case '7':
					printf("Char size exceeds 32768; cannot process font, stopped\n");
					break;
				case '8':
					printf("escape char not found when expected, stopped\n");
					break;
				case '9':
					printf("The font descriptor is larger than expected, stopped\n");
					break;
				default:
					break;}
			exit(0); 
			}
		printf("\nFont converted: %s --> %s\n",argv[1],argv[2]);
		exit(0);
	}
	else {
		printf("\nArgument ERROR. Correct form is:");
		printf("\n'path'PORTOLAN  PORTRAIT LANDSCAPE\n\n");
		exit(0);
	}
}

if(error = init_window() != 0) {
	clear_window();
	exit(FALSE);                 /* Open the window	*/
	}
for ( quit_flag = FALSE; ! quit_flag ; ) {

    while (! (message=(struct IntuiMessage *)GetMsg(p_Window->UserPort)) )
	WaitPort( p_Window->UserPort );  /* Wait for input event          */

    class = message->Class;    code = message->Code;   /* Get event info */
    ReplyMsg( message );    /* Tell intuition we're through with event   */

    switch ( class ) {           /*    Process the input event           */
	case CLOSEWINDOW:   quit_flag = TRUE;
	    break;

	/* Heres what you're looking for...	*/
	case MENUPICK:
	   if ( (MENUNUM( code ) == 0) && (ITEMNUM( code ) == 0) ) {
		/* Call up the filename requester for portrait font */

		if ( get_fname(p_Window, "Portrait Path/File",file_name,dir_name) ) {
		    temp = strrchr(dir_name,SLASH);
		    if (strlen(temp) != ONE) 
		    	strcat(dir_name, slash);  /* add '/' if missing */
		    strcpy(portrait,dir_name);	/* form path name */
		    strcat(portrait,file_name); /* complete portrait path */
		    strcat(file_name,LAND);  /* add default extension */ 
		}
		else {
		    printf("User aborted request\n");
		    delay();
		    clear_window();
		    exit(0); }
		    
		/* Call up the filename requester for landscape font */

		if ( get_fname(p_Window, "Rename for LANDscape:",file_name,dir_name) )  {
		    temp = strrchr(dir_name,SLASH);
		    if (strlen(temp) != ONE) 
		    	strcat(dir_name, slash);  /* add '/' if missing */
		    strcpy(landscape,dir_name);	/* form path name */
		    strcat(landscape,file_name);
		}  
		else {   
		    printf("User aborted file requester\n");
		    delay();
		    clear_window();
		    exit(0); }

		if (error = convert(portrait,landscape) != 0) { /*  process the request */
			printf("Failure to process font through WB\n");
			printf("\nError message: %d\n ",error); 
			switch(error){
				case '4':
					printf("Failure to open portrait font file\n");
					break;
				case '5':
					printf("Landscape file exists; rename.\n");
					break;
				case '6':
					printf("Not font file?? First char should be 'escape'\n");
					break;

				case '7':
					printf("Char size exceeds 32768; cannot process font\n");
					break;
				case '8':
					printf("escape char not found when expected, stopped\n");
					break;
				case '9':
					printf("The font descriptor is larger than expected, stopped\n");
					break;
				default:
					break;
			}
			delay();
			clear_window();
			exit(0); 
			}

		strcpy(file_name,"");
		break;

	} /* if MENUM */
	
	} /* SWITCH */ 
	
		
   } /* for loop looking for quit.flag */
   
delay();   
clear_window();               /* Return all resources and exit */

}
