#include "exec/types.h"
#include "stdio.h"
#include "libraries/dos.h"
#include "intuition/intuition.h"

struct IntuitionBase *IntuitionBase;
struct IntuiMessage *message;
 
/* tell the compiler that we're going to use FileHandles */
extern struct FileHandle *Open();

/* declare a pointer to a FileHandle for both the printer and file */
struct FileHandle *fh;  /* the file handle    */
struct FileHandle *pfh; /* the printer handle */

struct Window *Window;
struct NewWindow OurWindow = {
 140,95,         /* window XY origin relative to TopLeft  */
 382,10,         /* window width and height               */
 0,1,         /* detail and block pens                 */
 CLOSEWINDOW+ACTIVEWINDOW,    /* IDCMP flags        */
 WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE,/* other window flags */
 NULL,    /* first gadget in gadget list           */
 NULL,         /* custom CHECKMARK imagery              */
 (UBYTE *)"Close Window To Stop Printing",   /* window title */
 NULL,    /* custom screen pointer                 */
 NULL,         /* custom bitmap                         */
 382,10,         /* minimum width and height              */
 640,200, /* maximum width and height              */
 WBENCHSCREEN /* destination screen type               */
};

main()
{
 ULONG class;

 /************************************************************************
  Let's take advantage of the fact that Lattice always opens a small window
  on the Workbench whenever a program is started from the Workbench. This
  window serves at the "stdin" and "stdout" window. For Manx users, open the
  CON: device the same way you would for a file. As an alternative, you
  may wish to open a console window using the tool field in the icon. For
  more information on these techniques, please refer to previous Jumpdisk
  "C" columns.
 ************************************************************************/
 char input_file[80], output_buffer[160], dummy_input[80];
 int i, input_return, quit_flag; 

 /* first thing to check is whether or not the printer is available */
 pfh = Open("PRT:",MODE_OLDFILE);
 if (pfh == NULL) {
    printf("\nThe printer is not available.\n");
    printf("press RETURN.\n");
    gets(dummy_input);
    exit(1); 
 }

 /* this loop will ensure that the user enters a valid filename */
 quit_flag=FALSE;
 do{
    printf("enter a filename to print.\n");
    printf("or press RETURN to exit:\n");
    gets(input_file);

    /* if nothing was entered, just leave without doing anything */
    if(strlen(input_file)==0) {
      Close(pfh);
      exit(0);
    }

    /* if something was entered, echo the users choice */
    printf("Now printing ");
    for(i=0;i<(strlen(input_file)+1);i++) putchar(input_file[i]);
    printf("\n");

    /* now that we have a filename, we must check its validity
       by actually attempting to open the file.               */
    fh = Open(input_file, MODE_OLDFILE); 

    /* if a NULL was returned, then the file doesn't exist    */
    if (fh ==NULL) {
       printf("**** ERROR **** ERROR ****\n");
       printf("file cannot be opened.\n\n");
    }
    else quit_flag = TRUE;

} while (quit_flag == FALSE);

/**** If the program has gotten here, both input and output streams are
 **** ready. Now handle the Intuition stuff.                         ****/

/* The usual opens for intuition and other good stuff. Notice that the
   check for Intuition opening isn't really necessary. If it doesn't
   open, then the Amiga is near death, and we shouldn't tax it further */
 IntuitionBase = (struct IntuitionBase *)
                 OpenLibrary("intuition.library", 0);
 if (IntuitionBase == NULL) exit(NULL);

/* Notice in this case, we should check the Intuition open since it is
   conceivable that the user doesn't have enough memory to open the
   window. In that case, we should exit as gracefully as we can       */
 if((Window = (struct Window *) OpenWindow(&OurWindow))==NULL) { 
   printf("Couldn't open the window.\n");
   CloseLibrary(IntuitionBase);
   Close(pfh);
   Close(fh);
   exit(99);
 }
 /************************************************************************
   notice that here we're not going to use Wait(). That's because we're
    not going to wait until the user does something. Instead, we'll keep 
    printing until we get a stop message from the user, at which time we'll
    close the printer, the Intuition window, and the file we've opened.                       
 ************************************************************************/
 quit_flag=FALSE;
 do {
    while((message=(struct IntuiMessage *) 
       GetMsg(Window->UserPort))!=NULL) {
  class = message->Class;
  ReplyMsg(message);
  if (class == CLOSEWINDOW) quit_flag = TRUE;
    }
    /* Read a line, and then print it using the functions described in 
       this month's tutorial.                                        */
    input_return = Read(fh, output_buffer, sizeof(output_buffer));

    /* if the return is greater than zero, then we're fine, otherwise,
       we'll assume that we've hit an end of file condition. Notice that
       this isn't necessary true, there are other reasons why we would
       have a bad Read(), however, in any case, we have to close 
       anyway, so treat it as an EOF.                               */
    if(input_return>0)
      Write(pfh, output_buffer, input_return);
     else
      quit_flag = TRUE;

 } while (quit_flag == FALSE);

 /* it will drop through here when the user selects the close gadget */
 CloseWindow(Window);
 CloseLibrary(IntuitionBase);
 Close(pfh);
 Close(fh);
 return(0);

} /* end of the main program */
