#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <stdio.h>
#include <time.h>
#include <fcntl.h>

#define isblank(c) ( ((c)!=32) && ((c)!=9) )

#define LEFT 20
#define TOP 78
#define RIGHT 347
#define BOTTOM 108
#define HEIGHT (BOTTOM-TOP)

/****************************/
/* Var Definition	    */
/****************************/

char Editor[40] = "C:Ed";
char More[40] = "Utilities:More";
char Dir[40] = "SYS:";

char Date[9];
char LoadDate[9];
char Password[30];

struct tm *date;
struct FileHandle *output = NULL;

#include "NoteEdit.h"

/****************************/
/* Message		    */
/****************************/

void Message(char *text)
    {
	struct RastPort *rp = window->RPort;
	SetAPen(rp, 0);
	RectFill(rp, LEFT+4, TOP+2, RIGHT-4, BOTTOM-2);

	SetAPen(rp, 1);
	Move(rp, LEFT+8, TOP+HEIGHT/2+2);
	Text(rp, text, strlen(text));
    }

/****************************/
/* ShowLoadDate 	    */
/****************************/

void ShowLoadDate()
    {
	struct RastPort *rp = window->RPort;

	sprintf(LoadDate, "%02d-%02d-%02d", date->tm_mday, date->tm_mon+1, date->tm_year);

	SetAPen(rp, 1);
	Move(rp, 154, 60);
	Text(rp, LoadDate, 8);
    }

/****************************/
/* random		    */
/****************************/

LONG q0 = 31415821 / 10000;
LONG q1 = 31415821 % 10000;

LONG random(LONG randombit)
    {
	LONG p0, p1, r;

	p0 = randombit / 10000;
	p1 = randombit % 10000;

	r = ( ( (p1*q0+p0*q1) % 10000 ) * 10000 + p1*q1 ) % 100000000;

	return((r+1) % 255);
    }

/****************************/
/* LoadConfiguration	    */
/****************************/

void LoadConfiguration()
    {
	FILE *conf;
	SHORT i;
	char Buffer[256];
	char Token[256];
	char Arg[256];

	if ( conf = fopen("s:Note.config", "r") )
	    {
		while ( !feof(conf) )
		    {
			Arg[0] = Token[0] = 0;

			fgets(Buffer, 255, conf);
			sscanf(Buffer, "%s : %s", Token, Arg);

			if ( !strcmp(Token, "EDITOR") )
			    strcpy(Editor, Arg);

			if ( !strcmp(Token, "MORE") )
			    strcpy(More, Arg);

			if ( !strcmp(Token, "DIR") )
			    strcpy(Dir, Arg);
		    }

		fclose(conf);
	    }
    }

/****************************/
/* Decrypt		    */
/****************************/

