/*
 *	PatchReq.c - Copyright © 1991 by Devil's child.
 *
 *	Created:	25 Jan 1991  19:44:11
 *	Modified:	25 Jul 1991  19:15:27
 *
 *	Make>> cc -qf -ps -so -sb -wu -wd -wp -md -hi ram:small.sym <file>.c
 *	Make>> ln -m <file>.o -larpsc
 */

#include <dos_functions.h>
#include <arpdos_pragmas.h>

extern struct ArpBase *ArpBase;
extern int _argc;
extern char **_argv;

#define	DIR_CACHE	1
#define NOAUTOREQ	2


void main(int argc, char *argv[])
{
	struct ReqLib *ReqBase;
	PVF *FuncTab;
	long (*OldTextRequest)();
	long (*OldAutoRequest)();
	BPTR Segment;
	PVF *(*Setup)(struct ArpBase *, struct ReqLib *, long (*)(), long (*)(), short);
	short Flags = 0;

	if (!(ReqBase = (struct ReqLib *)OpenLibrary("req.library", 1))) {
		Printf("You need req.library\n");
		ArpExit(20,0);
	}
	if ((Segment = LoadSeg("PatchReq-Seg")) || (Segment = LoadSeg("L:PatchReq-Seg"))) {
		Printf("[33mPatchReq[31m V1.4 Copyright © 1991 by P.C.\nArp FileRequest() Patched.\n");
		if (argv[1])
			Flags |= NOAUTOREQ;
		else
			Printf("AutoRequest() Patched.\n");
		if (argv[0]) {
			Flags |= DIR_CACHE;
			Printf("Directory cache on.\n");
		}
		Setup = (PVF *(*)())((Segment << 2) + 4);
		Forbid();
		OldTextRequest = (long (*)())SetFunction((struct Library *)ReqBase, -0xAE, NULL);				/* Get Old function to give to Setup() */
		OldAutoRequest = (long (*)())SetFunction((struct Library *)ReqBase->IntuiLib, -0x15C, NULL);	/* Get Old function to give to Setup() */
		FuncTab = Setup(ArpBase, ReqBase, OldTextRequest, OldAutoRequest, Flags);
		SetFunction((struct Library *)ReqBase->IntuiLib, -0x15C, FuncTab[0]);	/* NewAutoRequest() */
		SetFunction((struct Library *)ReqBase, -0xAE, FuncTab[1]);				/* NewTextRequest() */
		SetFunction((struct Library *)ArpBase, -0x126, FuncTab[2]);				/* NewFileRequest() */
		Permit();
	}
	else {
		Printf("Couldn't load PatchReq-Seg\n");
		ArpExit(20,0);
	}
}


void _cli_parse(struct Process *pp, long alen, char *aptr)
{
	_argv = ArpAlloc(12);
	_argc = (int)GADS(aptr, alen, "Usage: PatchReq [CACHE] [NOAUTOREQ]", _argv, "CACHE/s,NOAUTOREQ/s");
	if (_argc < 0) {
		Puts(_argv[0]);
		ArpExit(20L, ERROR_LINE_TOO_LONG);
	}
}


