#include "SysUser.h"

#include <link/io.h>

int HandleBuiltIn(UBYTE cmd)
{
   char  name[36];

   switch(cmd)
   {
///   Abort
      case 0:
               exitflag = TRUE;
               break;
//-

///   Add User
      case 1:
               AFPrintf(NULL,sout,SA[2076]);

               if (!Add_User())  AFPrintf(NULL,sout,SA[2077]);

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

               break;
//-

///   Add Template
      case 2:
               AddTemplate();
               break;
//-

///   Delete User
      case 3:
               if (Get_UserName(name) == -1)  break;

               AFPrintf(NULL,sout,SA[2078],name);

               if (finput(1, SA[2079]))
               {
                  char   tu[128];
                  struct USER_DATA u;

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

                  if(0 != GetFirstStruct(tu,(char *)&u,sizeof(struct USER_DATA)))
                  {
                     AFPrintf(NULL,sout,SA[2080]);
                     break;
                  }

                  if(u.User_Level > UserDat.User_Level)
                  {
                     AFPrintf(NULL,sout,SA[2080]);
                     break;
                  }

                  AFPrintf(NULL,sout,"\n\n");
                  Delete_User(name,1);

                  if (!(NumUsers=Read_Users()))
                  {
                     AFPrintf(NULL,sout,SA[2075]);
                     _CXBRK();
                  }
               }
               else
               {
                  AFPrintf(NULL,sout,SA[2080]);
               }
               break;
//-

///   Delete Template
      case 4:
               DeleteTemplate();
               break;
//-

///   Edit User
      case 5:
            {
               char   tu[128];
               struct USER_DATA u;

               if (Get_UserName(name) == -1)  break;

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

               if(0 != GetFirstStruct(tu,(char *)&u,sizeof(struct USER_DATA)))
               {
                  AFPrintf(NULL,sout,SA[2080]);
                  break;
               }

               if(u.User_Level > UserDat.User_Level)
               {
                  AFPrintf(NULL,sout,SA[2080]);
                  break;
               }

               Edit_User(name);
               break;
            }
//-

      case 6:        /* SYSU_EditTmplt */
               EditTemplate();
               break;

      case 7:        /* SYSU_Exit */  
               _CXBRK();
               break;

      case 8:        /* SYSU_GlobalTmplt */
               GlobalTemplate();
               break;

      case 9:        /* SYSU_List */
               Print_Users();
               break;

      case 10:       /* SYSU_Purge */
               Purge_Users();
               break;

      case 11:       /* SYSU_Validate */
               Validate_Users();
               if (!(NumUsers=Read_Users()))
               {
                  AFPrintf(NULL,sout,SA[2075]);
                  _CXBRK();
               }
               break;
   }

   return(TRUE);
}
