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

#include "global.h"

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


void
read_parameters(char *username, char *passwd)
{
	char verify[MAX_PASSWORD + 1];

	cs_get_required_parameter("username", username, MAX_USERNAME );
	check_str(username,  ERR_INVALID_USERNAME);   /* shared_super_util.c */

	cs_get_required_parameter("passwd", passwd, MAX_PASSWORD );
	check_str(passwd, ERR_INVALID_PASSWORD);        /* shared_super_util.c */

	cs_get_required_parameter("passwd2", verify, MAX_PASSWORD);
        if(strcmp(passwd, verify))
           cs_critical_error(ERR_PASSWORDS_DONT_MATCH, "");
        
        if( (strlen(passwd) < 6)   ||  (strlen(passwd) > 15 ) )
               cs_critical_error(ERR_PASSWORD_WRONG_LEN, "");


}






void
set_success(ADMIN_CONFIG_STRUCT *conf,  char *username)
{

   super_conf_info_table(conf, username, conf->page_title);  /* shared_super_util.c */
    
    
}








void
get_default_super_conf(ADMIN_CONFIG_STRUCT *conf)
{
  ADMIN_CONFIG_STRUCT new_conf = {"",
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1,
           1};
           
  strncpy(new_conf.page_title, 
                         hdf_get_value(global_cgi->hdf, "lang.super_menu.super_user", "Super User"),
                                                                                      MAX_ADMIN_PAGE_TITLE + 1);
  *conf = new_conf;
  
}
  
  



void
create_super_user( char *username,  char *passwd)
{
    ADMIN_CONFIG_STRUCT conf;
    
    get_default_super_conf(&conf);
    create_admin_user_dir(username, passwd,&conf);   /* shared_encrypt.c */
    
    write_to_index(username, conf.page_title);  /* shared_super_util.c" */ 
    set_success(&conf, username);
}





void
check_no_admins( )
{
    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, "");
}






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

    check_no_admins();
    

    read_parameters(username, passwd);  

    cs_load_lang("super_menu");
    create_super_user( username, passwd);
    
    cs_set_int_value("super_action", 1);
    cs_cgi_display("super_add_admin_form", 1);
    cs_cgi_destroy();
    return 0;
}

       
    
	
