#include "UserEdit.h"

struct kludge
{
   char name[30];
};


BOOL SelectUser(void)
{
   char                 filename[128];

   long                 nusers;
   long                 l;

   struct   kludge     *userarray   =  NULL;
   struct   NameStruct *ns          =  NULL;

   ULONG                usize       =  0;


   if(-1 == FileSize(NAMERECORD,&usize))                    return(FALSE);
   if((usize == 0) || (usize < sizeof(struct NameStruct)))  return(FALSE);

   nusers = (long)(usize / sizeof(struct NameStruct));

   if(nusers == 0)                           return(FALSE);
   if(NULL == (userarray = calloc(nusers,sizeof(struct kludge))))   return(FALSE);

   if(NULL == (ns = calloc(1,usize)))
   {
      free(userarray);
      return(FALSE);
   }

   if(-1 == GetFirstStruct(NAMERECORD,(char *)ns,usize))
   {
      free(ns);
      free(userarray);
      return(FALSE);
   }

   for(l = 0; l < nusers; l++)
      strcpy(userarray[l].name,ns[l].name);

   free(ns);

   while(TRUE)
   {
      short lev;

      DLGInput("Name of User -> ",(char *)userarray,name,(char *)-1L,30,nusers,6);
      TwoLines();
      if(name[0] == '\0')
      {
         AFPrintf(&UserDat,sout,"%b0%a1Aborted.\n");
         return(FALSE);
      }

      if(UserDat.User_Level < 255)
      {
         lev = GetLevel(name);

         if(lev > UserDat.User_Level)
         {
            AFPrintf(&UserDat,sout,"%b0%a1Cannot edit a user with higher access than yours!%a7%b0\n\n");
            continue;
         }
         else
         {
            break;
         }
      }
      else
      {
         break;
      }
   }


   free(userarray);

   if(1 != CheckUser(name))  return(FALSE);

   ASPrintf(NULL,filename,"User:%s/user.data",name);
   UnderScore(filename);

   if(-1 == GetFirstStruct(filename,(char *)&udat,sizeof(struct USER_DATA)))
      return(FALSE);

   if(!Stricmp(name,RStruct.Name))
      Same = TRUE;
   else
      Same = FALSE;

   return(TRUE);
}

