#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_add_util.h"
#include "manhat-lib/shared_file_util.h"

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

  cs_get_required_parameter ("crs", course, MAX_PATH);	/* shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);

}


int config_value(char *name)
{
 
 int value;
 value = hdf_get_int_value( global_cgi->hdf, name,  0); 
 return value;
}  








/* need to remove < and > from some items
** since HTML codes can mess up the display.
** Normal html escaping will increase the length of the
** strings, which is a problem since they have to fit into
** fixed length string arrays.
**
** Simply convert < to ( and > to )
**/


static void  
convert_angle_brackets(char *target)
{
  char *ptr;

  for(ptr = target; *ptr; ptr++)
   {
     if(*ptr == '>')
           *ptr = ')';
     else
       if(*ptr == '<')
              *ptr = '(';
   }
                
}








static void
update_configuration (const char *crs, CONFIG_STRUCT *conf)
{
  CONFIG_STRUCT new_conf;

  
  char fname[MAX_PATH + 1];

  FILE *fp;


  /* first copy over the data items which we are not allowing the user to change */
  new_conf = *conf;

  cs_get_required_parameter ("course", new_conf.course_no, MAX_COURSE_NO);
  convert_angle_brackets(new_conf.course_no);


  cs_get_required_parameter ("title", new_conf.title, MAX_COURSE_TITLE);
  convert_angle_brackets(new_conf.title);
  

  cs_get_required_parameter ("semester", new_conf.semester, MAX_SEMESTER);
  convert_angle_brackets(new_conf.semester);
  
  
  cs_get_required_parameter ("instructor", new_conf.instructor, MAX_INSTRUCTOR);
  convert_angle_brackets(new_conf.instructor);  
  
  new_conf.assignments = config_value("Query.assignments");
  new_conf.lectures = config_value("Query.lectures");
  new_conf.syllabus = config_value("Query.syllabus");
  new_conf.discussion = config_value("Query.discussion");
  new_conf.postoffice = config_value("Query.postoffice");
  new_conf.internet = config_value("Query.internet");
  new_conf.lounge = config_value("Query.lounge");
  new_conf.grades = config_value("Query.grades");
  new_conf.anonymous = config_value("Query.anonymous");
  new_conf.team = config_value("Query.team");
  new_conf.team_teach = config_value("Query.teamteach");
  new_conf.chat = config_value("Query.chat");
  new_conf.selftest = config_value("Query.selftest");
  new_conf.people = config_value("Query.people");
  new_conf.surveys = config_value("Query.surveys");  
  new_conf.podcasts    = config_value("Query.podcasts");
  new_conf.calendar    = config_value("Query.calendar");
  new_conf.testpilot    = config_value("Query.testpilot");

  /* UNSET_SPELL_CHECK, UNSET_FIXED_FONT, et. al. in the next are macros
  ** #defined in global.h
  */  
  new_conf.misc = 0;
  new_conf.misc = config_value("Query.spell")? SET_SPELL_CHECK(new_conf.misc) : UNSET_SPELL_CHECK(new_conf.misc);  
  new_conf.misc = config_value("Query.lock")? LOCK_CLASSROOM(new_conf.misc) : UNLOCK_CLASSROOM(new_conf.misc);    
  new_conf.misc = config_value("Query.fixed_font")? SET_FIXED_FONT(new_conf.misc) : UNSET_FIXED_FONT(new_conf.misc); 
  new_conf.misc = config_value("Query.emoticons_disabled")? DISABLE_EMOTICONS(new_conf.misc) : ENABLE_EMOTICONS(new_conf.misc); 
  new_conf.misc = SET_EDITOR_WIDTH_PREFERENCE(new_conf.misc, config_value("Query.editor_width")); 
 
  snprintf (fname, MAX_PATH + 1, "%s%s", conf->course_path, CONFIG_FNAME);
  fp = fopen (fname, "w");
  if (!fp)
    cs_critical_error (ERR_FOPEN_WRITE_FAILED, fname);
  
  get_exclusive_lock(fileno(fp), 1);    /* shared_lock.c */
  
  if (fwrite (&new_conf, sizeof (CONFIG_STRUCT), 1, fp) != 1)
  {
    release_lock(fileno(fp)); 
    fclose (fp);
    cs_critical_error (ERR_FOPEN_WRITE_FAILED, fname);
  }
  
  release_lock(fileno(fp));    /* shared_lock.c */
  
  fclose (fp);
  
  *conf = new_conf;
  
}









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 */
    
  cs_cgi_init();
  read_parameters (course, key);
  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))
   {
     update_configuration (course, &conf);
   }  
  else
    cs_critical_error (ERR_REQUEST_DENIED, "");
    
  if(user.group == FACULTY)
        printf ("Location:%s?crs=%s&id=%s\n\n", "main_menu", course, key);
  else
        printf ("Location:%s?crs=%s&id=%s\n\n", "admin_main_menu", course, key);
        
  cs_cgi_destroy(); 
		  
  return 0;
}

