/* NoDelete V1.5A (C) HooverSoft 1991

	cc NoDelete +L
	ln NoDelete -la -lc32


*/


#ifdef AZTEC_C					/* Works under Manx, convert it to Lattice */
									/* if you don't mind the hassle...			 */

#include<libraries/arpbase.h>
#include<intuition/intuition.h>
#include<intuition/intuitionbase.h>
#include<libraries/dos.h>
#include<libraries/dosextens.h>
#include<stdio.h>
/* #define DEBUG 1 */

char bodystring[80],patterns[10][10], titlebak[80], title[80];
ULONG answer, PAT_SET = 0L,  howmany = 0L;
ULONG OVERRIDE = 0L, SILENT = 0L;


struct NewWindow mywindow = {
	100,0,	/* window XY origin relative to TopLeft of screen */
	350,10,	/* window width and height */
	0,1,	/* detail and block pens */
	CLOSEWINDOW+VANILLAKEY,	/* IDCMP flags */
	WINDOWDRAG+WINDOWDEPTH+WINDOWCLOSE+SIMPLE_REFRESH,	/* other window flags */
	NULL,	/* first gadget in gadget list */
	NULL,	/* custom CHECKMARK imagery */
	(UBYTE *)title,	/* window title */
	NULL,	/* custom screen pointer */
	NULL,	/* custom bitmap */
	5,5,	/* minimum width and height */
	640,110, /* maximum width and height */
	WBENCHSCREEN	/* destination screen type */
	};


struct IntuiText body = {
	1,0,
	JAM1|COMPLEMENT,
	30,16,
	NULL,
	NULL,
	NULL
	};

struct IntuiText fbody = {
	1,0,
	JAM1|COMPLEMENT,
	30,5,
	NULL,
	(UBYTE *) "System tries to fool around...",
	&body
	};

struct IntuiText ok = {
	1,0,
	JAM1|COMPLEMENT,
	6,3,
	NULL,
	(UBYTE *)	"Yes, Delete!",
	NULL
	};

struct IntuiText no = {
	1,0,
	JAM1|COMPLEMENT,
	6,3,
	NULL,
	(UBYTE *)  "Oops, No Way!",
	NULL
	};


struct IntuiMessage *msg, *GetMsg();

struct IntuitionBase *IntuitionBase;
struct Window			*mw, *aw, *OpenWindow();
struct DosBase			*DosBase;
struct ArpBase			*ArpBase;
UBYTE						*filename;
void 						*OpenLibrary();
struct Task				*FindTask();

void NoDelete()
{
	char parsed[10];
#asm
	public	_LVOSetProtection

	movem.l	d0-d7/a0-a6,-(sp)
#endasm
	geta4();
#asm
	move.l	d1,_filename
#endasm

	if(PAT_SET)
		{

		for(answer=0L; answer < howmany; answer++)			
			{
			PreParse(patterns[answer], parsed);
			if( !PatternMatch(parsed, filename) ) /* this is new */
				continue;
			else
				goto request;
			}
		}
	else 
		goto request;
	goto delete_anyway;		
		
request:
	strcpy(bodystring, "Delete File `");
	strcat(bodystring, (char *)filename);
	strcat(bodystring, "'?");
	body.IText = (UBYTE *) bodystring;
	aw = (struct Window *) IntuitionBase->ActiveWindow;
	answer = (long)AutoRequest(aw, &fbody, &ok, &no, NULL, NULL, 360L, 61L);
	if(!answer)
	{
		;
#asm
	movem.l	(sp)+,d0-d7/a0-a6
	move.l	#1,d0
#endasm
	return;
	}

	if(answer)
	{

delete_anyway:
		;
#asm

		move.l	_DosBase,a6		
		move.l	#$70f7,d0			;restore old vector
		move.w	d0,-72(a6)
		move.l	#$60000092,d0
		move.l	d0,-70(a6)
		
		tst.l		_OVERRIDE
		beq.s		_dont_unprotect

		move.l	_filename,d1


		move.l	#0,d2
		jsr		_LVOSetProtection(a6)

_dont_unprotect:

		move.l	_filename,d1
		jsr		-72(a6)				;DeleteFile() call
#endasm

		;

		SetFunction(DosBase, -72L, &NoDelete);

/* install our function */


#asm
		movem.l	(sp)+,d0-d7/a0-a6
		move.l	#1,d0
#endasm
		return;
		}
	}
		

void quit(why)
UBYTE *why;
{
	if(!SILENT)
		Printf(why);
	SetTaskPri((struct Task *)FindTask(0L), 0L);
	if(DosBase)
	{
		;
#asm
		move.l	_DosBase,a6		
		move.l	#$70f7,d0			;restore old vector
		move.w	d0,-72(a6)
		move.l	#$60000092,d0
		move.l	d0,-70(a6)
#endasm
		;
	}

	if	(mw)
		CloseWindowSafely(mw);

	if(ArpBase)
		CloseLibrary(ArpBase);
	exit(0L);
}