SHORT Decrypt(char *name)
    {
	LONG size;

	/*******************/
	/* get file length */
	/*******************/
	    {
		struct FileInfoBlock *fblock;

		Message("scanning...");

		if ( fblock = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR | MEMF_PUBLIC) )
		    {
			struct FileLock *slock;

			if ( slock = (struct FileLock *)Lock(name, ACCESS_READ) )
			    {
				if ( Examine(slock, fblock) )
				    {
					if ( fblock->fib_DirEntryType > 0 )
					    {
						Message("It's a directory !");
						return(-1);
					    }
					else
					    size = fblock->fib_Size;
				    }
				else
				    {
					UnLock(fblock);
					FreeMem(fblock, sizeof(struct FileInfoBlock));
					Message("Can't examine file.");
					return(-1);
				    }

				UnLock(slock);
			    }
			else
			    {
				FreeMem(fblock, sizeof(struct FileInfoBlock));
				Message("Can't lock file.");
				return(-1);
			    }

			FreeMem(fblock, sizeof(struct FileInfoBlock));
		    }
		else
		    {
			Message("No memory !");
			return(-1);
		    }
	    }

	/*************/
	/* load data */
	/*************/
	    {
		struct FileHandle *in, *out;
		UBYTE *newmem = NULL;

		Message("loading...");

		newmem = (UBYTE *)AllocMem(size, MEMF_CLEAR | MEMF_PUBLIC);
		if ( newmem )
		    {
			in = (struct FileHandle *)Open(name, MODE_OLDFILE);
			if ( in )
			    {
				if ( Read(in, newmem, size) != size )
				    {
					Close(in);
					FreeMem(newmem, size);
					Message("Read error.");
					return(-1);
				    }
				Close(in);
			    }
			else
			    {
				FreeMem(newmem, size);
				Message("Can't open file.");
				return(-1);
			    }

			/*************/
			/* open dest */
			/*************/

			out = (struct FileHandle *)Open("T:Note", MODE_NEWFILE);

			if ( out )
			    {
				/**************/
				/* decrypt it */
				/**************/

				LONG seed;
				LONG count = size;
				LONG pwcount = 0, pwlen = strlen(Password);
				UBYTE *mem = newmem;

				/************/
				/* get seed */
				/************/
				    {
					char *pass = Password;

					seed = pass[0] * pwlen;
					while ( *pass )
					    seed += *pass++;
				    }

				Message("decrypting...");
				while ( count-- )
				    {
					seed = random(seed);

					if (pwcount == pwlen)
					    pwcount = 0;

					*mem = (*mem + Password[pwcount]);
					*mem = (*mem + seed);

					pwcount++;
					mem++;
				    }

				Message("writing...");

				if ( Write(out, newmem, size) != size )
				    {
					Close(out);
					remove("T:Note");
					FreeMem(newmem, size);
					Message("Write error.");
					return(-1);
				    }

				Close(out);
			    }
			else
			    {
				FreeMem(newmem, size);
				Message("Can't open temporary.");
				return(-1);
			    }

			FreeMem(newmem, size);
		    }
		else
		    {
			Message("Out of memory.");
			return(-1);
		    }
	    }

	Message("done.");
	return(0);
    }

/****************************/
/* Crypt		    */
/****************************/

SHORT Crypt(char *name)
    {
	LONG size;

	/*******************/
	/* get file length */
	/*******************/
	    {
		struct FileInfoBlock *fblock;

		Message("scanning...");

		if ( fblock = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR | MEMF_PUBLIC) )
		    {
			struct FileLock *slock;

			if ( slock = (struct FileLock *)Lock("T:Note", ACCESS_READ) )
			    {
				if ( Examine(slock, fblock) )
				    {
					if ( fblock->fib_DirEntryType > 0 )
					    {
						Message("It's a directory !");
						return(-1);
					    }
					else
					    size = fblock->fib_Size;
				    }
				else
				    {
					UnLock(fblock);
					FreeMem(fblock, sizeof(struct FileInfoBlock));
					Message("Can't examine temporary.");
					return(-1);
				    }

				UnLock(slock);
			    }
			else
			    {
				FreeMem(fblock, sizeof(struct FileInfoBlock));
				Message("Can't lock temporary.");
				return(-1);
			    }

			FreeMem(fblock, sizeof(struct FileInfoBlock));
		    }
		else
		    {
			Message("No memory !");
			return(-1);
		    }
	    }

	/*************/
	/* load data */
	/*************/
	    {
		struct FileHandle *in, *out;
		UBYTE *newmem = NULL;

		Message("loading...");

		newmem = (UBYTE *)AllocMem(size, MEMF_CLEAR | MEMF_PUBLIC);
		if ( newmem )
		    {
			in = (struct FileHandle *)Open("T:Note", MODE_OLDFILE);
			if ( in )
			    {
				if ( Read(in, newmem, size) != size )
				    {
					Close(in);
					FreeMem(newmem, size);
					Message("Read error.");
					return(-1);
				    }
				Close(in);
			    }
			else
			    {
				FreeMem(newmem, size);
				Message("Can't open temporary.");
				return(-1);
			    }

			/*************/
			/* open dest */
			/*************/

			out = (struct FileHandle *)Open(name, MODE_NEWFILE);

			if ( out )
			    {
				/************/
				/* crypt it */
				/************/

				LONG seed;
				LONG count = size;
				LONG pwcount = 0, pwlen = strlen(Password);
				UBYTE *mem = newmem;

				/************/
				/* get seed */
				/************/
				    {
					char *pass = Password;

					seed = pass[0] * pwlen;
					while ( *pass )
					    seed += *pass++;
				    }

				Message("crypting...");

				while (count --)
				    {
					seed = random(seed);

					if (pwcount == pwlen)
					    pwcount = 0;

					*mem = (*mem - Password[pwcount]);
					*mem = (*mem - seed);

					pwcount++;
					mem++;
				    }

				Message("writing...");

				if ( Write(out, newmem, size) != size )
				    {
					Close(out);
					remove(name);
					FreeMem(newmem, size);
					Message("Write error.");
					return(-1);
				    }
				Close(out);
			    }
			else
			    {
				FreeMem(newmem, size);
				Message("Can't open file.");
				return(-1);
			    }

			FreeMem(newmem, size);
		    }
		else
		    {
			Message("Out of memory.");
			return(-1);
		    }
	    }

	Message("done.");
	return(0);
    }

