#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_delete.h"
#include "manhat-lib/shared_course_info.h"
#include "manhat-lib/shared_update_user_info.h"
#include "manhat-lib/shared_authenticate.h"

   

void
buffer_standard_xml_file ( const char *roster_path, char **buffer )
{
 FILE *fp;
 struct stat statbuf;
 
 *buffer = (char *) 0;
 
 if (stat (roster_path, &statbuf)) 
 {
    cs_critical_error(ERR_FOPEN_READ_FAILED , "");
 }  

 *buffer = (char *) malloc((statbuf.st_size + 1)  * sizeof(char));
 if(!*buffer)
  {
   cs_critical_error (ERR_MALLOC_FAILED, "");
  }  
  
 fp = fopen(roster_path, "r");
 if(!fp)
  {
   cs_critical_error(ERR_FOPEN_READ_FAILED , "");
  } 
 
 if(fread(*buffer,statbuf.st_size,1,fp) != 1)
  {
    cs_critical_error(ERR_FREAD_FAILED, "");
  }

 fclose(fp);
 *(*buffer + statbuf.st_size) = '\0';   /* null terminate the string */
}


XML_COURSE_INFO_LIST *
build_xml_data_list( const char *roster_path)
{
   XML_COURSE_INFO_LIST *head;    /* shared_roster_xml_parser.h */
   char *buf;

   buffer_standard_xml_file(roster_path, &buf);
   head = xml_course_data_parser(buf);   /* shared_roster_xml_parser.c */
   free(buf);
   return head;
}






void
delete_users(XML_COURSE_INFO_LIST *list, SUBDIR_COURSE_LIST *sub_list)  
{
#define MAX_TMP_NAME 30
   int i =0, j=0;
   char name[MAX_TMP_NAME];
   char manhat_course_id[MAX_COURSE_ID +1];
   int found = 0;
   char path[MAX_PATH +1];
   COURSE_TYPE type;
   P_NODE *ptr;
   CONFIG_STRUCT conf;
   struct stat buff;
   int deleted  =0;

   if(list->head)
   {

      for(list->current = list->head;  list->current; list->current = list->current->next)
      {
        found = find_manhat_course_id(sub_list,  
			list->current->subdir,list->current->dir_id,manhat_course_id, &type);
	if(found)
	{
	   j = 0; 
           snprintf(name, MAX_TMP_NAME, "del_course.%d.id", i);
	   cs_set_value(name, manhat_course_id);
           snprintf(name, MAX_TMP_NAME, "del_course.%d.subdir", i);
	   cs_set_value(name, list->current->subdir);
           snprintf(name, MAX_TMP_NAME, "del_course.%d.org_id", i);
	   cs_set_value(name, list->current->dir_id);
           snprintf(name, MAX_TMP_NAME, "del_course.%d.no", i);
	   cs_set_value(name, list->current->course_code);
           snprintf(name, MAX_TMP_NAME, "del_course.%d.title", i);
	   cs_set_value(name, list->current->title);
           snprintf(name, MAX_TMP_NAME, "del_course.%d.term", i);
	   cs_set_value(name, list->current->semester);

	   snprintf (path, MAX_PATH + 1, "%s/%s",
	        list->current->subdir, manhat_course_id);

           read_configuration_file (path, &conf);

           for(ptr = list->current->user_head; ptr; ptr = ptr->next)
	   {
	      if(ptr->data.group != FACULTY)
	      {
	          snprintf(path, MAX_PATH +1, "%s%s/%s/people/%s", 
			  COURSE_PATH, list->current->subdir, manhat_course_id, ptr->data.username);
                  if(!stat(path, &buff))
                  {
	              snprintf(path, MAX_PATH +1, "%s/%s", 
			   list->current->subdir, manhat_course_id);
	              delete_course(ptr->data.username, path);    /* shared_delete.c */
	              mark_passwd_file (ptr->data.username, &conf);
	              deleted = !user_contributed (ptr->data.username, &conf);
                      snprintf(name, MAX_TMP_NAME, "del_course.%d.user.%d.name", i, j);
	              cs_set_value(name, ptr->data.realname);
                      snprintf(name, MAX_TMP_NAME, "del_course.%d.user.%d.lname", i, j);
	              cs_set_value(name, ptr->data.lastname);
                      snprintf(name, MAX_TMP_NAME, "del_course.%d.user.%d.username", i, j);
	              cs_set_value(name, ptr->data.username);
                      snprintf(name, MAX_TMP_NAME, "del_course.%d.user.%d.id", i, j);
	              cs_set_value(name, ptr->data.id);

	      
	              if (deleted)
	              {
                          snprintf(name, MAX_TMP_NAME, "del_course.%d.user.%d.no_con", i, j);
	                  cs_set_int_value(name, 1);
	                  snprintf (path, MAX_PATH + 1, "%s%s/%s",
	                                  conf.course_path, PEOPLE_DIR,ptr->data.username);
	                  remove_directory (path);
	              }
                      j++;
	          }
	     }
	      
	   }
       i++;
     }
   }
 }
#undef MAX_TMP_NAME 
}


void get_usernames(XML_COURSE_INFO_LIST *list)
{
    P_NODE *ptr;

    for(list->current = list->head;  list->current; list->current = list->current->next)
    {
	for(ptr = list->current->user_head; ptr; ptr = ptr->next)
	    derive_username (ptr->data.realname, ptr->data.lastname,ptr->data.id, 
			    ptr->data.username, MAX_USERNAME+1);  /* shared_authenticate.c */


    }

}
    

int main()
{
   char path[MAX_PATH + 1];  /* path to roster file containing XML data */
   XML_COURSE_INFO_LIST *list;    /* shared_roster_xml_parser.h */
   SUB_DIR_ID_NODE *unique_dir_list;
   SUBDIR_COURSE_LIST *sub_list;
  
   cs_cgi_init(); 
   cs_get_required_parameter ("path", path, MAX_PATH );    /* shared_cs_util.c */
   
   list = build_xml_data_list(path);
   get_usernames(list);
   unique_dir_list = build_unique_subdir_list(list);
   sub_list = build_sub_list_info(unique_dir_list);
   free_sub_list_info(unique_dir_list);
   delete_users(list, sub_list);
   free_subdir_course_list(sub_list);
   free_xml_data_list(list);
   cs_cgi_display("super_delete_drop", 0);
   cs_cgi_destroy();
   return 0;
}
    



	       
