#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>

#include "ClearSilver.h"
#include "global.h"
#include "grade.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_grades_util.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_file_util.h"

static void
delete_files (const char *username, CONFIG_STRUCT *conf)
{
  char fname[MAX_PATH + 1];

  snprintf (fname, MAX_PATH + 1, "%speople/%s/grades/%s", conf->course_path,
	    username, GRADES_NEW_FNAME);
  unlink (fname);
}



static void
delete_student_files (CONFIG_STRUCT *conf, P_NODE *head)
{

 P_NODE *ptr;
 
  for (ptr = head; ptr; ptr = ptr->next)
    if(ptr->data.group == STUDENT)
        delete_files (ptr->data.username, conf);
}


static void
delete_teacher_files (CONFIG_STRUCT *conf, P_NODE *head, const char *course, const char *key)
{

  char fname[MAX_PATH + 1];
  char url[MAX_PATH + 1];
  P_NODE *ptr;
  
  for(ptr=head; ptr; ptr=ptr->next)
    if(ptr->data.group == FACULTY)
     {
       update_grades_inf (conf, ptr->data.username);    /* shared_grades_util.c */

       if(conf->grades == 1)
       {
          snprintf (fname, MAX_PATH + 1, "%s%s/%s", conf->course_path,"grade_book", GRADES_TAB_FNAME);
          snprintf (url, MAX_PATH + 1, "%s?crs=%s&id=%s", "grades_fac_menu", course, key);
	  cs_set_value("back_url", url);
       }
       else if(conf->grades == 3)
       {
          snprintf (fname, MAX_PATH + 1, "%s%s/%s", conf->course_path,"grade_book", "gradequick");
          snprintf (url, MAX_PATH + 1, "%s?crs=%s&id=%s", "grades_fac_menu", course, key);
	  cs_set_value("back_url", url);
       }
       else
          snprintf (fname, MAX_PATH + 1, "%s%s/%s", conf->course_path,"grade_book", GRADE_BOOK_STUDENT_DB);
       if(conf->grades == 1 || conf->grades == 2)
         unlink (fname);
       else
	  kill_directory(fname);

       snprintf (fname, MAX_PATH + 1, "%speople/%s/grades/%s", conf->course_path,
	                                                    ptr->data.username, GRADES_INF_FNAME);
       unlink (fname);
       snprintf (fname, MAX_PATH + 1, "%speople/%s/grades/%s", conf->course_path,
	                                                    ptr->data.username, GRADES_TAB_FNAME);
       unlink (fname);
     }

}


int
delete_action ( CONFIG_STRUCT *conf, SESSION *user, const char *course, const char *key)
{
   P_NODE *person_head;
   char *check1 =0;

   check1 = hdf_get_value(global_cgi->hdf, "Query.val", 0);
   if( check1 )
   { 
     person_head = build_option_person_list(conf);   /* shared_person_list.c */
     delete_student_files (conf, person_head);
     delete_teacher_files (conf, person_head, course, key);
     free_option_person_list(person_head);      /* shared_person_list.c */
    return 1;
   }
  return 0;
}


	
void
read_parameters ( char *course, char *key)
{
   cs_get_required_parameter("crs", course, MAX_PATH);
   cs_get_required_parameter("id", key, MAX_KEY);
}


int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];
  SESSION user;
  CONFIG_STRUCT conf;

  cs_cgi_init();     /* shared_cs_util.c */
  read_parameters (course, key);
  read_configuration_file (course, &conf);	
  validate_key (key, &user, &conf);	

  if(user.group != FACULTY)
      cs_critical_error(ERR_REQUEST_DENIED, "");
  if(!has_write_permission(&user, &conf))   /* shared_access.c */
      cs_critical_error(ERR_REQUEST_DENIED, "");

  if(!delete_action( &conf, &user, course, key))
      cs_critical_error(ERR_MISS_CHECKBOXES, "grades_delete");
  if(conf.grades == 1)  
     printf("Location: %s?crs=%s&id=%s\n\n", "grades_fac_menu", course, key);
  else
  {
     cs_set_course_info(&conf);
     cs_set_int_value("success_delete", 1);
     cs_cgi_display("grades_delete_form", 1); 
  }
  cs_cgi_destroy();                   

  return 0;			/* exit successfully */

}