/****************************/
/* LoadText		    */
/****************************/

void LoadText()
    {
	char comline[256];
	char filename[256];

	APTR oldfirst = window->FirstGadget;
	window->FirstGadget = NULL;

	sprintf(filename, "%sData/%s", Dir, LoadDate);
	if ( Decrypt(filename) != -1 )
	    {
		Message("showing...");
		sprintf(comline, "%s T:Note", More);
		Execute(comline, NULL, output);

		remove("T:Note");

		Message("done.");
	    }

	window->FirstGadget = oldfirst;
    }

/****************************/
/* EditTodayText	    */
/****************************/

void EditTodayText()
    {
	char comline[256];
	char filename[256];

	APTR oldfirst = window->FirstGadget;
	window->FirstGadget = NULL;

	sprintf(filename, "%sData/%s", Dir, Date);
	Decrypt(filename);

	Message("editing...");
	sprintf(comline, "%s T:Note", Editor);
	Execute(comline, NULL, output);

	Crypt(filename);
	remove("T:Note");

	window->FirstGadget = oldfirst;
    }

/****************************/
/* EditConfig		    */
/****************************/

void EditConfig()
    {
	char comline[256];

	APTR oldfirst = window->FirstGadget;
	window->FirstGadget = NULL;

	sprintf(comline, "%s s:Note.config", Editor);
	Execute(comline, NULL, output);

	LoadConfiguration();
	Message("Configuration loaded.");

	window->FirstGadget = oldfirst;
    }

/****************************/
/* GetPassword		    */
/****************************/

char GetChar(SHORT xpos)
    {
	struct RastPort *rp = window->RPort;
	struct IntuiMessage *msg;
	SHORT count = 0;

	while (TRUE)
	    {
		Delay(1);
		if (count == 0)
		    {
			Move(rp, xpos, TOP+20);
			Text(rp, "X", 1);
		    }
		else if (count == 8)
		    {
			Move(rp, xpos, TOP+20);
			Text(rp, " ", 1);
		    }

		count = (count + 1) & 15;

		while(msg = (struct IntuiMessage *)GetMsg(window->UserPort))
		    {
			ULONG class;
			USHORT code;

			class = msg->Class;
			code = msg->Code;

			ReplyMsg(msg);

			if ( class = VANILLAKEY )
			    return((char)code);
		    } /* while (msg =... */
	    }
    }

