//typedef long AVLKey;
#include "dos\dos.h"
#include <proto/dos.h>
#include <proto\exec.h>
#include "inline\exec_protos.h"
#include "inline\dos_protos.h"
#include <string.h>

extern struct Library * SysBase;
int main(int argc, char **argv)
{
	argc = argc;
	argv=argv;
	struct Library *DosLib = OldOpenLibrary("dos.library");
	int error = 1;
	if (DosLib)
	{
		struct DosLibrary *DOSBase = (struct DosLibrary*)DosLib;
		BPTR outputHandle=Output();

		char targetString[] = "Usecode: $";
		int targetStringLen = strlen(targetString);
		char* ptr = 0;
		int memOffset = 0x200000;
		int memSize = 0x200000;
		char* tempPtr = ptr + memOffset;
		char* tempPtrEnd = tempPtr + memSize;

		if (argc > 1)
		{
			BPTR fileHandle = Open(argv[1], MODE_NEWFILE);
			if (fileHandle)
			{
				BOOL found = FALSE;
				for (; tempPtr < tempPtrEnd; tempPtr++)
				{
					if (*tempPtr == 'U' && tempPtr[1] == 's' && !strncmp(tempPtr, targetString, targetStringLen))
					{
						char* useCodePtr = tempPtr + targetStringLen;
						if ((useCodePtr[0] >= '0' && useCodePtr[0] <= '9') || (useCodePtr[0] >= 'A' && useCodePtr[0] <= 'F'))
						{
							found = TRUE;
							error = 0;
							Write(fileHandle, "usecode: set (usecode|$", 23);
							Write(fileHandle, useCodePtr, 4);
							Write(fileHandle, ")\n", 2);
							break;
						}
					}
				}
				//		char* ptr = argv[1];
				if (!found)
				{
					Write(outputHandle, "Usecode not Found in memory!\n", 29);
				}
				Close(fileHandle);
			}
			else
			{
				Write(outputHandle, "Could not open file ", 20);
				Write(outputHandle, argv[1], strlen(argv[1]));
				Write(outputHandle, " for writing!\n", 14);
			}
		}
		else
		{
			Write(outputHandle, "No output filename!\n", 20);
		}
		CloseLibrary(DosLib);
	}
	return error;
}