#include "SysUser.h"

#include <link/io.h>
#include <link/util.h>

void Purge_Users(void)
{
   long zflag;
   long PurgeVal;
   long a;
   int Level,query;

   PurgeVal=iinput(1,999999,0, SA[2100]);
   AFPrintf(NULL,sout,"\n\n");

   if(!PurgeVal)
      return;

   Level=iinput(1,255,0, SA[2101]);
   AFPrintf(NULL,sout,"\n\n");

   if(!Level)
      return;

   query=finput(1, SA[2102]);
   AFPrintf(NULL,sout,"\n\n");

   AFPrintf(NULL,sout,SA[2103],NumUsers);

   for(a=0;a<NumUsers;a++)
   {
      char filename[80];

      if(ReadChar(0)==3)
      {
         AFPrintf(NULL,sout,SA[2084]);
         break;
      }

      AFPrintf(NULL,sout,SA[2104],(users+a)->name);

      ASPrintf(NULL,filename,"USER:%s/User.Data",(users+a)->name);
      UnderScore(filename);

      if(GetFirstStruct(filename,(char *)&User,sizeof(User))==-1)
      {
         AFPrintf(NULL,sout,SA[2105]);
         continue;
      }

      if(User.Long_Date<((long)AmigaTime()-(86400L*PurgeVal))
                           && User.User_Level<=Level)
      {

         if(query)
         {
            AFPrintf(NULL,sout,"\n\n");
            Print_User((users+a)->name);
            zflag=finput(1, SA[2106]);
            AFPrintf(NULL,sout,"\n\n");
        
            if(zflag)
            {
               Delete_User((users+a)->name,0);
               numdeleted=BuildArray(&deleted,(users+a)->name,36,numdeleted,10);
            }
         }
         else 
         {
            AFPrintf(NULL,sout,"\n");
            Delete_User((users+a)->name,0);
            numdeleted=BuildArray(&deleted,(users+a)->name,36,numdeleted,10);
         }
      }
      else
      {
         AFPrintf(NULL,sout,SA[2107]);
      }
   }

   if(deleted)
   {
      numdeleted=BuildArray(&deleted,NULL,36,numdeleted,10);

      PurgeAll(deleted,numdeleted);

      free(deleted);
      deleted=NULL;
      numdeleted=0;

      if(!(NumUsers=Read_Users()))
      {
         AFPrintf(NULL,sout,SA[2075]);
         _CXBRK();
      }
    
      AFPrintf(NULL,sout,"\n");
   }
  
}

