#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <dos/dosextens.h>

#include <proto/exec_protos.h>
#include <proto/dos_protos.h>
#include <clib/alib_protos.h>

#include <stdlib.h>

#ifndef __COMMODORE_DATE__
#define __COMMODORE_DATE__ __DATE__
#endif

#define BASENAME "DelOld"

const STATIC char VER[] = "$VER: " BASENAME " 1.0 (" __COMMODORE_DATE__ ")";
const STATIC char Basename[] = BASENAME;

#define MAXPATHLEN 512
#define POODLESIZE (1024*10)

STATIC VOID ClearMem(VOID *mem,ULONG size)
{
UBYTE *ptr = mem;

   while (size--)
      *ptr++ = 0;
}

STATIC ULONG Strlen(STRPTR s)
{
STRPTR b = s;

   while (*s) ++s;

   return (s-b);
}

__stkargs void _main(int unused_arglen,char *unused_argptr)
{
int ret = RETURN_FAIL;

   if ((DOSBase->lib_Version>=37) && (SysBase->lib_Version >= 37))
      {
      LONG array[] = { NULL, NULL, FALSE, FALSE };
      struct RDArgs *rdargs = ReadArgs("PATTERN/M/A,DAYS/N/K,QUIET/S,ALL/S", array, NULL);
      if (rdargs)
	 {
	 struct DateStamp now = { 0,0,0 };
	 LONG days = 7;

	 if (array[1])
	    days = *(LONG *)array[1];

	 DateStamp(&now);
	 if (now.ds_Days >= days)
	    now.ds_Days -= days;
	 else
	    now.ds_Days = 0;

	 struct List delfiles;
	 NewList(&delfiles);

	 VOID *pool;

	 if (pool = LibCreatePool(MEMF_ANY, POODLESIZE, POODLESIZE))
	    {
	    struct AnchorPath *anchorpath = LibAllocPooled(pool,sizeof(struct AnchorPath)+MAXPATHLEN);
	    if (anchorpath)
	       {
	       ClearMem(anchorpath,sizeof(struct AnchorPath)+MAXPATHLEN);
	       anchorpath->ap_BreakBits = SIGBREAKF_CTRL_C;
	       anchorpath->ap_Strlen   = MAXPATHLEN;

	       STRPTR *multi = (STRPTR *)array[0];
	       ret = RETURN_OK;

	       while (*multi) /* parse each pattern */
		  {
		  LONG mferr = MatchFirst(*multi,anchorpath);
		  while (mferr == 0)
		     {
		     if (anchorpath->ap_Flags &  APF_DIDDIR)
			anchorpath->ap_Flags &= ~(APF_DIDDIR | APF_DODIR);
		     else
			{
			if (anchorpath->ap_Info.fib_DirEntryType > 0) /* directory */
			   {
			   if (array[3])
			      anchorpath->ap_Flags |= APF_DODIR;
			   }
			else if (anchorpath->ap_Info.fib_DirEntryType < 0) /* file */
			   {
			   if (CompareDates(&now,&anchorpath->ap_Info.fib_Date)<=0)
			      {
			      if (!FindName(&delfiles,anchorpath->ap_Buf)) /* no doubles */
				 {
				 struct Node *node = LibAllocPooled(pool,sizeof(struct Node));
				 if (node)
				    {
				    ULONG size = Strlen(anchorpath->ap_Buf)+1;
				    if (node->ln_Name = LibAllocPooled(pool,size))
				       CopyMem(anchorpath->ap_Buf,node->ln_Name,size);
				    else
				       {
				       PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
				       break;
				       };
				    AddTail(&delfiles,node);
				    }
				 else
				    {
				    PrintFault(ret = ERROR_NO_FREE_STORE,Basename);
				    break;
				    };
				 };
			      };
			   };
			};
		     mferr = MatchNext(anchorpath);
		     };
		  multi++;
		  };

	       for (struct Node *node = delfiles.lh_Head; node->ln_Succ; node = node->ln_Succ)
		  {
		  char *name = node->ln_Name;
		  if (name)
		     {
		     if (array[2])
			DeleteFile(name);
		     else
			{
			PutStr(name);
			if (DeleteFile(name))
			   PutStr(" Deleted\n");
			else
			   PrintFault(ret = IoErr()," Not Deleted");
			};
		     };
		  };
	       }
	    else
	       PrintFault(ret = ERROR_NO_FREE_STORE,Basename);

	    LibDeletePool(pool);
	    }
	 else
	    PrintFault(ret = ERROR_NO_FREE_STORE,Basename);

	 FreeArgs(rdargs);
	 }
      else
	 PrintFault(ret = IoErr(),Basename);
      }
   else
      {
      if (DOSBase->lib_Version >= LIBRARY_MINIMUM)
	 {
	 if (Output())
	    {
	    Write(Output(),Basename,sizeof(Basename)-1);
	    Write(Output(),": Can't open dos.library v37 (OS 2.04)\n",39);
	    };
	 ret = ERROR_INVALID_RESIDENT_LIBRARY;
	 };
      };

   _exit(ret);
}
