#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;
}  




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);
  cs_get_required_parameter ("title", new_conf.title, MAX_COURSE_TITLE);
  cs_get_required_parameter ("semester", new_conf.semester, MAX_SEMESTER);
  cs_get_required_parameter ("instructor", new_conf.instructor, MAX_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");

  /* 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, "");
  get_exclusive_lock(fileno(fp), 1);
  if (fwrite (&new_conf, sizeof (CONFIG_STRUCT), 1, fp) != 1)
  {
    release_lock(fileno(fp)); 
    fclose (fp);
    cs_critical_error (ERR_FOPEN_WRITE_FAILED, "");
  }
  release_lock(fileno(fp)); 
  fclose (fp);
  
  *conf = new_conf;
  
}





/* For most modules, the required directories are created when the course is 
** created, or when a new user is added to a course.  For modules that have
** arrived within Manhattan more recently, such as Surveys and Podcasts, 
** this is not the case.  This function is called to set up the required
** directories for these newer modules.
**/



static
void create_additional_mod_dir(const char *course_path, const char *dirname, P_NODE *head)
{

P_NODE *ptr;
char module_central_inbox_dir[MAX_PATH + 1];

  snprintf (module_central_inbox_dir, MAX_PATH + 1, "%s%s/%s/", course_path, INBOXES_DIR, dirname);
  if(!file_exists(module_central_inbox_dir)) /* shared_file_util.c */
   {
     mkdir(module_central_inbox_dir,0770);          /* create courses/sample/inboxes/podcasts directory */

     /* for each user, create courses/sample/people/username/dirname directory */
     for(ptr=head; ptr; ptr=ptr->next)
         create_module_directory(course_path, ptr->data.username,dirname);  /* shared_add_util.c */
   }  
}




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 */
  P_NODE *head = 0;
    
  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);
     if(conf.surveys || conf.podcasts)
      {
         head = build_option_person_list(&conf);   /* shared_person_list.c */
         if(conf.surveys)
            create_additional_mod_dir(conf.course_path, SURVEYS_DIR, head);
         if(conf.podcasts)
            create_additional_mod_dir(conf.course_path, PODCASTS_DIR, head);
         free_option_person_list(head);   /* shared_person_list.c */   
      }   
   }  
  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;
}

