#include <stdio.h>
#include <string.h>
#include <time.h>

#include "global.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_search_profile.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_teamstring.h"

typedef struct course_person_list
   {
	char dir[MAX_PATH +1];
	CONFIG_STRUCT conf;
	P_NODE *p_head;
	struct course_person_list *next;
   }COURSE_PERSON_LIST;



COURSE_PERSON_LIST *
get_new_node( char *crs)
{
   COURSE_PERSON_LIST *ptr;
   char *str_ptr;
  
   str_ptr = strchr(crs, ':');
   *str_ptr = '\0';

   ptr = (COURSE_PERSON_LIST*)malloc(sizeof(COURSE_PERSON_LIST));
   if(!ptr)
	cs_critical_error(ERR_MALLOC_FAILED, "");
   strncpy(ptr->dir, crs, MAX_PATH +1);
   *str_ptr = ':';
   ptr->p_head =0;
   ptr->next =0;
   return ptr;
}


COURSE_PERSON_LIST *
get_course_list()
{

   COURSE_PERSON_LIST *head =0, *current=0, *ptr;
   char *str;
   HDF *obj;

    
   str = hdf_get_value(global_cgi->hdf, "Query.course_list.0", 0);
   if(str)
   {
       obj = hdf_get_child(global_cgi->hdf, "Query.course_list");
       while(obj)
       {
          str = obj->value;
	  if(str && strlen(str))
	  {
	      ptr = get_new_node(str);
	      if(!head)
	         head = ptr;
	      else 
	         current->next = ptr;
	      current = ptr;
	   }
	   obj = obj->next;
        }

   }
   else
   {
      str = hdf_get_value(global_cgi->hdf, "Query.course_list", 0);
      if(!str)
	cs_critical_error(ERR_PARAM_MISSING, "course_list");
       head = get_new_node(str);
   } 
   return head;


}
    	
COURSE_PERSON_LIST *
read_parameters (char *key, char *crs, char *group)
{
   COURSE_PERSON_LIST *head;
   cs_get_required_parameter ("id", key, MAX_KEY);
   cs_get_required_parameter ("crs", crs, MAX_PATH );
   cs_get_required_parameter ("group", group, MAX_ID);
   head = get_course_list();
   return head;
}

COURSE_PERSON_LIST *
update_course_person_list(COURSE_PERSON_LIST *head)
{
   COURSE_PERSON_LIST *ptr;
   for(ptr = head; ptr; ptr = ptr->next)
   {
      read_configuration_file (ptr->dir, &(ptr->conf));      /* shared_util.c */
      ptr->p_head = build_option_person_list(&(ptr->conf));
   }
   return head;
}



void
free_course_person_list(COURSE_PERSON_LIST *head)
{
    COURSE_PERSON_LIST *ptr;
    while(head)
    {
        ptr = head->next;
	free_option_person_list(head->p_head);
	free(head);
	head = ptr;
    }
}






void
set_head( char *key, char *crs,  char *group, CONFIG_STRUCT *conf)
{

   cs_set_course_info(conf); 
   cs_set_current_time(); 
   cs_set_value("add_type",!strcmp(group,"student")? "student":"faculty");
   cs_set_value("id", key);
   cs_set_value( "crs", crs);
   cs_set_value( "group", group);
   

}
 


void
set_one_course(COURSE_PERSON_LIST *one_course, P_NODE *current_list, const char *key, const char *crs, const char *group, int i, int *count)
{
   #define MAX_TMP_NAME 30
   P_NODE *ptr;
   int find =0, j =0;
   char name[MAX_TMP_NAME];
   char url[MAX_PATH +1];

   snprintf(name, MAX_TMP_NAME, "course.%d.course_no", i);
   cs_set_value(name, one_course->conf.course_no);

   snprintf(name, MAX_TMP_NAME, "course.%d.title", i);
   cs_set_value(name, one_course->conf.title);
   snprintf(name, MAX_TMP_NAME, "course.%d.dir", i);
   cs_set_value(name, one_course->dir);

   for(ptr = one_course->p_head; ptr; ptr = ptr->next)
   {
        find = find_user_in_course(current_list, ptr->data.username); /*shared_search_profile.h */
        
        snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.in_class", i, j);
        cs_set_int_value(name, find? 1 : 0);
        
       if(find)
	{
           snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.url", i, j);
           snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s&amp;open=1&amp;username=%s", "admin_list_passwd", key, crs,ptr->data.username);
	   cs_set_value(name, url); 
	}
        snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.username", i, j);
        cs_set_value(name, ptr->data.username); 
        snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.realname", i, j);
        cs_set_value(name, ptr->data.realname); 
        snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.id", i, j);
        cs_set_value(name, ptr->data.id); 
        if(!find)
	{
          snprintf(name, MAX_TMP_NAME, "course.%d.user.%d.team", i, j);
          if(strcmp(group, "student") == 0)
           cs_set_value(name, "A"); 
	  else
           cs_set_value(name, "Z"); 
	} 
        j++;	
    }	   
}

void set_all_person_in_courses(COURSE_PERSON_LIST *head, CONFIG_STRUCT *conf,
		const char *key, const char *crs, const char *group)
{
    COURSE_PERSON_LIST *ptr;
    int i =0, count =0;
    P_NODE *crs_person = build_option_person_list(conf);   /* shared_person_list.c */

    for(ptr = head; ptr; ptr = ptr->next)
    {
       set_one_course(ptr, crs_person, key, crs, group, i, &count);
       i++;
    }
      
   free_option_person_list(crs_person);    /* shared_person_list.c */
   cs_set_int_value("count", count); 
}

		
void
do_action( char *key,  char *crs, char *group, 
		CONFIG_STRUCT *conf, COURSE_PERSON_LIST *head)
{

   set_head(key, crs, group, conf);
   set_all_person_in_courses(head, conf, key, crs, group);
   cs_set_team();   /* shared_teamstring.c  */
}




int
main()
{
  char key[MAX_KEY +1];
  char crs[MAX_PATH + 1];   /* from command line - which program to pass form selections to? */
  SESSION user;
  char group[MAX_ID +1];
  CONFIG_STRUCT conf;
  COURSE_PERSON_LIST *head;

  cs_cgi_init();
  head = read_parameters (key, crs, group);
  read_configuration_file (crs, &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))
  {
     
     head = update_course_person_list(head);
     do_action(key, crs, group, &conf, head);
  }
  else
         access_denied_error();                   /* shared_access.c */
   
   
  free_course_person_list(head);
  cs_cgi_display("admin_list_students_in_courses", 1);
  cs_cgi_destroy();
  return 0;
}