void GetPassword()
    {
	struct RastPort *rp = window->RPort;
	BOOL bool = TRUE;
	SHORT i = 0;
	SHORT xpos = LEFT+16;

	APTR oldfirst = window->FirstGadget;
	window->FirstGadget = NULL;

	SetAPen(rp, 0);
	RectFill(rp, LEFT+4, TOP+2, RIGHT-4, BOTTOM-2);

	SetAPen(rp, 1);
	Move(rp, LEFT+8, TOP+10);
	Text(rp, "Enter password :", 16);

	while (bool && (i < 20))
	    {
		char c;

		c = GetChar(xpos);
		if ( (c == 13) && (i > 4) )
		    bool = FALSE;
		else
		    {
			Password[i++] = c;

			Move(rp, xpos, TOP+20);
			Text(rp, "*", 1);

			xpos += 8;
		    }
	    } /* while (bool) */
	Password[i] = 0;

	/**********/
	/* VERIFY */
	/**********/
	    {
		char Verify[30];

		SetAPen(rp, 0);
		RectFill(rp, LEFT+4, TOP+2, RIGHT-4, BOTTOM-2);

		SetAPen(rp, 1);
		Move(rp, LEFT+8, TOP+10);
		Text(rp, "Verify :", 8);

		bool = TRUE;
		i = 0;
		xpos = LEFT+16;
		while (bool && (i < 20))
		    {
			char c;

			c = GetChar(xpos);
			if ( (c == 13) && (i > 4) )
			    bool = FALSE;
			else
			    {
				Verify[i++] = c;

				Move(rp, xpos, TOP+20);
				Text(rp, "*", 1);
				xpos += 8;
			    }
		    } /* while (bool) */
		Verify[i] = 0;

		if ( !strcmp(Password, Verify) )
		    Message("Password ok !");
		else
		    Message("Wrong password !");
	    }

	window->FirstGadget = oldfirst;
    }

/****************************/
/* Other		    */
/****************************/

struct FileName
    {
	struct FileName *Next;
	struct FileName *Prev;

	char Name[108];
    };

