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

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_teamstring.h"


static void
read_parameters (char *course, char *key, char *group)
{

  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("group", group, MAX_ID);
  cs_set_value("crs", course);
  cs_set_value("id", key);
  cs_set_value("group", group);
}


static void
print_standalone_form(const char *course, const char *key, const char *group)
{
  cs_set_team();   /* shared_cs_util.c */
  if(strcmp(group, "student") ==0)
  {
     cs_set_int_value("group", 0);
  }
  else
  {
     cs_set_int_value("group", 1);
  }
  cs_set_int_value("max_name", MAX_NAME/2);
  cs_set_int_value("max_id", MAX_ID);
}



static void
set_form_data (const char *course, const char *key, CONFIG_STRUCT *conf, const char *group)
{
  char url[MAX_PATH +1];  
  cs_set_course_info(conf);
  cs_set_current_time();
  print_standalone_form(course, key,group);
  snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s", "admin_main_menu", key, course);
  cs_set_value("back_url", url);
}


int
main ()
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user;
  CONFIG_STRUCT conf;
  char group[MAX_ID +1];
  
  cs_cgi_init();
  read_parameters (course, key, group);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	        /* shared_util.c */

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

  if ( (user.group == FACULTY) || (user.group == ADMIN))
    set_form_data (course, key, &conf, group);
  else
    access_denied_error();   /* shared_access.c */
  cs_cgi_display("admin_add_student_form", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

