#include "dos.h"
#include "dir.h"
#include "stdio.h"
int P = 1;
char filespec[12] = "*.*";
int fls = 0;
int drs = 0;

main(argc,argv)
  int argc;
  char *argv[];
  {
  char answer = 'N';
  char parameter[4];
  int  parnum = 2;

  if ( ( argc == 1 ) | ( *argv[1] == '?') | (*argv[1] == '/') )
  {
    printf("NUKE! 2.00, Copyright 1991 by Chris Knight\n");
    printf("\n    NUKE pathname [/N:filespec] [/P] [/Y]\n");
    printf("\nSwitches");
    printf("\n/N:filespec   Delete specific files within tree; wildcards allowed.");
    printf("\n/P            Preserve directory tree; default option for /N");
    printf("\n/Y            No prompt, for batch files.\n");
    return(1);
  }

  for (;parnum < argc;)
    {
    strncpy(parameter, argv[parnum], 3);
    switch (toupper(parameter[1])) {
      case 'N':  strncpy(filespec, (argv[parnum]+3), 12);
                 P = 0;
                 break;
      case 'P':  P = 0;
                 break;
      case 'Y':  answer = 'Y';
                 break;
      default :  printf("\nbad majik.  NUKE /? for help.\n");
                 return(1);
                 break;
      }
    parnum++;
    }

  if (answer == 'N')
    {
    printf("\nAre you sure you wish to NUKE the \"%s\" DIR? ", argv[1]);
    answer = getche();
    }
  if ( toupper(answer) == 'Y')
    {
    if (nuke(argv[1]) == 0)
      printf("\nErased %d files, and removed %d directories.\n\n",fls, drs);
    else
      printf("\nExited with an error.\n\n");
    }
  else
    printf("\nBetter safe than sorry!\n");
  return(0);
  }


nuke(directory)
  char *directory;
  {
  struct ffblk f;
  register int done;

  if ( chdir(directory) != 0 )
    {
    printf("\nDirectory %s does not exist.", directory);
    return(1);
    }

  erase();

  done = findfirst("*.*", &f, 255);
  while (!done)
    {
    if ( (*f.ff_name != '.') & ( (f.ff_attrib & FA_DIREC) != 0) )
      if ( nuke(f.ff_name) != 0 )
        return(3);
    done = findnext(&f);
    }

    chdir("..");
    if ( P )
      {
      if ( rmdir(directory) != 0 )
        {
        printf("\nUnable to remove directory:  %s.", directory);
        printf("\nPlease contact the author, Chris Knight.");
        return(4);
        }
       else drs++;
      }
  return(0);
  }


erase()
  {
  struct ffblk f;
  register int done;
  int FILES = (255 - FA_DIREC);

  done = findfirst(filespec, &f, FILES);
  while (!done)
    {
    if ( remove(f.ff_name) != 0)
      {
      _chmod(f.ff_name, 1, 0);
      remove(f.ff_name);
      }
    fls++;
    done = findnext(&f);
    }
  return(0);
  }