void Other()
    {
	struct RastPort *rp;
	struct FileName *FirstFile = NULL;
	struct FileName *fm, *topf;
	struct FileLock *dlock = NULL;
	struct FileInfoBlock *fblock = NULL;
	struct Window *win = NULL;
	SHORT y, top = NULL;
	char Name[256];
	char comline[256];

	APTR oldfirst = window->FirstGadget;
	window->FirstGadget = NULL;

	sprintf(Name, "%sOther", Dir);

	/******************/
	/* scan directory */
	/******************/

	Message("scanning directory...");

	if ( !( fblock = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR | MEMF_PUBLIC) ) )
	    {
		Message("No memory !");
		goto fault;
	    }

	if ( !( dlock = (struct FileLock *)Lock(Name, ACCESS_READ) ) )
	    {
		Message("Can't lock directory.");
		goto fault;
	    }

	if ( !Examine(dlock, fblock) )
	    {
		Message("Can't examine directory.");
		goto fault;
	    }

	if ( fblock->fib_DirEntryType <= 0 )
	    {
		Message("It's not a directory !");
		goto fault;
	    }

	while ( ExNext(dlock, fblock) )
	    {
		struct FileName *new;

		new = (struct FileName *)AllocMem(sizeof(struct FileName), MEMF_CLEAR | MEMF_PUBLIC);
		if ( new )
		    {
			if ( FirstFile )
			    FirstFile->Prev = new;
			new->Next = FirstFile;
			new->Prev = NULL;
			FirstFile = new;

			strcpy(new->Name, fblock->fib_FileName);
		    }
		else
		    {
			Message("Out of memory !!");
			goto fault;
		    }
	    } /* while ( ExNext... */

	UnLock(dlock);
	dlock = NULL;

	FreeMem(fblock, sizeof(struct FileInfoBlock));
	fblock = NULL;

	/***************/
	/* open window */
	/***************/

	win = (struct Window *)OpenWindow(&lnw);
	if ( !win )
	    {
		Message("Window error.");
		goto fault;
	    }

	rp = win->RPort;

	SetAPen(rp, 1);
	Move(rp, 0,    0);
	Draw(rp, 0,    119);
	Draw(rp, 0+1,  119-1);
	Draw(rp, 0+1,  0);
	Draw(rp, 199-1, 0);
	Draw(rp, 199-3, 0+2);
	Draw(rp, 199-3, 119-2);
	Move(rp, 199-2, 0+2);
	Draw(rp, 199-2, 119-1);
	Draw(rp, 0,    119-1);

	SetAPen(rp, 2);
	Move(rp, 199-1, 0+1);
	Draw(rp, 0+3,  0+1);
	Draw(rp, 0+3,  119-2);
	Move(rp, 0+2,  0+1);
	Draw(rp, 0+2,  119-1);
	Draw(rp, 0+1,  119);
	Draw(rp, 199,   119);
	Draw(rp, 199,   0);
	Draw(rp, 199-1, 0+1);
	Draw(rp, 199-1, 119);

	SetAPen(rp, 1);
	for ( fm = FirstFile, y = 10 ; fm && (y < 115) ; fm = fm->Next, y += 8 )
	    {
		Move(rp, 8, y);
		Text(rp, fm->Name, (strlen(fm->Name) < 21) ? strlen(fm->Name) : 20);
	    }

	topf = FirstFile;

	/**********/
	/* Select */
	/**********/
	    {
		BOOL bool = TRUE;

		while ( bool )
		    {
			struct IntuiMessage *msg;

			Wait( 1 << win->UserPort->mp_SigBit );

			while(msg = (struct IntuiMessage *)GetMsg(win->UserPort))
			    {
				ULONG class;
				USHORT code;
				struct Gadget *iaddress;
				SHORT mousex, mousey;

				class = msg->Class;
				code = msg->Code;
				iaddress = msg->IAddress;
				mousex = msg->MouseX;
				mousey = msg->MouseY;

				ReplyMsg(msg);

				switch (class)
				    {
					case GADGETDOWN :
					    {
						switch (iaddress->GadgetID)
						    {
							case 0 :
							    {
								while ( LGadgetUp.Flags & SELECTED )
								    {
									if ( topf->Prev )
									    {
										topf = topf->Prev;
										top--;

										ScrollRaster(rp, 0, -8, 8, 4, 169, 115);
										SetAPen(rp, 0);
										RectFill(rp, 8, 4, 169, 11);
										SetAPen(rp, 1);
										Move(rp, 8, 10);
										Text(rp, topf->Name, (strlen(topf->Name) < 21) ? strlen(topf->Name) : 20);
									    }
									Delay(2);
								    }
								break;
							    }
							case 1 :
							    {
								while ( LGadgetDown.Flags & SELECTED )
								    {
									fm = topf;
									for ( y = 0 ; fm && (y < 14) ; y++, fm = fm->Next );

									if ( fm )
									    {
										topf = topf->Next;
										top++;

										ScrollRaster(rp, 0, 8, 8, 4, 169, 115);
										SetAPen(rp, 0);
										RectFill(rp, 8, 108, 169, 115);
										SetAPen(rp, 1);
										Move(rp, 8, 114);
										Text(rp, fm->Name, (strlen(fm->Name) < 21) ? strlen(fm->Name) : 20);
									    }
									Delay(2);
								    }
								break;
							    }
						    }
						break;
					    } /* GADGETDOWN */
					case MOUSEBUTTONS :
					    {
						switch ( code )
						    {
							case SELECTUP :
							    {
								if ( (mousex > 8) && (mousex < 170) && (mousey > 4) && (mousey <116) )
								    {
									mousey = ( (mousey - 4) >> 3 ) + top;

									for ( fm = FirstFile ; fm && mousey ; mousey-- )
									    fm = fm->Next;

									if ( fm )
									    bool = FALSE;
								    }
								break;
							    }
							case MENUUP :
							    {
								fm = NULL;
								bool = FALSE;
								break;
							    }
						    }
					    } /* MOUSEBUTTONS */
				    } /* switch (class) */
			    } /* while ( msg =... */
		    } /* while (bool) */
	    } /* Select */

	CloseWindow(win);
	win = NULL;

	/***********/
	/* edit it */
	/***********/

	if ( fm )
	    {
		sprintf(Name, "%sOther/%s", Dir, fm->Name);
		Decrypt(Name);

		Message("editing...");
		sprintf(comline, "%s T:Note", Editor);
		Execute(comline, NULL, output);

		Crypt(Name);
		remove("T:Note");
	    } /* edit it */

	Message("done.");

