/***********************************************************/
/*
**   $VER: PurgeDelDir v1.3 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.3 by Giles Burdett";

//#define PDD_DEBUG       // Uncomment to debug


#define DEFAULT_SLOT_NUMBER 32


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[3];
    LONG user_slot;

    if ((cmdline_args=ReadArgs("Device/A,Slots/N", arguments, NULL))==NULL)
    {
        printf ("You must specify a device/volume name\n");
        return 21;
    }

    strcpy (target, (STRPTR)arguments[0]);
    #ifdef PDD_DEBUG
        printf("you typed: %s\n", target);
    #endif
    strcat (target, "deldirpurgefile");
    #ifdef PDD_DEBUG
        printf("The full path: %s\n");
    #endif

    // Check for env variable, and override default if present
    if (fp=Open("Env:PurgeDelDir/slot_value", MODE_OLDFILE))
    {
        char i=0;
        while (i<3)
        {
            if ((env_slot_value_string[i]=FGetC(fp))==-1)
            {
                env_slot_value_string[i]=0;
                break;
            }
            if (i==2)
                env_slot_value_string[i]=0;

            i++;
        }

        // 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

        Close(fp);
    }

    #ifdef PDD_DEBUG
        printf ("arg[1]=%ld\n", arguments[1]);
    #endif

    // Check if user wants to override default or env variable
    if (arguments[1])
    {
        user_slot=*((LONG *)(arguments[1]));
        
        if (user_slot<1 || user_slot>99)
        {
            printf ("Bad slot number - aborting DelDir flush...\n");
            FreeArgs(cmdline_args);
            return 21;
        }
        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))==0)
         {
             PrintFault (IoErr(), "Failure");
             return 21;
         }

         Close (fp);

         DeleteFile (target);
    }


    FreeArgs(cmdline_args);
    return 0;
}

