#define bufsize 1024

#include <intuition/intuition.h>
#include <exec/memory.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

#include <stdio.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/icon_protos.h>
#include <clib/wb_protos.h>
#include <clib/dos_protos.h>
#include <fcntl.h>


struct IntuitionBase *IntuitionBase;
struct WorkbenchBase *WorkbenchBase;
struct IconBase *IconBase;
struct MsgPort *msgport;
struct AppIcon *ai;
struct DiskObject *dobj;
struct AppMessage *amsg;
int prth; /* printer's file handle */
int imagememsize = 0;

void CleanExit (int returnval);
void PrintIcon (void);


void CleanExit (returnval)
int returnval;
{
    if (ai)
        RemoveAppIcon(ai);
    if (dobj)
        FreeDiskObject(dobj);
    if (msgport) {
        while(amsg = (struct AppMessage *)GetMsg(msgport))
        ReplyMsg((struct Message *)amsg);
        DeleteMsgPort(msgport);
    }
    if  (IconBase) 
        CloseLibrary(IconBase);
    if (WorkbenchBase)
        CloseLibrary(WorkbenchBase);
    if (IntuitionBase)
        CloseLibrary(IntuitionBase);
    if (prth)
        close(prth);
    exit (returnval);
}
    

void main(void)
{
    if (!(IntuitionBase = OpenLibrary("intuition.library", 36)))
        CleanExit(10);
    if (!(WorkbenchBase = OpenLibrary("workbench.library", 36)))
        CleanExit(10);
    if (!(IconBase = OpenLibrary("icon.library", 36)))
        CleanExit(10);
    if (!(msgport = CreateMsgPort()))
        CleanExit(10);
    if (!(dobj = GetDiskObject("printicon")))
        CleanExit(10);
    PrintIcon();
}

void PrintIcon()
{
    struct WBArg   *argptr;
    int fh, length_read;
    char buffer[bufsize];
    int flag; /* your generic run-of-the-mill flag */

    BOOL            done = FALSE;
    int             i;


    if (!(ai = AddAppIcon(1, 0, "PrintIt", msgport, NULL, dobj, NULL)))
        CleanExit(10);
    do {
        Wait(1 << msgport->mp_SigBit);
        while (amsg = (struct AppMessage *) GetMsg(msgport)) {
            if (amsg->am_NumArgs == 0) {
                done = TRUE;
                break;
            }
            argptr = amsg->am_ArgList;
            for (i = 0; i < amsg->am_NumArgs; i++) {
                if (*(argptr->wa_Name)) {
                    CurrentDir(argptr->wa_Lock);
                    if (!(prth = open("PRT:",O_WRONLY,0))) 
                        CleanExit(10); 
                    fh = open(argptr->wa_Name,O_RDONLY,0);
                    if (fh) {
                        length_read = bufsize;
                        flag = 1;
                        while (length_read == bufsize && flag != -1) {
                            length_read = read(fh, &buffer, bufsize);
                            if (length_read != -1)
                                flag = write(prth, &buffer, length_read);
                        }
                        if (flag != -1 && length_read != -1)
                            write (prth, "\f", 1); /* send form feed */
                        close(fh);
                        close(prth);
                    }
                }
                argptr++;
            }
            ReplyMsg((struct Message *) amsg);
        }
    } while (!done);
    RemoveAppIcon(ai);
}