fault:	if (fblock)
	    FreeMem(fblock, sizeof(struct FileInfoBlock));
	if (dlock)
	    UnLock(dlock);
	if (win)
	    CloseWindow(win);

	while ( FirstFile )
	    {
		struct FileName *next;

		next = FirstFile->Next;
		FreeMem(FirstFile, sizeof(struct FileName));
		FirstFile = next;
	    }

	window->FirstGadget = oldfirst;
    }

/****************************/
/* New			    */
/****************************/

void New()
    {
	struct Window *win;
	struct RastPort *rp;
	BOOL ok;
	char FileName[256];

	win = (struct Window *)OpenWindow(&nnw);
	if ( !win )
	    {
		Message("Window error.");
		return();
	    }

	rp = win->RPort;

	SetAPen(rp, 1);
	Move(rp, 0,    0);
	Draw(rp, 0,    42);
	Draw(rp, 0+1,  42-1);
	Draw(rp, 0+1,  0);
	Draw(rp, 187-1, 0);
	Draw(rp, 187-3, 0+2);
	Draw(rp, 187-3, 42-2);
	Move(rp, 187-2, 0+2);
	Draw(rp, 187-2, 42-1);
	Draw(rp, 0,    42-1);

	SetAPen(rp, 2);
	Move(rp, 187-1, 0+1);
	Draw(rp, 0+3,  0+1);
	Draw(rp, 0+3,  42-2);
	Move(rp, 0+2,  0+1);
	Draw(rp, 0+2,  42-1);
	Draw(rp, 0+1,  42);
	Draw(rp, 187,   42);
	Draw(rp, 187,   0);
	Draw(rp, 187-1, 0+1);
	Draw(rp, 187-1, 42);

	Message("Enter filename.");

	/**********/
	/* Select */
	/**********/
	    {
		BOOL bool = TRUE;

		while ( bool )
		    {
			struct IntuiMessage *msg;

			Wait( 1 << win->UserPort->mp_SigBit );

			while(msg = (struct IntuiMessage *)GetMsg(win->UserPort))
			    {
				ULONG class;
				struct Gadget *iaddress;

				class = msg->Class;
				iaddress = msg->IAddress;

				ReplyMsg(msg);

				if ( class == GADGETUP )
				    {
					bool = FALSE;

					switch (iaddress->GadgetID)
					    {
						case 0 :
						case 2 :
						    {
							ok = TRUE;
							break;
						    }
						case 1 :
						    {
							ok = FALSE;
							break;
						    }
					    } /* switch */
				    } /* if (class) */
			    } /* while ( msg =... */
		    } /* while (bool) */
	    } /* Select */

	CloseWindow(win);

	if ( ok && strlen(NameBuffer) )
	    {
		struct FileHandle *file;

		sprintf(FileName, "%sOther/%s", Dir, NameBuffer);

		if ( file = (struct FileHandle *)Open(FileName, MODE_OLDFILE) )
		    {
			Close(file);
			Message("File already exists.");
			return();
		    }

		if ( file = (struct FileHandle *)Open(FileName, MODE_NEWFILE) )
		    {
			Close(file);
			Message("File created.");
		    }
	    }
	else
	    Message("Job cancelled.");
    }

/****************************/
/* MAIN 		    */
/****************************/