/* test_nd() tests the DeleteFile vector to see if NoDelete is already 
	installed. Returns zero if NoDelete is installed, one if it is not.
*/
#asm
		public	_test_nd
_test_nd:

		move.l	_DosBase,a6
		cmpi.l	#$60000092,-70(a6)
		bne.s		_is_there				
		moveq		#1,d0
		bra.s		_fuckoff
_is_there:
		moveq		#0,d0
_fuckoff:
		rts	
#endasm


void main(argc,argv)
long argc;
char *argv[];
	{

	static UBYTE *msg1 = (UBYTE *)"Installing NoDelete...";
	static UBYTE *msg2 = (UBYTE *)"done. NoDelete V1.5A (c) HooverSoft 1991. \
This Program is FREEWARE.\n";
	long i;

	if(! (ArpBase = (struct ArpBase *) OpenLibrary("arp.library",0L) ) )
		quit("NoDelete needs arp.library\n");
	
	DosBase = (struct DosBase *) ArpBase->DosBase;
	IntuitionBase = (struct IntuitionBase *) ArpBase->IntuiBase;  

	strcpy(title, (UBYTE *) "NoDelete V1.5A (c) HooverSoft");


	


			

#ifdef	DEBUG
		Printf("I found %ld patterns.\n", howmany);
#endif			



	for(i=1; i<argc; i++)
		{
#ifdef DEBUG
			Printf("Scanning argv[%ld]: %s\n", i, (UBYTE *) argv[i] );
#endif
			if( (char) argv[i][0] == '-')
				{
				switch( (char) argv[i][1] )
					{
					case 'y':	if( atoi(argv[i+1]) <= 245L)
										mywindow.TopEdge = atoi(argv[i+1]);
									i++;	
									goto fuckoff;					
 									break;

					case 'p':	if(atoi(argv[i+1]) < 10L && atoi(argv[i+1]) > -20L)
										SetTaskPri((struct Task *)FindTask(0L),\
														(long)atoi(argv[i+1]));
									else
										SetTaskPri((struct Task *)FindTask(0L), -5L);

									i++;
									goto fuckoff;
									break;


					case  'O':	OVERRIDE = 1L;
									goto fuckoff;
									break;
				
					case  'S':	SILENT = 1L;
									goto fuckoff;
									break;
				
					case default:
									Printf("Unknown Option -%c\n", argv[i][1]); 
									goto fuckoff; 
									break;
					}
				/* end switch */
				}/*end if */
			else
				{
				PAT_SET = 1L;
				if( howmany<10 )
					strcpy(patterns[howmany], argv[i]);
#ifdef	DEBUG
				Printf("Copied %s, Result: %s\n", argv[i], patterns[i-1L]);
#endif	
				howmany++;
				}
fuckoff:
			;
		/*end for */
			}	
					
	if(howmany)
		strcat(title, (UBYTE *) " PAT SET");			
	else
		{
		PAT_SET = 0L;
		strcat(title, (UBYTE *) " NO PAT");
		}

/* calculate dimension of TitleBar */

	mywindow.Width = ( strlen(title) * 8L) + 85L;
	mywindow.LeftEdge = (640L-mywindow.Width) / 2L;

	if(!SILENT)
		Printf(msg1);

	if( !test_nd() )
		{
		Printf("NoDelete already installed.\n");
		CloseLibrary(DosBase);
		CloseLibrary(IntuitionBase);
		CloseLibrary(ArpBase);
		exit(0L);
		}

	mw = OpenWindow(&mywindow);
	if(!mw)
		quit("Could not open Window.\n\n");
	if(!SILENT)
		Printf(msg2);

	SetFunction(DosBase, -72L, &NoDelete); 

	for(;;)
		{
		Wait(1L << mw->UserPort->mp_SigBit);
getmsg:
		msg = GetMsg(mw->UserPort);
		ReplyMsg(msg);
		if (msg->Class == CLOSEWINDOW)
			quit("NoDelete V1.5A exiting.\n\n");
		if(msg->Class == VANILLAKEY)
			{
			strcpy(titlebak, (UBYTE *) "Patterns: ");
			for(i=0L; i < howmany ; i++)
				{
				strcat(titlebak, patterns[i]);
				strcat(titlebak, " ");
				}
			SetWindowTitles(mw, titlebak);
			Wait(1L << mw->UserPort->mp_SigBit);
			SetWindowTitles(mw, title);
			ReplyMsg( GetMsg(mw->UserPort) );
			}

		else
			continue;
		}
	}
#endif	
	
