#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
              


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_roster_xml_parser.h"
#include "manhat-lib/shared_survey_string.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_course_info.h"
#include "manhat-lib/shared_course_request_xml.h"





XML_COURSE_INFO_LIST *
build_xml_data_list(const char *roster_path)
{
   XML_COURSE_INFO_LIST *head;    /* shared_roster_xml_parser.h */
   char *buf;
   FILE *fp;
   struct stat file_stat;


   if(stat(roster_path, &file_stat))
	   cs_critical_error( ERR_FOPEN_READ_FAILED, roster_path);

   buf = (char*)malloc(file_stat.st_size +1);
   if(!buf)
        cs_critical_error( ERR_MALLOC_FAILED, "build_xml_data_list()");

   fp = fopen(roster_path, "r");
   if(!fp)
        cs_critical_error(ERR_FOPEN_READ_FAILED , roster_path);

   if(fread(buf,file_stat.st_size,1,fp) != 1)
        cs_critical_error(ERR_FREAD_FAILED, roster_path);
   fclose(fp);
   
   buf[file_stat.st_size] = '\0';

   head = xml_course_data_parser(buf);   /* shared_roster_xml_parser.c */
   
   free(buf);
   return head;
}

XML_COURSE_INFO * 
get_one_course_roster(XML_COURSE_INFO *one_course)
{
    XML_COURSE_INFO *ptr;
    P_NODE *person;
    
    ptr = (XML_COURSE_INFO *) malloc(sizeof(XML_COURSE_INFO));
    if(!ptr)
	cs_critical_error(ERR_MALLOC_FAILED, "");
    ptr->user_head = 0;
    ptr->user_current = 0;
    ptr->next = 0;

    strncpy(ptr->course_code, one_course->course_code, MAX_COURSE_NO +1);
    strncpy(ptr->title, one_course->title, MAX_COURSE_TITLE +1);
    strncpy(ptr->semester, one_course->semester, MAX_SEMESTER +1);
    strncpy(ptr->subdir, one_course->subdir, MAX_COURSE_SUBDIR +1);
    strncpy(ptr->dir_id, one_course->dir_id, MAX_COURSE_ID +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 = CENTRAL;
    for(one_course->user_current = one_course->user_head;
         one_course->user_current; 
        one_course->user_current = one_course->user_current->next)
    {
         person = (P_NODE*)malloc(sizeof(P_NODE));
         if(!person)
	    cs_critical_error(ERR_MALLOC_FAILED, "admin_get_course_list");
         person->next = 0;
	 strncpy(person->data.realname, one_course->user_current->data.realname, MAX_NAME +1);
	 strncpy(person->data.lastname, one_course->user_current->data.lastname, MAX_NAME +1);
	 if(strlen(one_course->user_current->data.username))
	    strncpy(person->data.username, one_course->user_current->data.username, MAX_USERNAME +1);
	 else
	     person->data.username[0] = '\0';
	 strncpy(person->data.id, one_course->user_current->data.id, MAX_ID +1);
	 person->data.group = one_course->user_current->data.group;

         if(!ptr->user_head) 
             ptr->user_head = person;
	 else 
             ptr->user_current->next = person;
	 ptr->user_current = person;

    }
   
    return ptr;
}

XML_COURSE_INFO_LIST * 
get_my_course_list(XML_COURSE_INFO_LIST *list, SESSION *user)
{
    XML_COURSE_INFO_LIST *head;
    P_NODE *ptr;
    int flag = 0;
    XML_COURSE_INFO *one_course;

    head = (XML_COURSE_INFO_LIST *) malloc(sizeof(XML_COURSE_INFO_LIST));
    if(!head)
	cs_critical_error(ERR_MALLOC_FAILED, "");
    head->head =0;
    head->current =0;

    for(list->current = list->head; list->current; list->current = list->current->next)
    {
       flag = 0;
       for(ptr = list->current->user_head; ptr; ptr = ptr->next)
       {
	   if( strcmp(ptr->data.id, user->id) == 0 && ptr->data.group == FACULTY )
	   {
               flag = 1;
	       break;
	   }

       }    
       if(flag)
       {
            one_course = get_one_course_roster(list->current);
	    if(!head->head)
		   head->head = one_course;
	    else
		   head->current->next = one_course;
            head->current = one_course;
       }
   } 
   return head; 
}


   
void read_parameters(char *key, char *path, char *division)
{
   char temp[MAX_PATH +1]; 
   char *ptr;

   cs_get_required_parameter ("id", key, MAX_KEY );    /* shared_cs_util.c */
   cs_get_required_parameter ("roster_path", temp, MAX_PATH );    /* shared_cs_util.c */
   ptr = strchr(temp, ':');
   if(ptr)
       *ptr = '\0';
   strncpy(path, temp, MAX_PATH +1);
   ptr++;
   strncpy(division, ptr, MAX_PATH +1);
   cs_set_value("division", ptr); 


}

int find_in_request_list(XML_COURSE_INFO *one, REQUEST_COURSE_LIST *request_list, int i)
{
     char name[MAX_PATH +1];
      for(request_list->current = request_list->head;
		      request_list->current;
		      request_list->current = request_list->current->next)
      {
	    if( !strcmp(one->subdir, request_list->current->subdir) &&
		!strcmp(one->dir_id, request_list->current->sis_id))
	    {
                  snprintf(name, MAX_PATH +1, "sis_course.%d.faculty", i);
		  cs_set_value(name, request_list->current->realname);
		    return 1;
	    }
      }
      return 0;
}

int  has_existing_request(REQUEST_COURSE_LIST *request_list, XML_COURSE_INFO_LIST *my_list)
{
     int i =0, count = 0, request_existing = 0, new = 0;
     char name[MAX_PATH +1];
     
	for(my_list->current = my_list->head;
			my_list->current; my_list->current = my_list->current->next)
	{
	     if(!strcmp(my_list->current->instructor_title, "new"))
	     {
		  new++;
                  snprintf(name, MAX_PATH +1, "sis_course.%d.request", i); 
		  if(find_in_request_list(my_list->current, request_list, i))
		  {
		     cs_set_int_value(name, 1);
                     request_existing++;
		  }
		  else
		  {
		     cs_set_int_value(name, 0);
	             count++;
		  }
	     }
	     i++;
	}
	if(new == request_existing)
	{
           cs_set_int_value("has_new_requests", 0);
	   return 0;
	}
	else    
	{
          cs_set_int_value("has_new_requests", new - request_existing);
          return 1;
	}

}

int  check_existing_request(XML_COURSE_INFO_LIST *my_list,  char *division)
{
     REQUEST_COURSE_LIST *request_list;
     char file[MAX_PATH +1];
     int no_request =0;
     

     get_request_list_filename(division, file);      /* shared_course_request_xml.h */
     if(file_exists(file))
     {
        request_list = xml_request_course_parser(file); /* shared_course_request_xml.h */
        if(request_list->head)
           no_request = has_existing_request(request_list, my_list);
        free_request_list(request_list);                 /* shared_course_request_xml.h */
     }
     else
	return -1;
     return no_request;
}    

void set_division_readingname(char *division)
{
     char temp[MAX_PATH +1];
     char *ptr;
     int i = 0;
     int found =0;

     for(ptr = division; *ptr && *ptr != '.'; ptr++)
     {
            if(*ptr == '_')
              temp[i] = ' ';
            else if(*ptr == '-' && !found)
            {
              temp[i] = ' ';
              found = 1;
            }
            else 
              temp[i] = *ptr;
           temp[i +1] = '\0';
           i++;
     } 
     cs_set_value("division_read", temp);
}


int main()
{
   char key[MAX_KEY + 1];
   char path[MAX_PATH + 1];
   char division[MAX_PATH + 1];
   XML_COURSE_INFO_LIST *list;    /* shared_roster_xml_parser.h */
   SESSION user;
   SUB_DIR_ID_NODE *unique_dir_list;
   SUBDIR_COURSE_LIST *sub_list;
   XML_COURSE_INFO_LIST *my_list;    /* shared_roster_xml_parser.h */
   char url[MAX_PATH +1];
   int has_new_course = 0, no_request = 0;

   cs_cgi_init(); 
   read_parameters(key, path, division);
   validate_server_key(key, &user);   /* shared_util.c */ 
   
   list = build_xml_data_list(path);

   unique_dir_list = build_unique_subdir_list(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 */

   my_list = get_my_course_list(list, &user);
   free_xml_data_list(list);                        /* shared_roster_xml_parser.c */

   if(!my_list->head)
     cs_critical_error(ERR_YOU_DONT_HAVE_COURSE, "");

   has_new_course = list_courses(my_list, key, "", sub_list);  /* shared_course_info.c */
   if(has_new_course)
   {
        no_request = check_existing_request(my_list, division);
        if(no_request == 0)
	   cs_set_int_value("no_submit", 1);	
	else if(no_request == -1)
	   cs_set_int_value("use_total_count", 1);	
	   
   }

   snprintf(url, MAX_PATH +1, "%s?id=%s", "admin_add_course_form", key);
   cs_set_value("back_url", url);
   set_division_readingname(division);

   free_subdir_course_list(sub_list);                    /* shared_course_info.c */
   free_xml_data_list(my_list);                          /* shared_roster_xml_parser.c */
   cs_set_value("instructor", user.realname);
   cs_cgi_display("admin_get_course_list", 1);
   cs_cgi_destroy();
   return 0;
}
    



	       
