#include <stdio.h>		/* standard io functions */
#include <string.h>
#include <ctype.h>
#include <unistd.h>		/* for unlink() */
#include <sys/types.h>
#include <dirent.h>		/* Directory information.       */
#include <sys/stat.h>		/* for stat() */

#include "global.h"
#include "custom.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_update_user_info.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_teamstring.h"



void
get_old_new_data(USER_DATA *old_data, USER_DATA *new_data,
	       	int standalone)
{
  char temp[MAX_NAME +1];

  /* username can't be changed - it's always the same for old and new */
  cs_get_required_parameter ("username", old_data->username, MAX_USERNAME);
  strncpy(new_data->username , old_data->username, MAX_USERNAME+1);

  /* get old, new realnames */
  cs_get_required_parameter ("realname", old_data->realname, MAX_NAME);
  cs_get_required_parameter ("new_realname", new_data->realname, MAX_NAME);

  /* group (faculty, student) can't be changed via form */  
  cs_get_required_parameter ("group", temp, MAX_NAME);
  old_data->group = get_group_type(temp);
  new_data->group = old_data->group;


  /* get new, old id number */
  cs_get_required_parameter ("ssn", old_data->id, MAX_ID);
  cs_get_required_parameter ("new_ssn", new_data->id, MAX_ID);
  
  /* get new, old team */  
  cs_get_required_parameter ("team", temp, MAX_NAME);
  old_data->team = *temp;
  cs_get_required_parameter ("new_team", temp, MAX_NAME);
  new_data->team = *temp;

  /* get new, old alias */
  cs_get_required_parameter ("alias", old_data->alias, MAX_NAME);
  cs_get_required_parameter ("new_alias", new_data->alias, MAX_NAME);
  
}




void
read_parameters (char *course, char *key,  USER_DATA * new_data, int *changed,  int *team_changed)
{
  USER_DATA old_data;
  int standalone = 0;
  char scratch[10];
  
  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("is_standalone", scratch, 9);
  standalone = atoi(scratch);

  get_old_new_data(&old_data, new_data,standalone);

  *team_changed = (old_data.team != new_data->team);

 *changed =  strcmp (old_data.realname, new_data->realname) ||
             strcmp (old_data.id, new_data->id) ||
             *team_changed || 
             strcmp (old_data.alias, new_data->alias);

}







static void
report_success (USER_DATA * new_info, char *course, char *key, int team_changed, CONFIG_STRUCT *conf)	
{
  char url[MAX_PATH +1];


  snprintf (url, MAX_PATH +1, "%s?crs=%s&amp;id=%s", "admin_chg_form", course, key);
  cs_set_value("back_url", url);
  cs_set_course_info(conf);
  cs_set_current_time();
  cs_set_value("username", new_info->username);
  cs_set_value("realname", new_info->realname);
  cs_set_value("user_id", new_info->id);
  get_team_name(new_info->team, url);
  cs_set_value("team", url);
  cs_set_value("alias", new_info->alias);
  if(conf->standalone)
  {
    if (team_changed)
       cs_set_int_value("team_chg", 1);
    else
       cs_set_int_value("team_chg", 0);
  }
 cs_set_int_value("success", 1);

}



int
main ()
{

  USER_DATA new_info;		/* data come from form */
  int changed;			/* is old different from new? */
  int team_changed;
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */

  cs_cgi_init();

  read_parameters (course, key,  &new_info, &changed,  &team_changed);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */

  if(!has_write_permission(&user, &conf))   /* shared_access.c */
     access_denied_error();                 /* shared_access.c */


  if ( (user.group != FACULTY) && (user.group != ADMIN))
    cs_critical_error (ERR_REQUEST_DENIED, "");

  if (changed)
    {

      mark_passwd_file (new_info.username, &conf);   /* shared_update_user_info.c */
      update_passwd (&new_info, &conf);              /* shared_update_user_info.c */
      if(!conf.standalone)
           update_course_key(&new_info, course);        /* shared_update_user_info.c */
      report_success (&new_info, course, key, team_changed, &conf);
    }
  else
    cs_critical_error (ERR_NO_CHANGES, "");

   #ifdef HIDE_MANHATTAN_USERNAMES
   cs_set_int_value("hide_manhattan_username", 1);
  #endif
    
  /* Note the cs file is shared with the admin_chg_form.c program */  
  cs_cgi_display("admin_chg_form", 1);
  cs_cgi_destroy();
  return 0;
}

