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


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

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



static void
read_parameters (char *course, char *key)
{
  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
}


static
void  set_menu_item(int item_number, char *item_name, int should_display, char *url)
{
 #define MAX_TMP_NAME 30 
 char name[MAX_TMP_NAME + 1];
 
 snprintf(name, MAX_TMP_NAME, "item.%d.name", item_number);
 cs_set_value(name, item_name);
 
 snprintf(name,MAX_TMP_NAME, "item.%d.should_display", item_number);
 cs_set_int_value(name, should_display);
 
 snprintf(name,MAX_TMP_NAME, "item.%d.url", item_number);
 cs_set_value(name, url);
 
  #undef MAX_TMP_NAME 
}

  
static void
set_admin_data (const char *course, const char *key, CONFIG_STRUCT *conf, SESSION *user, int can_write)
{
 
  char url[MAX_PATH +1];
  char server_string[MAX_PATH +1];

  cs_set_course_info(conf);
  cs_set_current_time();

  if(conf->template)
     cs_set_int_value("is_template", 1);
  
  
  get_server_string(server_string, MAX_PATH +1);   /* shared_server_string.c */
  if(user->group == ADMIN)  /* admin entered this program -- provide way back to super admin program */
    snprintf (url, MAX_PATH +1, "%s/%s/%s?id=%s", server_string, SBIN_ALIAS, "super_config_course_list", user->realname);
  else
     snprintf (url, MAX_PATH +1, "%s?crs=%s&amp;id=%s", "main_menu", course, key);
  cs_set_value("back_url", url);




  /* select course modules, etc. - available only if user can_write */
  if(can_write)
    snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","admin_module_form", course, key); 
  else
    *url = '\0';  

  set_menu_item(0, "select_modules", 1, url);
  


  /* view password file - always available*/
  snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","admin_list_passwd", course, key);
  set_menu_item(1, "view_password_file", 1, url);




  
  /* download password file - always available */
  snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;go=yes","admin_download_passwd", course, key); 
  set_menu_item(2, "get_course_roster", 1, url);
  

  /* statistics menu - always available */
  snprintf(url, MAX_PATH + 1, "%s?crs=%s&amp;id=%s","stats_menu", course, key); 
  set_menu_item(3, "stats_menu", 1, url);



  /* add a student - available only if user can_write */
  if(can_write)
    {
      snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;group=%s",
		   conf->standalone?"admin_add_student_form":"admin_search_form", course, key, "student");
    }
  else
     *url = '\0';
  
  set_menu_item(4, "add_student", 1, url);   
     

   /* add a teacher - available only if user can_write */
  if(can_write) 
    {
    snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;group=%s",
                conf->standalone?"admin_add_student_form":"admin_search_form", course, key, "faculty");
    }
  else
    *url = '\0';  
  set_menu_item(5, "add_teacher", 1, url);
  

  /* delete user - available only if user can_write */
  if(can_write)
    {
      snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;faculty=%s",
          			"admin_del_student_form", course, key, "student");
    }
  else
     *url = '\0';
     
  set_menu_item(6, "delete_user", 1, url);           			
  

  /* reset password - avail only if user can_write */
   if((can_write && is_wnec_faculty(user->id)) || user->group == ADMIN) /* shared_util.c */
   {
     snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s", "admin_reset_pass_form", course, key);
   }
  else
    *url = '\0';
    
  set_menu_item(7, "reset_password", ALLOW_PASSWORD_RESET? 1:0, url);   


  
  /* change user info - available only if user can_write */
  if(can_write)
    {
      snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","admin_chg_form", course, key);
     }
  else 
     *url = '\0';
     
  set_menu_item(8, "change_name", 1, url);  
  

  /* assign teams - available only if user can_write */
  if(can_write)
    {
      snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","admin_assign_team_form", course, key);
    }
  else
     *url = '\0';
       
  set_menu_item(9, "assign_teams", 1, url);                      


  

  /* clipboard viewer - not available in capture mode */
   snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s","clipboard_view", course, key); 
  set_menu_item(10, "clipboard_view", conf->access != ACCESS_CAPTURE? 1: 0, 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;		/* the configuration read from config file */
  int can_write;
  
  cs_cgi_init(); 
  read_parameters (course, key);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */

  can_write = has_write_permission(&user, &conf);   /* shared_access.c */
  
  if ((user.group == FACULTY) || (user.group == ADMIN))
    set_admin_data (course, key, &conf, &user, can_write);
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");

  cs_cgi_display("admin_main_menu", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

