/***********************************************************/
/*
**   $VER: PurgeDelDir v1.4 by Giles Burdett
**
**   PurgeDelDir - Purge the .DelDir of a PFS formatted
**   partition of all its entries.
**
**   See the ReadMe for usage information
**
**   PurgeDelDir has been designed and programmed
**   by Giles Burdett in 2000
**     - layabouts@the-giant-sofa.demon.co.uk
**
**   PFS is the Professional File System by
**   GREat Effects Development
**     - www.greed.nl
**
*/
/***********************************************************/




#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <clib/dos_protos.h>


UBYTE version_info[]="$VER: PurgeDelDir v1.4 by Giles Burdett";

//#define PDD_DEBUG       // Uncomment to debug


#define DEFAULT_SLOT_NUMBER 32
#define MAX_SLOT_NUMBER 992
#define MAX_DIGITS 4

int main (int argc, char **argv)
{
    struct RDArgs *cmdline_args;
    LONG arguments[]={0, 0};
    BPTR fp;
    // Default slot value in case user doesn't specify one
    int slot=DEFAULT_SLOT_NUMBER;
    LONG i;
    unsigned char target[256];
    char env_slot_value_string[MAX_DIGITS];
    LONG user_slot=0;
    BOOL user_slot_entered=FALSE;

    if ((cmdline_args=ReadArgs("Device/A,Slots/N", arguments, NULL))==NULL)
    {
        puts ("Error: Missing device/volume name");
        return RETURN_FAIL;
    }

    // Store arguments
    strcpy (target, (STRPTR)arguments[0]);
    if (arguments[1])
    {
        user_slot=*((LONG *)(arguments[1]));
        user_slot_entered=TRUE;
    }

    FreeArgs(cmdline_args);

    #ifdef PDD_DEBUG
        printf("you typed: %s\n", target);
    #endif
    strcat (target, "deldirpurgefile");
    #ifdef PDD_DEBUG
        printf("The full path: %s\n");
    #endif

    {
        int len;
        if ((len=GetVar("PurgeDelDir/slot_value", env_slot_value_string, MAX_DIGITS, GVF_GLOBAL_ONLY))>0)
        {
            // Reset to default if env var is a non-number string
            if (!(slot=abs(atoi(env_slot_value_string))))
                slot=DEFAULT_SLOT_NUMBER;
            #ifdef PDD_DEBUG
                printf ("env slots: %d\n", slot);
            #endif
        }
    }
    #ifdef PDD_DEBUG
        printf ("arg[1]=%ld\n", arguments[1]);
    #endif

    // Check if user wants to override default or env variable
    if (user_slot_entered)
    {
        if (user_slot<1 || user_slot>MAX_SLOT_NUMBER)
        {
            puts ("Bad slot number - aborting DelDir flush...");
            return RETURN_FAIL;
        }
        else
            slot=user_slot;
        #ifdef PDD_DEBUG
            printf ("slots: %d\n", slot);
        #endif
    }

    #ifdef PDD_DEBUG
        printf ("Final slot value = %d\n", slot);
    #endif

    for (i=0; i<slot; i++)
    {
         // Need the extra brackets around the assignment otherwise
         // the compiler gets horribly confused!
         if (!(fp=Open (target, MODE_NEWFILE)))
         {
             PrintFault (IoErr(), "Failure");
             return RETURN_FAIL;
         }
         Close (fp);
         DeleteFile (target);
    }
    
    return RETURN_OK;
}

