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

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


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


static void
read_parameters (char *course, char *key, int *oper, char *name)
{
  char temp[30];
  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_set_value("crs", course);
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_set_value("id", key);
  cs_get_optional_parameter ("oper", temp, 29);
  if(strlen(temp))
     *oper =1;
  else
    *oper =0;
  if(*oper)
     cs_get_required_parameter ("student", name, 99);


}



void
set_common_data(const char *course, const char *key,CONFIG_STRUCT *conf)
{
    char url[MAX_PATH +1];
    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();
}




static void
set_list_data (const char *course, const char *key,CONFIG_STRUCT *conf)
{
  P_NODE *pers_head;

  set_common_data(course, key,conf);
  pers_head = build_option_person_list (conf);	/* shared_person_list.c */
  set_option_userlist (pers_head);	/* shared_person_list.c */
  free_option_person_list (pers_head);	/* shared_person_list.c */
}






void
set_all_hidden(USER_DATA *data, char *course, char *key, CONFIG_STRUCT *conf)
{
   char temp[20];

   cs_set_value("id", key);
   cs_set_value("crs", course);
   cs_set_value("username", data->username);
   cs_set_value("realname", data->realname);
   cs_set_value("group", data->group == STUDENT ? "student":"faculty" );
   cs_set_value( "ssn", data->id);
   cs_set_value("is_standalone", conf->standalone ? "1":"0");
   snprintf(temp, 2, "%c", data->team);
   cs_set_value("team", temp);
   cs_set_value( "alias", data->alias);

}


static void 
get_report_data (USER_DATA *data, char *course,  char *key, CONFIG_STRUCT *conf)
{
   char temp[20];
   set_common_data(course, key,conf);
   cs_set_int_value("display", 1);
   cs_set_value("username", data->username);
   cs_set_value("realname", data->realname);
   cs_set_value("alias", data->alias);
   cs_set_value("user_id", data->id);

   snprintf(temp, 2, "%c", data->team);
   cs_set_value("team", temp);
   cs_set_team();   /* shared_cs_util.c */
   set_all_hidden(data, course, key, conf);
}


int
main ()
{
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  char name[100];	/* from id=? command line */
  SESSION user;
  CONFIG_STRUCT conf;
  int oper =0;
  USER_DATA data;

  cs_cgi_init();

  read_parameters (course, key, &oper, name);
  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))
   {
      if(!oper)
        set_list_data (course, key, &conf);
      else
      {
          get_user_data (name, &data, &conf);    /* shared_person_list.c */
	  get_report_data (&data, course, key, &conf);
      }
     
   }
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");

   #ifdef HIDE_MANHATTAN_USERNAMES
   cs_set_int_value("hide_manhattan_username", 1);
  #endif
    
  cs_cgi_display("admin_chg_form", 1);
  cs_cgi_destroy();
  return 0;

}

