#include <stdio.h>		/* standard io functions */
#include <string.h>
#include <sys/types.h>
#include <ctype.h>

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_add_util.h"
#include "manhat-lib/shared_add_student.h"
#include "manhat-lib/shared_encrypt.h"
#include "manhat-lib/shared_teamstring.h"


void
read_parameters (char *course, char *key,
		 char *first, char *last, char *student_id, char *group, char *fac_or_student)
{

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("group", fac_or_student, MAX_ID);
  if(strcmp(fac_or_student, "student") ==0)
     cs_set_int_value("group", 0);
  else
     cs_set_int_value("group", 1);
  cs_get_required_parameter ("first", first, MAX_NAME/2);
  cs_get_required_parameter ("last", last, MAX_NAME/2);
  cs_get_required_parameter ("user_id", student_id, MAX_ID);
  if (strlen (student_id) < 4)
    cs_critical_error (ERR_ID_TOO_SHORT, "");
  cs_get_required_parameter ("team", group, 1); 
}








static void
show_success ( char *username,  char *plaintext_password,  char *realname,
	       char *group,  char *id)
{
   char temp[10];

   cs_set_value("username", username);  
   cs_set_value("realname", realname);  
   cs_set_value("userid", id); 
   get_team_name(*group, temp);
   cs_set_value("team", temp);  
   cs_set_value("password", plaintext_password);  
   cs_set_int_value("add_success", 1);  


}










int
main ()
{

  char username[MAX_USERNAME + 1];
  char plaintext_password[MAX_PASSWORD + 1];
  char realname[MAX_NAME + 1];

  char first[MAX_NAME/2 + 1];
  char last[MAX_NAME/2 + 1];
  char group[10];
  char id[MAX_ID + 1];
  char fac_or_student[MAX_ID + 1];

  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 */
  char passwd_fname[MAX_PATH + 1];	/* path to the password file */
  char url[MAX_PATH +1];

  cs_cgi_init();
  read_parameters (course, key, first, last, id, group,fac_or_student);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */
  snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s", "admin_main_menu", key, course);
  cs_set_value("back_url", url);
  cs_set_course_info(&conf);
  cs_set_current_time();
  if ( ((user.group != FACULTY) && (user.group != ADMIN)) ||  (!conf.standalone) )
       cs_critical_error (ERR_REQUEST_DENIED, "");

  snprintf (passwd_fname, MAX_PATH + 1, "%s%s", conf.course_path,PASSWORD_FNAME);
  check_realname (first, last, realname, passwd_fname); /*shared_add_student.c */
  get_standalone_username (first, last, username, id, &conf); /*shared_add_student.c */
  check_id (id, passwd_fname);  /* shared_add_util.c */

  /* shared_authenticate.c */
  derive_password (username, first, last, id, plaintext_password,MAX_PASSWORD);

  /* don't test for errors on next, since directories might already exist */
  create_directories (conf.course_path, username);

  
  if(!add_to_passwd (username, realname, *group, id, passwd_fname, fac_or_student)) /* shared_add_util.c */
      cs_critical_error(ERR_ADDING_TO_PASSWORD, ""); 		

  if(! write_password(username, plaintext_password, conf.course_path)) /* shared_encrypt.c */
      cs_critical_error(ERR_ADDING_TO_PASSWORD, "");
      
  show_success (username, plaintext_password, realname, group, id);
  cs_cgi_display("admin_add_student_form", 1);
  cs_cgi_destroy();
  return 0;

}
