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





static void
set_back_url(const char *key)
{
   char url[MAX_PATH +1];
   snprintf (url, MAX_PATH +1, "%s?id=%s","super_admin_page",  key);
   cs_set_value("back_url", url);
}

int is_auto_update_file(char *roster)
{
    char *ptr;
    ptr = strstr(roster, "auto_update");
    if(ptr)
	return 1;
    else
	return 0;

}

int main()
{
   char key[MAX_KEY +1];
   
   XML_COURSE_INFO_LIST *xml_info_list;                                  /* shared_roster_xml_parser.h */
   DIRECTORY_NAME_NODE *directory_name_head;
   CREATED_COURSE_NODE *course_head = 0, *standalone_course = 0;
   USER_GROUP_INFO_LIST *user_list = 0;
   INDEX_USER_LIST *index_list = 0;
   
   SESSION user;
   int standalone;     /* was there at least one standalone course? */
   char roster_path[MAX_PATH + 1];   /* full (relative) path to xml file */ 
  
   cs_cgi_init(); 

   /* build a linked list of directory names assigned to each course by the user */
   directory_name_head = get_parameters(key, &standalone, roster_path);
   
   validate_super_key(key, &user);
   set_admin_page(&user, key, 0, "super_new_course_form"); /* shared_super_main_menu.c */

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

   /* incorporate any directory name changes made by the user into the xml_info_list */
   update_xml_info_list(xml_info_list, directory_name_head);         /* add correct course type and internal name to list */
   
   /* no longer need the list of directory names - free it */
   free_directory_name_list(directory_name_head);
   
   
   /* make sure the directory names provided are reasonable */
   check_internal_course_names(xml_info_list);   /* shared_xml_course_create_util.c */
   

   set_back_url(key);
  
   /* 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(xml_info_list);   
   
 
   if(course_head)
   {
      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);
      
      course_head =create_course_with_teachers(course_head); /* shared_xml_course_create_util.c */
      
      user_list = build_user_list(xml_info_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 */
          course_head = write_students_to_password_file(course_head);        /* shared_xml_course_create_util.c */
          reindex_users("../users");     /* shared_user_index.c */
          set_course_table(course_head, "ctr");                              /* shared_xml_course_create_util.c */
	  write_add_drop_list(course_head);                                  /* shared_xml_course_create_util.c */
     } 
   }
     
     
     
   if(standalone)
    standalone_course = standalone_course_list(xml_info_list);       /* shared_xml_course_create_util.c */
   if(standalone_course)
   {
        update_standalone_username(standalone_course);               /* shared_xml_course_create_util.c */
        create_course_with_teachers(standalone_course);              /* shared_xml_course_create_util.c */
        create_standalone_with_students(standalone_course);          /* shared_xml_course_create_util.c */
        write_students_to_password_file(standalone_course);          /* shared_xml_course_create_util.c */
        set_course_table(standalone_course, "std");                  /* shared_xml_course_create_util.c */
   }


   free_xml_data_list(xml_info_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(standalone_course)
     free_course_f_list(standalone_course);   /* shared_xml_course_create_util.c */
   if(user_list)
     free_user_group_info_list(user_list);    /* shared_xml_course_create_util.c */
     
   free_new_course_lock();   /* shared_new_course_lock.c */  

  if(!is_auto_update_file(roster_path))
   unlink(roster_path);
   cs_cgi_display("super_xml_course_create", 1);
   cs_cgi_destroy(); 
   return 0;
}
    



	       
