/* global erase files on given disk incl. hidden/ro/sys files
   HZ+JBT, 2/11/1995 ofzo en 9/11/1995
   en 26/2/1996, TC3 compilation feb.97 */


#include <conio.h>
#include <ctype.h>
#include <dir.h>
#include <direct.h>
#include <dos.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MASK1 FA_DIREC /* (sub)dirs */
#define MASK2 FA_RDONLY|FA_HIDDEN|FA_SYSTEM /* all files */

int prompt(void);
void kill(void);
void tree(char *path);


char searchpattern[8][20];
int lastpattern;
int PathSpecified=0;
int EntryDrive,OurDrive;
char buf[MAXPATH];
char fullname[MAXPATH];
char startpath[MAXPATH];
int total=0;
int Nulfiles=0,Batch=0,Askfirst=0;
int FlagsUsed=0;
int leavecolor=7;


int prompt(){
    /* ret 1 if yes, else 0 */
    int c;

    while(kbhit())
        getch();
    c=getch();
    if(c==27){
        _chdrive(EntryDrive);
        textcolor(leavecolor);
        cprintf(" ");
        exit(0);
    }
    return(toupper(c)=='Y');
}


void kill(){
    /* fullname is global, dus die hoeven we niet door te geven */
    _dos_setfileattr(fullname,0);
    if(!unlink(fullname))
        total++;
}


