#include <stdio.h>
#include <string.h>
#include "global.h"
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_super_list.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_new_course_lock.h"
#include "manhat-lib/shared_encrypt.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_menu.h"

DEL_COURSE_NODE *
read_parameters( char *password, char *key)
{

  DEL_COURSE_NODE *head;    /* shared_super_util.h */

  cs_get_required_parameter ("id", key, MAX_KEY );           
  cs_get_required_parameter ("password", password, MAX_PASSWORD);
  head = build_selected_course_list();    /* shared_super_util.c */
  
  return head;
}


void
set_delete_info( CONFIG_STRUCT *conf, P_NODE *head , int num_course)
{
   #define MAX_TMP_NAME 60
    P_NODE *ptr;
    char name[MAX_TMP_NAME]; 
    int i =0;
     
   snprintf(name, MAX_TMP_NAME, "del_course.%d.title", num_course);
   cs_set_value(name, conf->title);
   snprintf(name, MAX_TMP_NAME, "del_course.%d.semester", num_course);
   cs_set_value(name, conf->semester);
   snprintf(name, MAX_TMP_NAME, "del_course.%d.instructor", num_course);
   cs_set_value(name, conf->instructor);

   for(ptr = head; ptr; ptr=ptr->next)
   {
      snprintf(name, MAX_TMP_NAME, "del_course.%d.std.%d.username", num_course, i);
      cs_set_value(name, ptr->data.username);
      snprintf(name, MAX_TMP_NAME, "del_course.%d.std.%d.realname", num_course, i);
      cs_set_value(name, ptr->data.realname);
      snprintf(name, MAX_TMP_NAME, "del_course.%d.std.%d.id", num_course, i);
      cs_set_value(name, ptr->data.id);
      i++;
   }
   snprintf(name, MAX_TMP_NAME, "del_course.%d.count", num_course);
   cs_set_int_value(name, i);

   #undef MAX_TMP_NAME 
}





void
delete_course_from_student(char *crs, int num_course)
{
    CONFIG_STRUCT conf;
    P_NODE *head, *ptr;
    char course_path[MAX_PATH +1];
    COURSE_LIST_NODE *course_head;

    read_configuration_file (crs, &conf);          /*shared_util.c*/
    head = build_option_person_list (&conf);       /* shared_person_list.c */

    if(!conf.standalone)
    {

      for(ptr = head; ptr; ptr = ptr->next)
      {
        snprintf(course_path, MAX_PATH, "../%s/%c/%s/%s", 
			USERS_DIR, sub_dir_name((ptr->data.username)), ptr->data.username, COURSE_LIST_FNAME);
        course_head = excluding_delete_course_list(course_path, crs); /* shared_super_list.c */
        write_to_course_file(course_head, course_path);               /* shared_super_list.c */
        super_free_course_list(course_head);                          /*shared_super_list.c */
      }
    }
   snprintf(course_path, MAX_PATH, "%s%s", *crs == 'T' ? TEMPLATE_PATH : COURSE_PATH, crs);
   set_delete_info( &conf, head, num_course);
   kill_directory(course_path);  /* shared_file_util.c */
   free_option_person_list (head);  /* shared_person_list.c */
}




void
set_delete_courses(DEL_COURSE_NODE *head, const char *key)
{
    DEL_COURSE_NODE *ptr;
    char url[MAX_PATH +1];
    int i =0;

    snprintf(url, MAX_PATH +1, "%s?id=%s", "super_admin_page", key);
    cs_set_value("back_url", url);
    cs_set_current_time();

    for(ptr = head; ptr; ptr=ptr->next)
    {
      delete_course_from_student(ptr->del_id, i);
      i++;
    }
    
}
int
main()
{
    char password[MAX_PASSWORD +1];
    char key[MAX_KEY +1];
    SESSION user;
    DEL_COURSE_NODE *head;
    
    cs_cgi_init();
    head = read_parameters(password,  key);
    validate_super_key(key, &user);     		/* shared_super_util.c */

    set_admin_page(&user, key, 0, "super_delete_course_form"); /* shared_super_menu.c */
    if(confirm_super_password(password, user.username))      		/* shared_encrypt.c */
      {
       get_new_course_lock();                   /* shared_new_course_lock.c */
       set_delete_courses(head, key );
       free_new_course_lock();      /* shared_new_course_lock.c */
      } 
    else
       cs_critical_error(ERR_USERNAME_PASSWD_INCORRECT, "");
    free_del_list(head);
    cs_cgi_display("super_delete_course", 1);
    cs_cgi_destroy();
    return 0;
}

