#include <stdio.h>		/* standard io functions */
#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_news_util.h"


static void
read_parameters (char *course, char *key, int *first)
{
  char temp[20];
  cs_get_required_parameter ("crs", course, MAX_PATH);	/* shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_optional_parameter ("first", temp, 19);
   
  if (strlen(temp))
    *first = 1;
  else
    *first = 0;

}




void
set_form_data (const char *course, const char *key, CONFIG_STRUCT *conf)
{
 char url[MAX_PATH +1];

  cs_set_course_info(conf);
  cs_set_current_time();
  cs_set_int_value("max_course_no", MAX_COURSE_NO);
  cs_set_int_value("max_title", MAX_COURSE_TITLE);
  cs_set_int_value("max_instructor", MAX_INSTRUCTOR);
  cs_set_int_value("max_semester", MAX_SEMESTER);

  snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s", "admin_main_menu", key, course);
  cs_set_value("back_url", url);

  cs_set_int_value("is_standalone", conf->standalone? 1 : 0);
  
  cs_set_int_value("spell_check_enabled_on_server", SPELL_CHECK_ENABLED? 1 : 0);
  
  if(SPELL_CHECK_ENABLED)   /* #defined in custom.h */
         cs_set_int_value("spell_check_enabled_in_classroom", !modules_enabled(conf)?  1 : ALLOW_SPELL_CHECK(conf->misc)?1:0);  /* ALLOW_SPELL_CHECK is #defined in global.h */

  
  /** classroom locking
  *** modules_enabled is in shared_news_util.c 
  *** CLASSROOM_LOCKED is a macro #defined in global.h 
  **/
  
  cs_set_int_value("classroom_locked", !modules_enabled(conf) ? 1 : CLASSROOM_LOCKED(conf->misc)? 1 : 0);
  
  
  /*** editor width selection ***/
  cs_set_int_value("allow_editor_width_selection", ENABLE_EDITOR_WIDTH_SELECTION); /* custom.h */
  cs_set_int_value("current_editor_width_selection",
                           EDITOR_WIDTH_PREFERENCE(conf->misc));   /* macro #defined in global.h */
                           
  /* EDITOR_WIDTH_XXX in next is #defined in custom.h */                           
  cs_set_int_value("editor_width.0", EDITOR_WIDTH_STANDARD);                           
  cs_set_int_value("editor_width.1", EDITOR_WIDTH_MEDIUM);                           
  cs_set_int_value("editor_width.2", EDITOR_WIDTH_LARGE);                             
  cs_set_int_value("editor_width.3", EDITOR_WIDTH_XLARGE);                             
  
  /* USED_FIXED_FONT is #defined in global.h */         
  cs_set_int_value("fixed_font_enabled", USE_FIXED_FONT(conf->misc)?1:0);
  
  /* EMOTICONS_DISABLED is #defined in global.h */
  cs_set_int_value("emoticons_disabled", EMOTICONS_DISABLED(conf->misc)?1:0);  
           


  /* Next it to allow the page show special message when there are
  ** no modules currently enabled
  */
  cs_set_int_value("modules_enabled", modules_enabled(conf)? 1: 0);
           
  
  cs_set_int_value("module.assignments", conf->assignments);
  cs_set_int_value("module.lectures", conf->lectures);
  cs_set_int_value("module.syllabus", conf->syllabus);
  cs_set_int_value("module.internet", conf->internet);  
  cs_set_int_value("module.selftest", conf->selftest);
  cs_set_int_value("module.discussion", conf->discussion);
  cs_set_int_value("module.podcasts", conf->podcasts);
    
  if (!conf->template)
    {
      cs_set_int_value("module.anonymous", conf->anonymous);
      cs_set_int_value("module.lounge", conf->lounge);
      cs_set_int_value("module.team", conf->team);
      cs_set_int_value("module.teamteach", conf->team_teach);      
      cs_set_int_value("module.surveys", conf->surveys);
      cs_set_int_value("module.postoffice", conf->postoffice);

      cs_set_int_value("grades", conf->grades);


/*** PEOPLE and CHAT modules don't give the user the ability to hide/unhide messages
**** so we'll give them different names to help with the CS template
***/
     cs_set_int_value("nohide_module.people", conf->people);
     cs_set_int_value("nohide_module.chat", conf->chat);     
          
    }
}






int
main ()
{

  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  int first;			/* from first=? command line */
  SESSION user;
  CONFIG_STRUCT conf;

  cs_cgi_init();
  read_parameters (course, key, &first);
  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))
   {
     set_form_data (course, key, &conf);
   }
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");
  cs_cgi_display("admin_module_form", 1);
  cs_cgi_destroy();
  return 0;

}

