/* ---- DRev header ---- DO NOT EDIT! Please ---
**
**    Program           :   DelInfo
**    Copyright         :   © 1993 by Paolo Dell'Aquila
**    Author            :   Paolo Dell'Aquila
**    Creation Date     :   12.11.92
**    Current version   :   1.1
**    Compiled with     :   SAS 6.0
**
**    REVISION HISTORY
**
**    Date       Ver     Author                Comment
**    ---------  -----   -------------------   --------------------------
**    31.03.93    1.1    Paolo Dell'Aquila      Now use AllocDosObject and needs
**                                              no more RETURN after 'y' or 'n'.
**    31.03.93    1.0    Paolo Dell'Aquila      Born in the CPU (I was)
**
*/

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/exall.h>
#include <clib/dos_protos.h>
#include "proto/exec.h"
#include "string.h"

#include "proto/dos.h"

#define VERSION 1
#define REVISION 1
#define DATE "31.03.93"
#define VERS "DelInfo 1.1"
#define VSTRING "DelInfo 1.1 (31.03.93)\n\r"
char versiontag[]="$VER: DelInfo 1.1 (31.03.93)";

#define		TEMPLATE "DIR,I=INTER/S,S=SUBDIR/S"
#define		DIR			0
#define		INTER		1
#define		SUBDIR		2
#define		OPT_COUNT	3

LONG opts[OPT_COUNT];		/* C guarantees this will be all 0's! */

BOOL		SubDir=FALSE;
BOOL		Inter=FALSE;

#define BUFFSIZE 1024

extern int printf(const char *, ...);
extern int rawcon(int);

void DelInfo		(register TEXT *);

struct DosLibrary *DOSBase;


 long stdout;
 long stdin;
__asm __saveds  main(register __d0 int len,register __a0 char *arg)
{
	TEXT dir[255];
	struct RDArgs 	*argsptr;
	char 			**sptr;

    if (!(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",0)))
        return -1;
    stdout=Output();
	stdin=Input();
	argsptr=ReadArgs(TEMPLATE, opts, NULL);
	if (argsptr == NULL)
	{
		PrintFault(IoErr(), NULL);	/* prints the appropriate err message */
	}
	else
	{
		GetCurrentDirName(dir,255);
		sptr=(char **)(opts[DIR]);
		if(sptr)
		{
			AddPart(dir,(STRPTR)(LONG *)sptr,255);
		}
		if(opts[SUBDIR])	SubDir=TRUE;
		if(opts[INTER])		Inter=TRUE;
		FreeArgs(argsptr);
		printf("\n[1m%s[0m - Copyright © 1993 by [3mPaolino Dell'Aquila[0m (Fido 2:332/505.10)\n\n",VERS);
		DelInfo(dir);
//		printf("\n");
	}
	return 5;
}

void DelInfo(register TEXT *dir)
{

	BPTR obj_lock;
	LONG res2,more;
	struct ExAllData *Buffer,*ead;
	struct ExAllControl *control;
	LONG rc = RETURN_ERROR;

//	TEXT							buf[256];
	STRPTR 							s;
	ULONG 							temp;
	char							c;
	TEXT							file[256];
	TEXT							buf2[ 2 ];

	SHORT	len=0;

	control=(struct ExAllControl *) AllocDosObject(DOS_EXALLCONTROL,NULL);
	Buffer=(struct ExAllData *) AllocMem(BUFFSIZE,MEMF_PUBLIC|MEMF_CLEAR);

	if (!control || !Buffer)	goto cleanup; 	// always check allocations!

	/* lock the directory */
	if (obj_lock = Lock(dir,SHARED_LOCK))
	{
		control->eac_LastKey = 0;	/* paranoia */

		do 	/* while more */
		{
			more = ExAll(obj_lock,Buffer,BUFFSIZE,ED_TYPE,control);
			res2 = IoErr();
			if (!more && res2 != ERROR_NO_MORE_ENTRIES)
			{
				printf("Abnormal exit, error = %ld\n",res2);
				break;
			}

			if (control->eac_Entries)
			{
				ead = Buffer;
				do 
				{
					stccpy(file,dir,255);
					temp=SetSignal(0,0);
					if (temp & SIGBREAKF_CTRL_C) goto cleanup;

					if(ead->ed_Type >0)		// e' una dir
					{
						if(SubDir)
						{
							stccpy(file,dir,255);
							AddPart(file,ead->ed_Name,255);
							DelInfo(file);
						}

					}
					else
					{
						AddPart(file,ead->ed_Name,256);
						if((len=strlen(ead->ed_Name)) >5)
						{
							s=&(ead->ed_Name[strlen(ead->ed_Name)-5]);
							if(!(strnicmp(".info",s,5)))
							{
								if(Inter)
								{
									printf("  ---> Delete %s [y/N] ?  ",file);
									Flush(stdout);
									rawcon(1);
									c=FGetC(stdin);
									rawcon(0);
									if(c=='y')
									{
										if(!DeleteFile(file))
											printf("  *** Problem deleting %s\n",file);
									}

									buf2[ 0 ] = c;
									buf2[1]='\0';
									printf("%s\n",buf2);

								}
								else
								{
									if(DeleteFile(file))
										printf("  ---> Delete %s\n",file);
									else printf("  *** Problem deleting %s\n",file);
								}
							}
						}
					}	
					ead = ead->ed_Next;
				} while (ead);
			}

			rc = RETURN_OK;	/* success */

		} while (more);

		UnLock(obj_lock);

	} 
	else printf("Couldn't find %s\n",dir);


cleanup:
	if (Buffer)  FreeMem(Buffer,BUFFSIZE);
	if (control) FreeDosObject(DOS_EXALLCONTROL,control);

}







