#include <stdio.h>
#include <string.h>
#include <dirent.h>		/* Directory information. */
#include <sys/stat.h>		/* for stat()            */
#include <unistd.h>		/* for unlink, mktmp ... */

#include "global.h"
#include "custom.h"		/* for date FORMAT strings */

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



static void
set_form(  const char *key, ADMIN_CONFIG_STRUCT *current_conf,  char *username,
                                     char *realname)
{

  cs_set_value("username", username); 
  cs_set_value("realname", realname); 
  
  list_course_commands(current_conf);   /* shared_super_util.c */
  list_people_commands(current_conf);   
  list_server_commands(current_conf);
  cs_set_value("page_title", current_conf->page_title); 
  cs_set_int_value("grant_display", 1); 


}





void
read_parameters( char *key, char *del_name)
{
    cs_get_required_parameter("id", key,  MAX_KEY);
    cs_get_required_parameter("list_user", del_name, MAX_USERNAME + MAX_NAME +1);
}



void
do_action( const char *key,   char *del_name)
{
    char *realname;
    ADMIN_CONFIG_STRUCT old_conf;


    /** parse del_name, passed to this program
    *** format is 'username:realname'
    **/
    realname = strchr(del_name, ':');
    if(!realname)
        cs_critical_error(ERR_PASS_PARAMETERS_ERROR_MSG, "");
    *realname = '\0';
    realname++;
    
    if(!strlen(del_name))
        cs_critical_error(ERR_PARAM_MISSING, "user_list");

    /* del_name now contains the username
    ** realname points the the realname
    */
    

    if(!read_super_configuration_file(del_name, &old_conf))   /* shared_super_util.c */
        cs_critical_error(ERR_FREAD_FAILED, "");
        
     set_form(key,  &old_conf, del_name, realname);
}

    
int main()
{
     char key[MAX_KEY +1];
     char del_name[MAX_USERNAME + MAX_NAME +2];
     SESSION user;
    
     cs_cgi_init(); 
     read_parameters( key, del_name);
     validate_super_key(key, &user);
     set_admin_page(&user, key, 0, "super_grant_admin_form");  /* shared_super_menu.c */
     
     
    do_action( key, del_name);
    cs_cgi_display("super_add_admin_form", 1);
    cs_cgi_destroy();
    return 0;
}