int main()
    {
	if ( !output )
	    output = (struct FileHandle *)Output();

	/************/
	/* get date */
	/************/
	    {
		time_t t;

		t = time(NULL);
		date = localtime(&t);
		sprintf(Date, "%02d-%02d-%02d", date->tm_mday, date->tm_mon+1, date->tm_year);
		sprintf(Title, "NoteEdit V1.0   %s", Date);
	    }

	/*************/
	/* load conf */
	/*************/

	LoadConfiguration();

	window = (struct Window *)OpenWindow(&nw);
	if (window)
	    {
		struct RastPort *rp = window->RPort;

		SetWindowTitles(window, -1, "A Paradise Soft Production");


		SetAPen(rp, 1);
		Move(rp, LEFT,    TOP);
		Draw(rp, LEFT,    BOTTOM);
		Draw(rp, LEFT+1,  BOTTOM-1);
		Draw(rp, LEFT+1,  TOP);
		Draw(rp, RIGHT-1, TOP);
		Draw(rp, RIGHT-3, TOP+2);
		Draw(rp, RIGHT-3, BOTTOM-2);
		Move(rp, RIGHT-2, TOP+2);
		Draw(rp, RIGHT-2, BOTTOM-1);
		Draw(rp, LEFT,    BOTTOM-1);

		SetAPen(rp, 2);
		Move(rp, RIGHT-1, TOP+1);
		Draw(rp, LEFT+3,  TOP+1);
		Draw(rp, LEFT+3,  BOTTOM-2);
		Move(rp, LEFT+2,  TOP+1);
		Draw(rp, LEFT+2,  BOTTOM-1);
		Draw(rp, LEFT+1,  BOTTOM);
		Draw(rp, RIGHT,   BOTTOM);
		Draw(rp, RIGHT,   TOP);
		Draw(rp, RIGHT-1, TOP+1);
		Draw(rp, RIGHT-1, BOTTOM);

		ShowLoadDate();

		/****************/
		/* GET PASSWORD */
		/****************/

		GetPassword();

		/*************/
		/* MAIN LOOP */
		/*************/

		BOOL bool = TRUE;

		while (bool)
		    {
			struct IntuiMessage *msg;

			Wait( 1 << window->UserPort->mp_SigBit );

			while(msg = (struct IntuiMessage *)GetMsg(window->UserPort))
			    {
				ULONG class;
				USHORT code;
				struct Gadget *iaddress;

				class = msg->Class;
				code = msg->Code;
				iaddress = msg->IAddress;

				ReplyMsg(msg);

				switch (class)
				    {
					case GADGETUP :
					    {
						switch (iaddress->GadgetID)
						    {
							case 0 :
							    {
								EditTodayText();
								break;
							    }
							case 1 :
							    {
								GetPassword();
								break;
							    }
							case 2 :
							    {
								EditConfig();
								break;
							    }
							case 3 :
							    {
								LoadText();
								break;
							    }
							case 4 :
							    {
								Other();
								break;
							    }
							case 5 :
							    {
								New();
								break;
							    }
							case 10 :
							    {
								date->tm_mday++;
								if ( date->tm_mday == 32 )
								    date->tm_mday = 1;

								ShowLoadDate();
								break;
							    }
							case 11 :
							    {
								date->tm_mon++;
								if ( date->tm_mon == 12 )
								    date->tm_mon = 0;

								ShowLoadDate();
								break;
							    }
							case 12 :
							    {
								date->tm_year++;
								if ( date->tm_year == 100 )
								    date->tm_year = 00;

								ShowLoadDate();
								break;
							    }
							case 20 :
							    {
								date->tm_mday--;
								if ( date->tm_mday == 0 )
								    date->tm_mday = 31;

								ShowLoadDate();
								break;
							    }
							case 21 :
							    {
								date->tm_mon--;
								if ( date->tm_mon == -1 )
								    date->tm_mon = 11;

								ShowLoadDate();
								break;
							    }
							case 22 :
							    {
								date->tm_year--;
								if ( date->tm_year == -1 )
								    date->tm_year = 99;

								ShowLoadDate();
								break;
							    }
						    }
						break;
					    }
					case CLOSEWINDOW :
					    {
						bool = FALSE;
						break;
					    }
				    } /* switch class */
			    } /* while msg */
		    } /* while (bool) */

		/********/
		/* EXIT */
		/********/

		CloseWindow(window);
	    } /* if (window) */

	return(0);
    }

/****************************/
/* WBMAIN		    */
/****************************/

int wbmain(void *dummy)
    {
	output = (struct FileHandle *)Open("CON:0/200/640/56/Output", MODE_OLDFILE);

	if ( output )
	    {
		main();
		Close(output);
	    }

	return(0);
    }

