/* LMain.c - main c file for LARP */

#include "defs.h"

char *RexxHostName = "LARP";
UBYTE count = 0;		/* num messages in buffer */
char *Buf;			/* Buffer */
char PubName[32] = {0};		/* the name of the pubscreen */
UBYTE status = 0;
UBYTE dirt = 0;			/* No dirt */
UWORD MAXBUF = 60;

void main(int ac, char *av[])
{
	int done=0;
	ULONG class;
	UWORD code;
	LONG mask;
	struct IntuiMessage *imsg;
	if(ac > 0)
		status = CLI;
	if(status == CLI && ac > 1)
	{
		for(done = 1; done < ac; ++done)
		{
			if((strcmp(av[done], "PUBSCREEN"))==NULL)
			{
				if(ac > done)
				{
					if(strlen(av[done + 1]) < 32)
					{
						strcpy(&PubName[0], av[done + 1]);
						LARP_pubscreen = &PubName[0];
					}
				}
			}
			if((strcmp(av[done], "QUIET"))==NULL)
				status = CLI|QUIET;
			if((strcmp(av[done], "HISTORY"))==NULL)
			{
				if(ac > done)
				{
					MAXBUF = atoi(av[done + 1]);
					if(MAXBUF > 1000)
						MAXBUF = 60; /* There's got to be some sense */
				}
			}
		}
		done = 0;
	}
	
	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;
	LONG x, y, n;
	if(strncmp(arg0, "PUTS", 4) == 0)
	{
		*pres = "succ!";
		rc = 0;
		UpdateMessages(arg0 + 5);
		return(rc);
	}
	if(strncmp(arg0, "SIZE", 4) == 0)
	{
		n = sscanf(arg0 + 5, "%d %d", &x, &y);
		SizeWindow(Win0, x - Win0->Width, y - Win0->Height );
		*pres = "succ!";
		rc = 0;
		return(rc);
	}
	if(strncmp(arg0, "PLACE", 5) == 0)
	{
		n = sscanf(arg0 + 6, "%d %d", &x, &y);
		MoveWindow(Win0, x - Win0->LeftEdge, y - Win0->TopEdge );
		*pres = "succ!";
		rc = 0;
		return(rc);
	}
	rc = 5;
	*pres = "fail!";
	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-14-offy)/8;
	if(rowchar>80)
		rowchar = 80;
	if(dirt!=0)
	{
		SetAPen(win->RPort, 0);
		RectFill(win->RPort, offx+8, offy + 5, win->Width-offx-21, win->Height-win->BorderBottom-5);
	}
	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;
	}
	dirt = 1; /* There are chars in the display */
}

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);
		dirt = 0;
		DrawMessages(Win0, Buf, count);
		break;
	}
	return(returncode);
}

void wbmain(struct WBStartup *wbstartup)
{
	struct DiskObject *dobj;	/* Really didn't had the motivation to get   */
	struct WBArg *wbarg;		/* into this - so I looked at the Libraries  */
	char **toolarray;		/* - and copied it - well modified it quite a*/
	char *s;			/* bit actually */
	status = WB;			/* because wbmain calls main and main has to */
	wbarg = wbstartup->sm_ArgList;	/* know that it has been called by wbmain    */
	dobj = GetDiskObject(wbarg->wa_Name);
	if(dobj != NULL)
	{
		toolarray = (char**)dobj->do_ToolTypes;
		s = (char *)FindToolType(toolarray, "PUBSCREEN");
		if(s != NULL)
		{
			if(strlen(s) < 32)
			{
				strcpy(&PubName[0], s);
				LARP_pubscreen = &PubName[0];
			}
		}
		s = (char *)FindToolType(toolarray, "QUIET");
		if(s != NULL)
		{
			if((strcmp(s, "TRUE")) == NULL)
				status = WB | QUIET;
		}
		s = (char *)FindToolType(toolarray, "HISTORY");
		if(s != NULL)
		{
			MAXBUF = atoi(s);
			if(MAXBUF > 1000)
				MAXBUF = 60;
		}
		FreeDiskObject(dobj);
	}	
	main(NULL, NULL);
}
