/* Compile me to get full executable. */

#include "defs.h"

#define MAXBUF 60

char *RexxHostName = "LARP";
char *MyCommand = "PUTS";
UBYTE count = 0;		/* num messages in buffer */
char *Buf;			/* what a lame way - must fix this */

void main(void)
{
	int done=0;
	ULONG class;
	UWORD code;
	LONG mask;
	struct IntuiMessage *imsg;
	if(RexxSysBase == NULL)
	{
		puts("Unable to open rexxsyslib.library !");
		exit(20);
	}
	if (OpenLibs()==0)
	{
		Buf = AllocMem(MAXBUF*80l, MEMF_PUBLIC|MEMF_CLEAR);
		if(Buf == NULL)
		{
			puts("Cannot allocate memory.");
			CloseLibs();
			exit(20);
		}
		if (OpenWindowWin0()==0)
		{
			while(done==0)
			{
				mask = Wait((1l<<RexxSigBit)|(1L << Win0->UserPort->mp_SigBit));
				if(mask & (1l<<RexxSigBit))
				{
					ProcessRexxCommands(NULL);
					DrawMessages(Win0, Buf, count);
				}
				else
				{
					imsg=GT_GetIMsg(Win0->UserPort);
					while (imsg != NULL )
					{
						class=imsg->Class;
						code=imsg->Code;
						GT_ReplyIMsg(imsg);
						done = ProcessWindowWin0(class, code, imsg);
						imsg=GT_GetIMsg(Win0->UserPort);
					}
				}
			}
			CloseWindowWin0();
		}
		else
		{
			puts("Cannot open window.");
		}
		FreeMem(Buf, MAXBUF*80l);
		CloseLibs();
	}
	else
	{
		puts("Cannot open libraries.\n");
		exit(20);
	}
}

long DoRexxCommand(msg, port, arg0, pres)
void *msg;
struct MsgPort *port;
char *arg0;
char **pres;
{
	int rc;
	if(strncmp(arg0, MyCommand, 4) == 0)
	{
		*pres = "success!";
		rc = 0;
		UpdateMessages(arg0 + 5);
	}
	else
	{
		*pres = "failure!";
		rc = 5;
	}
	return(rc);
}

void DrawMessages(struct Window *win, char *buff, UBYTE count)
{
	short i, howmany, start;
	UWORD offx = win->BorderLeft;
	UWORD offy = win->BorderTop;
	int rowchar = (win->Width-32-offx)/8;
	int linechar = (win->Height-16-offy)/8;
	if(rowchar>80)
		rowchar = 80;
	SetAPen(win->RPort, 0);
	RectFill(win->RPort, offx+8, offy + 5, win->Width-offx-21, win->Height-7);
	SetAPen(win->RPort, 1);
	start = count - linechar;
	if(start<0)
		start = 0;
	for(i = 0; i<count; i++)
	{
		howmany = strlen(buff + ((i+start) * 80));
		if(howmany>rowchar)
			howmany = rowchar;
		Move(win->RPort, offx + 8, offy+ 11 + (i*8));
		Text(win->RPort, buff + ((i + start) * 80), howmany);
		if(i == linechar)
			break;
	}
}

void UpdateMessages(char *line)
{
	int howmany = strlen(line);
	if(howmany>78)
		howmany = 78;
	count++;
	if(count>MAXBUF)
	{
		count = MAXBUF;
		memmove(Buf, Buf + 80, (MAXBUF-1)*80);
		memcpy(Buf + ((MAXBUF-1)*80), line, howmany+1);
	}
	else
	{
		memcpy(Buf+((count-1)*80), line, howmany+1);
	}
}

int ProcessWindowWin0( LONG Class, UWORD Code, APTR IAddress )
{
	int returncode = 0;
	switch ( Class )
	{
	case IDCMP_CLOSEWINDOW:
		returncode = 1;
		break;
	case IDCMP_NEWSIZE:
		RendWindowWin0(Win0);
		DrawMessages(Win0, Buf, count);
		break;
	case IDCMP_REFRESHWINDOW :
		GT_BeginRefresh( Win0);
		RendWindowWin0( Win0);
		DrawMessages(Win0, Buf, count);
		GT_EndRefresh( Win0, TRUE);
		break;
	}
	return(returncode);
}

void wbmain(void)
{
	main();
}