void tree(char *path){
    char search[MAXPATH],nieuw[MAXPATH];
    struct ffblk ff,gg;
    int i;

    textcolor(7);
    cprintf("Searching: %s\n\r",path);

    for(i=0;i<lastpattern;i++){
        sprintf(buf,"%s%s",path,searchpattern[i]);
        if(!findfirst(buf,&gg,MASK2)){
            sprintf(fullname,"%s%s",path,gg.ff_name);
            if(Askfirst){
                textcolor(12);
                cprintf("Erase %s? (y/n)\n\r",fullname);
                if(!prompt()){
                    textcolor(7);
                    cprintf("Skipped\n\r");
                }
                else
                    kill();
            }
            else
                kill();
            while(!findnext(&gg)){
                sprintf(fullname,"%s%s",path,gg.ff_name);
                if(Askfirst){
                    textcolor(12);
                    cprintf("Erase %s? (y/n)\n\r",fullname);
                    if(!prompt()){
                        textcolor(7);
                        cprintf("Skipped\n\r");
                    }
                    else
                        kill();
                }
                else
                    kill();
            }
        }
    }

    if(Nulfiles){
        sprintf(buf,"%s*.*",path);
        if(!findfirst(buf,&gg,MASK2)){
            if(gg.ff_fsize==0){
                sprintf(fullname,"%s%s",path,gg.ff_name);
                if(Askfirst){
                    textcolor(12);
                    cprintf("Erase %s (0 BYTES)? (y/n)\n\r",fullname);
                    if(!prompt()){
                        textcolor(7);
                        cprintf("Skipped\n\r");
                    }
                    else
                        kill();
                }
                else
                    kill();
            }

            while(!findnext(&gg)){
                if(gg.ff_fsize==0){
                    sprintf(fullname,"%s%s",path,gg.ff_name);
                    if(Askfirst){
                        textcolor(12);
                        cprintf("Erase %s (0 BYTES)? (y/n)\n\r",fullname);
                        if(!prompt()){
                            textcolor(7);
                            cprintf("Skipped\n\r");
                        }
                        else
                            kill();
                    }
                    else
                        kill();
                }
            }
        }
    }

    sprintf(search,"%s*.*",path);
    if(findfirst(search,&ff,MASK1)==0){
        do{
            if((ff.ff_attrib&FA_DIREC)&&*ff.ff_name!='.'){
                sprintf(nieuw,"%s%s\\",path,ff.ff_name);
                tree(nieuw);
            }
        }
        while(findnext(&ff)==0);
    }
}


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

    if(argc==1){
        textcolor(14);
        cprintf("OMNIDEL [path] [searchpattern] [searchpattern] [searchpattern] [flags]\n\r");
        textcolor(15);
        cprintf("-[path] must be first parameter, omnidel works from there and below,\n\r");
        cprintf(" should look like this: ");
        textcolor(10);
        cprintf("A:");
        textcolor(15);
        cprintf(", ");
        textcolor(10);
        cprintf("C:");
        textcolor(15);
        cprintf(", ");
        textcolor(10);
        cprintf("D:");
        textcolor(15);
        cprintf(", ");
        textcolor(10);
        cprintf("C:\\WIN95");
        textcolor(15);
        cprintf(" or ");
        textcolor(10);
        cprintf("D:\\TRUE.BMP\\WALLET\n\r");
        textcolor(15);
        cprintf(" Leaving out this parameter causes omnidel to work in current directory!\n\r");
        cprintf("-MAXIMUM OF 8 SEARCHPATTERNS in a row, separated by a space\n\r");
        cprintf("-[searchpattern] can have the usual wildcards (??? and *)\n\r");
        cprintf("-[flags] must be last parameter and prefixed with /\n\r");
        cprintf(" FLAG-COMMANDS can be:\n\r");
        cprintf(" 0 = Erase all 0-byte files on specified drive\n\r");
        cprintf(" a = Automatic batch-execution (skips all prompting, so be careful!)\n\r");
        cprintf(" p = Prompt before each file to be deleted (overrides a)\n\r");
        cprintf("Examples could look like this:\n\r");
        textcolor(10);
        cprintf("C:\\>OMNIDEL *.DUP /0\n\r");
        textcolor(15);
        cprintf("this command kills ALL files that have extension DUP on the current drive\n\r");
        cprintf("(in this case C:) and it erases all files that have no content (0-byte-files)\n\r");
        textcolor(10);
        cprintf("C:\\>OMNIDEL D:\\WORD7 *.WBK *.GID *.SYD\n\r");
        textcolor(15);
        cprintf("this command kills wordbak-files, gid-files and syd-files on drive D: but only\n\r");
        cprintf("in the directory WORD7 and in folders below\n\r");
        cprintf("NEVER TRY THE FOLLOWING!\n\r");
        textcolor(10);
        cprintf("C:\\>OMNIDEL *.EXE *.COM *.DRV *.VXD 9*.TTF WIN*.* /A\n\r");
        textcolor(2);
        cprintf("                                         Copyright 1996 Immortalware JTHZ\n\r");
        textcolor(130);
        cprintf("                                         Julius '?' Thyssen & Hens Zimmermaniac\r");
        textcolor(leavecolor);
        cprintf(" ");
        exit(0);
    }

    if(argv[argc-1][0]=='/'){
        strupr(argv[argc-1]);
        FlagsUsed=1;
        if(strstr(argv[argc-1],"0"))
            Nulfiles=1;
        if(strstr(argv[argc-1],"A"))
            Batch=1;
        if(strstr(argv[argc-1],"P"))
            Askfirst=1;
        if(strstr(argv[argc-1],"Y"))
            leavecolor=14;

    }

    EntryDrive=_getdrive();
    OurDrive=EntryDrive+'A'-1; /* wordt evt. gecorrigeerd hierna!!! */
    getcurdir(EntryDrive,&startpath[1]); /* wordt evt. gecorrigeerd hierna!!! */
    startpath[0]='\\';

    if(argv[1][1]==':'){ /* drive, evt met pad */
        PathSpecified=1;
        OurDrive=toupper(argv[1][0]);
        _chdrive(OurDrive-'A'+1);
        strcpy(startpath,(&argv[1][2]));
    }
    if(argv[1][0]=='\\'){ /* ah, alleen een pad */
        PathSpecified=1;
        strcpy(startpath,(argv[1]));
    }
    if(startpath[strlen(startpath)-1]!='\\')
        strcat(startpath,"\\");

    strupr(startpath);

    if(Askfirst||!Batch){
        textcolor(12);
        cprintf("Omnidel wants to search & destroy files (Murder One will commence)\n\r");
        cprintf("Path %c:%s\n\r",OurDrive,startpath);
        cprintf("Files: ");
        textcolor(11);
        for(i=1+PathSpecified;i<(argc-FlagsUsed)&&i<(9+PathSpecified);i++)
            cprintf("  %s",argv[i]);
        if(Nulfiles)
            cprintf("  0-byte files");
        textcolor(12);
        cprintf("\n\r");
        cprintf("Continue? (y/n)\n\r");
        if(!prompt()){
            textcolor(leavecolor);
            cprintf("Omnidel cancelled...\n\r");
            _chdrive(EntryDrive);
            exit(0);
        }
    }

    for(i=1+PathSpecified;i<(argc-FlagsUsed)&&i<(9+PathSpecified);i++){
        if(!strcmp(argv[i],"*.*")){
            cprintf("*.* -> Will be the end of the world as you know it\n\rREALLY continue? (y/n)\n\r");
            if(!prompt()){
                textcolor(leavecolor);
                cprintf("OMNIDEL cancelled...\n\r");
                _chdrive(EntryDrive);
                exit(0);
            }
        }
    }

    for(i=1+PathSpecified;i<argc-FlagsUsed;i++){
        if(i>(8+PathSpecified))
            break;
        strcpy(searchpattern[i-1-PathSpecified],argv[i]);
    }
    lastpattern=i-PathSpecified;

    tree(startpath);

    textcolor(10);
    cprintf("\n\r%d file",total);
    if(total!=1)
        cprintf("s");
    cprintf(" killed\n\r");

    _chdrive(EntryDrive);
    textcolor(leavecolor);
    cprintf(" ");
}

