#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "global.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_update_user_info.h"
#include "manhat-lib/shared_delete.h"
#include "manhat-lib/shared_drop_add_list.h"


static void
read_parameters (char *course, char *key)
{

  cs_get_required_parameter ("crs", course, MAX_FILENAME);	/* shared_cgi_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);

}

void set_add_student_list(const char *path)
{
     FILE *fp;
     struct stat buff;
     char *data =0;

     if(stat(path, &buff))
	cs_critical_error(ERR_STAT_FAILED, "set_add_student_list");

     fp = fopen(path, "r");
     if(!fp)
	cs_critical_error(ERR_FOPEN_READ_FAILED, "set_add_student_list");
     data = (char *) malloc(sizeof(char) * buff.st_size +1);
     if(!data)
	cs_critical_error(ERR_MALLOC_FAILED, "set_add_student_list");
     if(!fread(data, buff.st_size, 1, fp))
	cs_critical_error(ERR_FOPEN_READ_FAILED, "set_add_student_list");
     fclose(fp);
     *(data + buff.st_size) = '\0';
     cs_set_value("add_list", data);
     free(data);

}


int set_drop_student_list(const char *path, CONFIG_STRUCT *conf)
{
#define MAX_TMP 30
    P_NODE *head = 0, *ptr;
    char time_string[MAX_PATH +1];
    char name[MAX_TMP];
    int i = 0;
	P_NODE *curr_head;

	curr_head = build_option_person_list (conf);
    head = build_drop_person_list (path, time_string, MAX_PATH +1);
    if(head)
    {
       cs_set_value("drop_time_string", time_string);
       for(ptr = head; ptr; ptr= ptr->next)
       {
         if(find_student_in_passwd(curr_head, ptr))
		 {
	        snprintf(name, MAX_TMP, "user.%d.id", i);
	        cs_set_value(name, ptr->data.id);
	        snprintf(name, MAX_TMP, "user.%d.username", i);
	        cs_set_value(name, ptr->data.username);
	        snprintf(name, MAX_TMP, "user.%d.realname", i);
	        cs_set_value(name, ptr->data.realname);
	        snprintf(name, MAX_TMP, "user.%d.group", i);
	        if(ptr->data.group == FACULTY)
	           cs_set_value(name, "faculty");
	        else
	         cs_set_value(name, "student");

	       i++;
          } 
	   }
    }
	if(i == 0)
	{
	    unlink(path);
		cs_set_int_value("no_list_msg", 1);
	}
    return i;
#undef MAX_TMP
}



P_NODE *get_drop_list()
{
#define MAX_TMP 40
     int number_user;
     P_NODE *head = 0, *current =0, *ptr;
     char name[MAX_TMP];
     char temp[MAX_TMP];
     int i;

     number_user = hdf_get_int_value(global_cgi->hdf, "Query.number_user", 0);
     if(number_user == 0)
	   return 0;
     for(i = 0; i<number_user; i++)
     {
	ptr = (P_NODE *) malloc(sizeof(P_NODE));
	if(!ptr)
	    cs_critical_error(ERR_MALLOC_FAILED, "get_drop_list");

        snprintf(name, MAX_TMP, "userid_%d",i);
        cs_get_required_parameter (name, ptr->data.id, MAX_ID);	/* shared_cgi_util.c */
        snprintf(name, MAX_TMP, "username_%d",i);
        cs_get_required_parameter (name, ptr->data.username, MAX_USERNAME);	/* shared_cgi_util.c */
        snprintf(name, MAX_TMP, "realname_%d",i);
        cs_get_required_parameter (name, ptr->data.realname, MAX_NAME);	/* shared_cgi_util.c */
        snprintf(name, MAX_TMP, "group_%d",i);
        cs_get_required_parameter (name, temp, MAX_TMP -1);	/* shared_cgi_util.c */

	if(strcmp(temp, "faculty") == 0)
	{
	     ptr->data.group = FACULTY;
	     ptr->data.team = 'Z';
	}
	else
	{
	     ptr->data.group = STUDENT;
	     ptr->data.team = 'A';
	}

        snprintf(name, MAX_TMP, "choice_%d",i);
        cs_get_required_parameter (name, ptr->data.lastname, MAX_NAME);	/* shared_cgi_util.c */
	strncpy(ptr->data.alias, "anonymous", MAX_NAME +1);
        ptr->next = 0;
	if(!head)
	    head = ptr;
	else
           current->next = ptr;
        current = ptr;	

     }	
     return head; 
