#include <stdio.h>
#include <string.h>
#include <time.h>

#include "global.h"

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

static
void  read_parameters(char *key, char *username, char *id_number, char *realname)
{
     char temp_realname[MAX_NAME + 1];
     

     cs_get_required_parameter ("id", key, MAX_KEY);
     cs_get_required_parameter ("username", username, MAX_USERNAME);
     cs_get_required_parameter ("id_number", id_number, MAX_ID);
     cs_get_required_parameter ("realname", temp_realname, MAX_NAME);
     unescape_realname(realname, temp_realname);    /* shared_escape_realname.c */

}



static
void validate_input (const char *supplied_username, const char *id_number, const char *supplied_realname)
{

 char *buffer;
 char actual_username[MAX_USERNAME + 1];
 char actual_realname[MAX_NAME + 1];
 
 buffer_index(&buffer);    /* read entire index into buffer - shared_user_index.c */
 
 index_get_names(buffer,id_number, actual_username, actual_realname); /* shared_user_index.c */

 if(buffer)
    free(buffer);
    
 if(strcmp(supplied_username, actual_username) || strcmp(supplied_realname, actual_realname) )
     cs_critical_error(ERR_SUPPLIED_DONT_MATCH_ACTUAL,"");
        
}








static void
set_data (const char *key,  char *username,  char *id_number,  char *realname)
{
    cs_set_value("id_number", id_number);
    cs_set_value("username", username);
    cs_set_value("realname", realname);
    cs_set_int_value("maxlength", MAX_NAME);
    cs_set_current_time();
}







int
main()
{
  /** from the command line */
  char key[MAX_KEY +1];
  char username[MAX_USERNAME +1];
  char id_number[MAX_ID +1];
  char realname[MAX_NAME +1];
  
  SESSION user;
  
  cs_cgi_init();
  read_parameters(key,username, id_number, realname);
                       
  validate_super_key(key, &user);
  validate_input(username, id_number, realname);
  set_data (key, username, id_number, realname);
  cs_cgi_display("super_modify_profile_form", 1);
  cs_cgi_destroy(); 
  return 0;
}
