/* NoDelete V1.35E (C) HooverSoft 1990

   cc NoDelete +L
   ln NoDelete -lc32


*/


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

#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],extensions[10][10], titlebak[80], title[80];
ULONG answer, EXT_SET = 0L,  howmany = 0L;
UBYTE *position, *rindex();

struct FileHandle *output, *Output();

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 Library       *DosBase;
UBYTE                *filename;
void                 *OpenLibrary();
struct Task          *FindTask();

void NoDelete()
{
#asm
   public   _LVOSetProtection

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

   if(EXT_SET)
      {

      position = rindex(filename, '.');
      if(!position)
         goto delete_anyway;
      else
         {
         for(answer=0L; answer < howmany; answer++)         
            {
            if( (strcmp( (UBYTE *) position, (UBYTE *) extensions[answer])) != 0L)
               continue;
            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)

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

      move.l   _filename,d1
      jsr      -72(a6)           ;call DeleteFile() 
#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;
{
   Write(output, why, (ULONG) strlen(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)
      CloseWindow(mw);

   if (DosBase)   
      CloseLibrary(DosBase);

   if (IntuitionBase)
      CloseLibrary(IntuitionBase);

   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    _end
_is_there:
      moveq    #0,d0
_end:
      rts   
#endasm

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

   static UBYTE *msg1 = (UBYTE *)"Installing NoDelete...";
   static UBYTE *msg2 = (UBYTE *)"done. NoDelete V1.35E (c) HooverSoft 1990\n\n";
   long i;

   DosBase = (struct Library *) OpenLibrary("dos.library",0L);
   output = Output();
   strcpy(title, (UBYTE *) "NoDelete V1.35E (c) HooverSoft");
   
   if(argc > 11)
      quit("Sorry, only 10 extensions allowed!\n");

/* Der erste Teil dieser If-Anweisung sucht nach allen
   gueltigen Endungen, die zu finden sind.
*/
   if(argc >= 2)
      {
      for(i=1; i<argc; i++)
         {
   
         if(argv[i][0] == '.')
            {
            EXT_SET = 1L;
            strcpy(extensions[howmany], argv[i]);
            howmany++;
            }
         else
          continue;

#ifdef   DEBUG
         printf("Copied %s, Result: %s\n", argv[i], extensions[i-1L]);
#endif   
         }

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


/* Found any extensions in command line? 
   if we did, please display */
      if(howmany)
            strcat(title, (UBYTE *) " EXT SET");         
/* Search for options */

      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]);
                           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);
                           break;

               case default: break;
               }
            /* end switch */
         /*end if */
      /*end for */
         }  
               
      }

   if(!howmany)
      {
      EXT_SET = 0L;
      strcat(title, (UBYTE *) " NO EXT");
      }

/* calculate dimension of TitleBar */

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

   IntuitionBase =  OpenLibrary("intuition.library",0L);
   Write(output, msg1, (ULONG)strlen(msg1));

   if( !test_nd() )
      {
      Write(output, "NoDelete already installed.\n",28L);
      CloseLibrary(DosBase);
      CloseLibrary(IntuitionBase);
      exit(0L);
      }

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

   Write(output, msg2, (ULONG)strlen(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.35E exiting.\n\n");
      if(msg->Class == VANILLAKEY)
         {
         strcpy(titlebak, (UBYTE *) "Extensions: ");
         for(i=0L; i < howmany ; i++)
            {
            strcat(titlebak, extensions[i]);
            strcat(titlebak, " ");
            }
         SetWindowTitles(mw, titlebak);
         Wait(1L << mw->UserPort->mp_SigBit);
         SetWindowTitles(mw, title);
         ReplyMsg( GetMsg(mw->UserPort) );
         }

      else
         continue;
      }
   }
#endif   
   