#undef MAX_TMP

}

void drop_action(CONFIG_STRUCT *conf,  char *crs)
{
   P_NODE *head =0, *ptr;
   char temp[MAX_NAME +1];
   char dirname[MAX_PATH +1];
   int deleted = 0;
   P_NODE *curr_head;

   curr_head = build_option_person_list (conf);
   head = get_drop_list();
   if(head)
   {
       for(ptr = head; ptr; ptr = ptr->next)
       {
         if(find_student_in_passwd(curr_head, ptr))
		 {
           if(!strcmp(ptr->data.lastname, "keep"))   /* add * in front of realname to indicate ok */
	       {
               snprintf(temp, MAX_NAME +1, "*%s", ptr->data.realname);
               snprintf(ptr->data.realname, MAX_NAME +1, "%s", temp);
	           mark_passwd_file(ptr->data.username, conf);
	          update_passwd(&(ptr->data), conf);

	       }
           else if(!strcmp(ptr->data.lastname, "keep_normal"))   /* add * in front of realname to indicate ok */
	       {
               snprintf(temp, MAX_NAME +1, "+%s", ptr->data.realname);
               snprintf(ptr->data.realname, MAX_NAME +1, "%s", temp);
	           mark_passwd_file(ptr->data.username, conf);
	          update_passwd(&(ptr->data), conf);
              }
	       else /* delete user from classromm */
	       {
	           delete_course(ptr->data.username, crs);    /* delete course in central course list
					            file - shared_delete.c */

	           mark_passwd_file (ptr->data.username, conf);
		       deleted = !user_contributed (ptr->data.username, conf);  /* shared_delete.c */
		       if (deleted)
		       {
		            snprintf (dirname, MAX_PATH + 1, "%s%s/%s",
		                 conf->course_path, PEOPLE_DIR, ptr->data.username);
		            remove_directory (dirname);
		       }


	       }
        }
	  }
   }
   free_option_person_list (head);
   free_option_person_list (curr_head);


}



void do_action(CONFIG_STRUCT *conf,  char *crs, const char *key)
{
     char path[MAX_PATH +1];
     char *add_confirm = 0;
     char *drop_confirm = 0;

     cs_set_course_info(conf);
     
    
     snprintf(path, MAX_PATH +1, "%s%s", conf->course_path, ADD_LIST_FNAME);
     if(file_exists(path))
     {
          add_confirm = hdf_get_value(global_cgi->hdf, "Query.add_confirm", 0);
	     if(add_confirm)
	     {
	        unlink(path);
            snprintf(path, MAX_PATH +1, "%s%s", conf->course_path, DROP_LIST_FNAME);
            if(file_exists(path))
			{
	            if(!set_drop_student_list(path, conf))
	               printf("Location: %s?crs=%s&id=%s\n\n", "main_menu", crs, key);
			} 
	        else
	           printf("Location: %s?crs=%s&id=%s\n\n", "main_menu", crs, key);
	       return;
	    }
	   else
	   {
	       set_add_student_list(path);
	       return;
	    }
     }
     snprintf(path, MAX_PATH +1, "%s%s", conf->course_path, DROP_LIST_FNAME);
     if(file_exists(path))
     {
        drop_confirm = hdf_get_value(global_cgi->hdf, "Query.drop_confirm", 0);
        if(drop_confirm)	
	    {
	       drop_action(conf, crs);
	       unlink(path);
	       printf("Location: %s?crs=%s&id=%s\n\n", "main_menu", crs, key);
	    }
	   else
	   {
           set_drop_student_list(path, conf);
	    return;
     }
  }
}

int
main (void)
{
  SESSION user;
  CONFIG_STRUCT conf;		/* the configuration read from config file */
  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */

  cs_cgi_init();
  read_parameters (course, key);

  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);          /* shared_util.c */
  do_action(&conf, course, key);

  cs_cgi_display("show_add_drop", 1);
  cs_cgi_destroy();
  return 0;

}



