#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
               
#include <string.h>

#include <dirent.h>
        


#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_cs_util.h"

static 
void set_data()
{

   cs_set_int_value("max_username", MAX_USERNAME);
   cs_set_int_value("max_passwd", MAX_PASSWORD);
}




void
delete_existing_admins()
{
  DIR *dir_p;
  struct dirent *dir_entry_p;
  char dirname[MAX_PATH + 1];
  char del_path[MAX_PATH + 1];
  
  snprintf(dirname, MAX_PATH +1, "../%s/%s", USERS_DIR, ADMIN_DIR);
  dir_p = opendir (dirname);
  if (!dir_p)
     cs_critical_error(ERR_OPENDIR_FAILED, "");
  
   while (NULL != (dir_entry_p = readdir (dir_p)))
    {
      if (strcmp (dir_entry_p->d_name, ".")
	  && strcmp (dir_entry_p->d_name, ".."))
	{
          if(strlen(dir_entry_p->d_name) == 1)  /* delete all directories that have a single letter */
            {
  	     snprintf (del_path, MAX_PATH + 1, "%s/%s", dirname, dir_entry_p->d_name);
             kill_directory(del_path);   /*shared_file_util.c */
            }
        }
    }
    
            
 closedir(dir_p);     
}





static
void show_super_user_form(char *username, char *passwd)
{
    char index_fname[MAX_PATH + 1];
    
    snprintf(index_fname, MAX_PATH +1, "../%s/%s/%s", USERS_DIR, ADMIN_DIR, USER_INDEX_FNAME);
    if(file_exists(index_fname))                    /* shared_file_util.c */
       cs_critical_error(ERR_SUPER_USER_EXISTING, "");
    else
     {
      delete_existing_admins();   
      set_data();
      cs_cgi_display("super_conf", 1);
     } 
}






int
main()
{
    char username[MAX_USERNAME +1];
    char passwd[MAX_PASSWORD +1];
    
    cs_cgi_init();

    show_super_user_form(username, passwd);
    cs_cgi_destroy();
    return 0;
}

       
    
	
