/* super_update_courses.c updates existing courses based on 
 * roster.xml file that is stored in auto_update/rosters
 */

#include "manhat-lib/shared_xml_course_create_util.h"
#include "manhat-lib/shared_course_request_xml.h"
#include "manhat-lib/shared_super_menu.h"
#include "custom.h"

void dump_xml_list(XML_COURSE_INFO_LIST *list);
void dump_user_list(USER_GROUP_INFO_LIST *user_list);

   



void    
read_parameters( char *roster_path, char *key)
{
    char *address = 0;
    char temp[MAX_PATH +1];


    cs_get_optional_parameter ("id", key, MAX_KEY);
    if(strlen(key) == 0)
    {
     address = hdf_get_value(global_cgi->hdf, "CGI.RemoteAddress", 0);
     if(!address)
	cs_critical_error(ERR_REQUEST_DENIED, " update course");
     if(strcmp(address, MANHATTAN_SERVER_IP))
	cs_critical_error(ERR_REQUEST_DENIED, " update course");
    
     cs_get_required_parameter ("roster_path", roster_path, MAX_PATH);
    }
    else
    {
        cs_get_required_parameter ("roster_path", temp, MAX_PATH);
	snprintf(roster_path, MAX_PATH +1, "%s/%s", AUTO_UPDATE_ROSTER_PATH, temp);
    }

}

P_NODE * get_one_user_node(P_NODE *users)
{
     P_NODE *new_node;

     new_node = (P_NODE*)malloc(sizeof(P_NODE));
     if(!new_node)
	 cs_critical_error(ERR_MALLOC_FAILED, "get_one_user_node");

     strncpy(new_node->data.realname, users->data.realname, MAX_NAME +1);
     strncpy(new_node->data.lastname, users->data.lastname, MAX_NAME +1);
     strncpy(new_node->data.id, users->data.id, MAX_ID +1);
     if(strlen(users->data.username))
        strncpy(new_node->data.username, users->data.username, MAX_USERNAME +1);
     else
	new_node->data.username[0] = '\0';
     new_node->data.group = users->data.group;
     new_node->next = 0;

    return new_node;

}

XML_COURSE_INFO* get_course_info_node(XML_COURSE_INFO *one_course, const char *manhat_id, COURSE_TYPE type)
{
    XML_COURSE_INFO *ptr;
    P_NODE *users, *user_ptr;

     ptr = (XML_COURSE_INFO*)malloc(sizeof(XML_COURSE_INFO));
     if(!ptr)
	  cs_critical_error(ERR_MALLOC_FAILED, "get_course_info_node");

     strncpy(ptr->subdir, one_course->subdir, MAX_COURSE_SUBDIR +1); 
     strncpy(ptr->dir_id, one_course->dir_id, MAX_COURSE_ID +1); 
     strncpy(ptr->real_dir_id, manhat_id, MAX_COURSE_ID +1); 
     strncpy(ptr->semester, one_course->semester, MAX_SEMESTER +1); 
     strncpy(ptr->course_code, one_course->course_code, MAX_COURSE_NO +1); 
     strncpy(ptr->title, one_course->title, MAX_COURSE_TITLE +1); 
     if(strlen(one_course->instructor_title))
        strncpy(ptr->instructor_title, one_course->instructor_title, MAX_INSTRUCTOR +1); 
     else
	ptr->instructor_title[0] = '\0';
     ptr->course_type = type;
     ptr->next = 0;

     ptr->user_head = 0;
     ptr->user_current = 0;

     for(users = one_course->user_head; users; users = users->next)
     {
           user_ptr = get_one_user_node(users);
	   if(!ptr->user_head)
		 ptr->user_head = user_ptr;
	   else
		 ptr->user_current->next = user_ptr;
	   ptr->user_current = user_ptr;
		 
     }
     return ptr;
}








static XML_COURSE_INFO_LIST * 
get_existing_course_list(XML_COURSE_INFO_LIST *list, SUBDIR_COURSE_LIST *sub_list)
{
    XML_COURSE_INFO_LIST *exist_list =0;
    XML_COURSE_INFO *ptr;
    int found =0;
    COURSE_TYPE type;
    char manhat_course_id[MAX_COURSE_ID +1];
    
    if(sub_list)
    {
	
        exist_list = (XML_COURSE_INFO_LIST *)malloc(sizeof(XML_COURSE_INFO_LIST));
        exist_list->head = 0;
	exist_list->current = 0;

	for(list->current=list->head; list->current; list->current=list->current->next)
	{
	  found = find_manhat_course_id(sub_list,
	         list->current->subdir,list->current->dir_id,manhat_course_id, &type);
	  if(found)
	  {
             ptr = get_course_info_node(list->current, manhat_course_id, type);

             if(!exist_list->head)
		    exist_list->head = ptr;
	     else
		    exist_list->current->next = ptr;
	     exist_list->current = ptr;
	        
	  }
	}
    }
    return exist_list;
} 





