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

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_file", temp, MAX_PATH);
      snprintf(roster_path, MAX_PATH +1, "%s/%s", AUTO_UPDATE_ROSTER_PATH, temp);
    }

}

USER_NODE *
initialize_user_node(P_NODE *one_user)
{
    USER_NODE *node;
    node = (USER_NODE*) malloc (sizeof(USER_NODE));
    if(!node)
	cs_critical_error(ERR_MALLOC_FAILED, "initialize_user_node");
    strncpy(node->user.first, one_user->data.realname, MAX_NAME +1);
    strncpy(node->user.last, one_user->data.lastname, MAX_NAME +1);
    strncpy(node->user.id, one_user->data.id, MAX_ID +1);
    node->user.group = one_user->data.group;
    if(!strlen(one_user->data.username))
       strncpy(node->user.username, one_user->data.username, MAX_USERNAME +1);
    else
      node->user.username[0] = '\0';
    node->next = 0;

    return node;
}

USER_NODE *
get_faculty_list(XML_COURSE_INFO_LIST *list, GROUP_TYPE type )
{
    P_NODE *ptr;
    USER_NODE *u_ptr, *u_head = 0, *u_current =0;
    INDEX_USER_LIST *index_list = 0;

    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 */

    for(list->current = list->head; list->current; list->current = list->current->next)
    {
	  for(ptr = list->current->user_head; ptr; ptr = ptr->next)
	  {
	      if(ptr->data.group == type)
	      {
		      u_ptr = initialize_user_node(ptr);
		      index_list = process_one_username(&(u_ptr->user), index_list);
		      if(!u_head)
			    u_head = u_ptr;
		      else
			    u_current->next = u_ptr;
		      u_current = u_ptr;

	      }
	  }
    }
    if(index_list)
        free_index_user_list(index_list);     /* shared_user_index.c */

    return u_head;
}
int create_empty_course_file(const char *username)
{
    char path[MAX_PATH + 1];
    FILE *fp;
    char dir;
    dir = sub_dir_name(username);
    snprintf(path, MAX_PATH + 1,
               "../%s/%c/%s/%s", USERS_DIR, dir,
                               username,COURSE_LIST_FNAME);
    fp = fopen(path,"w");
    if(!fp)
        return 0;
    fclose(fp);

   return 1;

}


void
add_faculty_to_system(USER_NODE *faculty_list)
{
#define MAX_ADD_NAME 50
     char realname[MAX_NAME +1];
     USER_NODE *ptr;
     char name[MAX_ADD_NAME];
     int i = 0;


     for(ptr = faculty_list; ptr; ptr = ptr->next)
     {
	   if(ptr->user.is_new == NEW)
	   {
              snprintf(realname, MAX_NAME +1, "%s %s", ptr->user.first, ptr->user.last);
	      if(!create_central_letter_dir (sub_dir_name(ptr->user.username)))
	         ptr->user.central_state = C_ERROR;

	      if(!create_central_user_dir(ptr->user.username))    /* shared_central_user.c */
                 ptr->user.central_state = C_ERROR;

              if(!write_central_id_file (realname,ptr->user.username, ptr->user.id))  /* shared_central_user.c */
                 ptr->user.central_state = C_ERROR;

	      if(!write_central_password_file (ptr->user.username, ptr->user.password))  /* shared_central_user.c */          
		  ptr->user.central_state = C_ERROR;
              if(!create_empty_course_file(ptr->user.username))   /* shared_central_user.c */
	               ptr->user.central_state = C_ERROR;

	      else{
		   ptr->user.central_state = SUCCESS;
	           ptr->user.is_new = NEW_1;   /* central records already updated for this user */
		   }
	      snprintf(name, MAX_ADD_NAME, "add.%d.id", i);
	      cs_set_value(name, ptr->user.id);
	      snprintf(name, MAX_ADD_NAME, "add.%d.realname", i);
	      cs_set_value(name, realname);
	      snprintf(name, MAX_ADD_NAME, "add.%d.username", i);
	      cs_set_value(name, ptr->user.username);
	      snprintf(name, MAX_ADD_NAME, "add.%d.state", i);
	      if(ptr->user.central_state == SUCCESS)
	        cs_set_value(name, "add success");
	      else
	        cs_set_value(name, "add failed");
          i++;
	   }
	   
     }
	 update_roster_log(ROSTER_ADD_FACULTY, "", "", "", i, -1, "add success");
#undef MAX_ADD_NAME 
}


int main()
{
   
   USER_NODE *list =0;
   XML_COURSE_INFO_LIST *xml_info_list;                                  /* shared_roster_xml_parser.h */
   char key[MAX_KEY +1];
   SESSION user;
   char *std = 0; 
   char roster_path[MAX_PATH + 1];   /* full (relative) path to xml file */ 
  
   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 */
   std = hdf_get_value(global_cgi->hdf, "Query.std", "");
   if(strlen(std) > 0)
      list = get_faculty_list(xml_info_list, STUDENT);
   else
      list = get_faculty_list(xml_info_list, FACULTY);
   free_xml_data_list(xml_info_list);
   if(list)
   {

   /* only one admin can create a course at a time */
   get_new_course_lock();    /* shared_new_course_lock.c */
   update_roster_log(ROSTER_ADD_FACULTY, roster_path, "", "", -1, -1, "Start add faculty"); /* shared_wnec_roster_log.h */
   add_faculty_to_system(list);
   reindex_users("../users");     /* shared_user_index.c */
   update_roster_log(ROSTER_ADD_FACULTY, roster_path, "", "", -1, -1, "End add faculty"); /* shared_wnec_roster_log.h */
   if(list)
      free_teacher_node(list);
   free_new_course_lock();   /* shared_new_course_lock.c */  
  }
   cs_cgi_display("super_auto_add_faculty", 1);
   cs_cgi_destroy(); 
   return 0;
}
    



	       
