#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>

#include "global.h"


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_encrypt.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_menu.h"

void
read_parameters(char *username, char *first, char *last, char *passwd, 
                                       char *passwd2, ADMIN_CONFIG_STRUCT *conf, char *key)
{
   cs_get_required_parameter("id", key, MAX_KEY);
   
   cs_get_required_parameter("username", username, MAX_USERNAME);
   
   cs_get_required_parameter("passwd", passwd, MAX_PASSWORD);   
   
   cs_get_required_parameter("passwd2", passwd2, MAX_PASSWORD);      


   cs_get_required_parameter("first_name", first, MAX_NAME);      

   cs_get_required_parameter("last_name", last, MAX_NAME);      
   
   get_cgi_admin_config_values(conf);   /* shared_super_util.c */
   /* page_title is the person's real name */
   snprintf(conf->page_title, MAX_ADMIN_PAGE_TITLE + 1, "%s %s", first, last);   
   
}



int
admin_username_exists(const char *username)
{
     FILE *fp;
     char line[200];
     char fname[MAX_PATH + 1];
     char *ptr;
     
      snprintf(fname, MAX_PATH +1, "../%s/%s/%s", USERS_DIR,  ADMIN_DIR,  USER_INDEX_FNAME);

     fp = fopen(fname, "r");
     if(!fp)
	cs_critical_error(ERR_FOPEN_READ_FAILED, "");
     get_shared_lock(fileno(fp), 1);
     while(fgets(line, 200, fp))
     {
       strtrm(line);
       ptr = strstr(line, username);
       if(ptr && (ptr==line))
       {
         release_lock(fileno(fp));
	 fclose(fp);
	 return 1;
       }
     }
     release_lock(fileno(fp));
     fclose(fp);
     return 0;
}





void
validate_parameters(char *username, char *first,  char *last, 
                            char *passwd,  char *passwd2, char *page_title)
{
    if(strlen(username) <= 4)
	  cs_critical_error( ERR_USERNAME_LEN, username);

    check_str(username, ERR_INVALID_USERNAME);   /* shared_super_util.c */
    if(admin_username_exists(username))
	  cs_critical_error( ERR_EXISTING_USER_MSG, "");
          
    if(strcmp(passwd, passwd2))
          cs_critical_error( ERR_PASSWORDS_DONT_MATCH, "");
        	  
    check_str(passwd, ERR_INVALID_PASSWORD);   /* shared_super_util.c */
    if( (strlen(passwd) < 6) || (strlen(passwd) > 15))
        cs_critical_error( ERR_PASSWORD_WRONG_LEN, "");
       
    check_str(first, ERR_FIRST_NAME_INVALID);
    check_str(last, ERR_LAST_NAME_INVALID);
    
    if(!strlen(page_title))
        cs_critical_error(ERR_PAGE_TITLE_BLANK, "");
} 


void
add_administrator(char *username, char *first, char *last, char *passwd,
                       char *passwd2, ADMIN_CONFIG_STRUCT *conf, const char *key)
{
 char realname[MAX_NAME + 1];
 
    validate_parameters(username, first, last, passwd, passwd2, conf->page_title);
    
    create_admin_user_dir(username, passwd, conf); /* shared_encrypt.c */

    snprintf(realname, MAX_NAME + 1, "%s %s", first, last);
    *(realname + MAX_NAME) = '\0';
    
    write_to_index(username, realname);  /* shared_super_util.c" */

    super_conf_info_table(conf, username, realname);  /* shared_super_util.c */
}





int
main()
{
    char key[MAX_KEY +1];
    char username[MAX_USERNAME + 1];
    char first[MAX_NAME + 1];
    char last[MAX_NAME + 1];
    char passwd[MAX_PASSWORD + 1];
    char passwd2[MAX_PASSWORD + 1];

    SESSION user;
    ADMIN_CONFIG_STRUCT  conf;
    
   
    cs_cgi_init(); 
    

    read_parameters(username, first, last, passwd, passwd2, &conf, key);
    validate_super_key(key, &user);    

    set_admin_page(&user, key, 0, "super_add_admin_form");  /* shared_super_menu.c */
    
    add_administrator(username, first, last, passwd, passwd2, &conf,  key);

    cs_set_int_value("display_add_success", 1);
    cs_cgi_display("super_add_admin_form", 1);
    cs_cgi_destroy();
    return 0;
}