int main()
{
   
   XML_COURSE_INFO_LIST *xml_info_list;                                  /* shared_roster_xml_parser.h */
   XML_COURSE_INFO_LIST *existing_course_list =0;                                  /* shared_roster_xml_parser.h */
   CREATED_COURSE_NODE *course_head = 0;
   USER_GROUP_INFO_LIST *user_list = 0;
   INDEX_USER_LIST *index_list = 0;
   SUB_DIR_ID_NODE *unique_dir_list;
   SUBDIR_COURSE_LIST *sub_list;
   char roster_path[MAX_PATH + 1];   /* full (relative) path to xml file */ 
   char msg[MAX_PATH + 1];   /* full (relative) path to xml file */ 
   char key[MAX_KEY +1];
   SESSION user;

   cs_cgi_init(); 

   key[0] = '\0';

   /* build a linked list of directory names assigned to each course by the user */
   read_parameters( roster_path, key);
   if(strlen(key))
   {
	validate_super_key(key, &user);
	set_admin_page(&user, key, 0, "super_new_course_form");

   }

   /* parse the xml file into a linked list */
   xml_info_list = parse_xml_file(roster_path);   /* parse xml file */

   unique_dir_list = build_unique_subdir_list(xml_info_list);  /* shared_course_info.c */
   sub_list = build_sub_list_info(unique_dir_list);            /* shared_course_info.c */
   free_sub_list_info(unique_dir_list);                        /* shared_course_info.c */

   if(sub_list)
      existing_course_list = get_existing_course_list(xml_info_list, sub_list);     
   free_xml_data_list(xml_info_list);


   if(!existing_course_list)
   {
	   snprintf(msg, MAX_PATH +1, "%s %s", "No courses were found in", roster_path);
	   update_roster_log(ROSTER_UPDATE_COURSE, "", "", "", -1, -1, msg); /* shared_wnec_roster_log.h */
       cs_critical_error(ERR_GENERAL_ERROR, "NO courses were found that were automatically created from a SIS roster.");
   }
   if(!existing_course_list->head)
   {
       free(existing_course_list);
	   snprintf(msg, MAX_PATH +1, "%s %s", "No courses were found in", roster_path);
	   update_roster_log(ROSTER_UPDATE_COURSE, "", "", "", -1, -1, msg); /* shared_wnec_roster_log.h */
       cs_critical_error(ERR_GENERAL_ERROR, "NO courses were found that were automatically created from a SIS roster.");
   }
  
   

   /* only one admin can create a course at a time */
   get_new_course_lock();    /* shared_new_course_lock.c */
   
   

   /* start list with normal and template courses
   ** and only teacher info 
   */
   course_head = build_normal_template_course_list(existing_course_list);  /* shared_xml_course_create_util.c */ 
   
 
   if(course_head)
   {
	  update_roster_log(ROSTER_UPDATE_COURSE, roster_path, "", "", -1, -1, "Start update courses"); /* shared_wnec_roster_log.h */

      index_list = (INDEX_USER_LIST *)malloc(sizeof(INDEX_USER_LIST));   
      index_list->current =0;
      index_list->head =0;

      index_list = build_index_user_list(index_list);     /* shared_user_index.c */
      index_list = derive_teacher_username_password(course_head,index_list); /*shared_xml_course_create_util.c */

      course_head =create_course_with_teachers(course_head);   /* shared_xml_course_create_util.c */

      user_list = build_user_list(existing_course_list);    /* list contains all students, with their courses */


      if(user_list)
       {
          index_list=derive_student_username_password(user_list,index_list);  /* shared_xml_course_create_util.c */
          course_head = add_students_to_courses(course_head, user_list);      /* shared_xml_course_create_util.c */
          write_students_to_password_file(course_head);                       /* shared_xml_course_create_util.c */
          reindex_users("../users");     /* shared_user_index.c */
	      write_add_drop_list(course_head);                                   /* shared_xml_course_create_util.c */
          set_course_data_record_log(course_head, "ctr", ROSTER_UPDATE_COURSE);                                /* shared_xml_course_create_util.c */
      } 
	  update_roster_log(ROSTER_UPDATE_COURSE, roster_path, "", "", -1, -1, "End update courses"); /* shared_wnec_roster_log.h */
   }


   free_xml_data_list(existing_course_list);
   if(index_list)
     free_index_user_list(index_list);     /* shared_user_index.c */
   if(course_head)
     free_course_f_list(course_head);      /* shared_xml_course_create_util.c */
   if(user_list)
     free_user_group_info_list(user_list); /* shared_xml_course_create_util.c */
   free_subdir_course_list(sub_list);

   free_new_course_lock();   /* shared_new_course_lock.c */  
   cs_cgi_display("super_xml_course_create", 1);
   cs_cgi_destroy(); 
   return 0;
}
    



	       
