#include <stdio.h>
#include <string.h>
#include <time.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_user_index.h"
#include "manhat-lib/shared_strtrm.c"
#include "manhat-lib/shared_cs_util.h"


static
void  read_parameters(char *key, char *username, char *id_number, char *realname)
{

     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", realname, MAX_NAME);
     strtrm(realname);    /* shared_strtrm.c */

}





void
modify_profile(const char *username, const char *id_number, const char *realname)
{
     char path[MAX_PATH +1];
     FILE *fp;

     /* sub_dir_name() is in shared_authenticate.c */
     snprintf(path, MAX_PATH+1, "../%s/%c/%s/id", USERS_DIR, sub_dir_name(username), username);

     fp = fopen(path, "w");
     if(!fp)
	cs_critical_error(ERR_FOPEN_WRITE_FAILED, path);
     fprintf(fp, "%s\n", id_number);
     fprintf(fp, "%s\n", realname);
     fclose(fp);
     snprintf(path, MAX_PATH + 1, "../%s", USERS_DIR);
     reindex_users(path);   /* shared_user_index.c */
}


static
void  set_results(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("show_result", 1);
}



/* quick check to see if the 'name' is reasonable' 
** This function is similar to one  in super_new_course.c
** should probably put it in a library.
*/
static void
check_name_format(char *realname)
{
 char banned_characters[] = ":/\\;,?";
 char *ptr;
 
 if(strlen(realname) > MAX_NAME)
  {
   cs_critical_error(ERR_PARAM_LONG, realname);
  }
  
 ptr = strchr(realname, ' ');
 if(!ptr)
    cs_critical_error(ERR_REALNAME_MUST_HAVE_SPACE, ""); 
  
 for(ptr=realname; *ptr; ptr++)
  if(strchr(banned_characters,*ptr))
   {
     cs_critical_error(ERR_NAME_HAS_BANNED_CHARS, banned_characters);
  }
  

   
}





static
void validate_input (char *supplied_username, char *id_number, 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) )
     cs_critical_error(ERR_SUPPLIED_DONT_MATCH_ACTUAL,"");
        
 if(!strcmp(supplied_realname, actual_realname))
     cs_critical_error(ERR_YOU_DIDNT_CHANGE_REALNAME, "");
     
     
 check_name_format(supplied_realname);     
}



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);
  modify_profile(username, id_number, realname);
  set_results(key,username, id_number, realname);
  cs_cgi_display("super_modify_profile_form", 1);
  cs_cgi_destroy();
  return 0;
}
