#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>		/* for stat() */

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_delete.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_update_user_info.h"
#include "manhat-lib/shared_time.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_menu.h"

static P_NODE *
get_new_std_node(char *value)
{
   P_NODE *ptr;
   char *se_ptr, *se_ptr1;
   char temp[MAX_PATH +1];

   strncpy(temp, value, MAX_PATH +1);

   ptr = (P_NODE*)malloc(sizeof(P_NODE));
   if(!ptr)
     cs_critical_error(ERR_MALLOC_FAILED, "");
   se_ptr = strchr(temp, ':');
   if(se_ptr)
     *se_ptr = '\0';
   strncpy(ptr->data.username, temp, MAX_USERNAME+1);
   se_ptr++;
   se_ptr1 = strchr(se_ptr, ':');
   if(se_ptr1)
     *se_ptr1 = '\0';
    strncpy(ptr->data.realname, se_ptr, MAX_NAME +1);
    se_ptr1++;
    strncpy(ptr->data.id, se_ptr1, MAX_ID+1);
    ptr->next = 0;
   
    return ptr;
}

static P_NODE *
buid_delete_user_list()
{
 #define MAX_TMP_NAME 50
    P_NODE *head = 0, *current =0, *ptr;
    HDF *obj;
    char *str;

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

    return head;
 #undef MAX_TMP_NAME 
}

P_NODE *
read_parameters (char *course, char *key)
{
  P_NODE *head;
  cs_get_required_parameter ("crs", course, MAX_PATH);
  cs_get_required_parameter ("id", key, MAX_KEY);
  head = buid_delete_user_list();
  return head;
}

void
set_report_data (P_NODE *head, CONFIG_STRUCT *conf, const char *key, const char *course)
{
 #define MAX_TMP_NAME 50
  P_NODE *ptr;
  char url[MAX_PATH +1];
  char name[MAX_TMP_NAME];
  int i =0;

  snprintf(url, MAX_PATH +1, "%s?id=%s&amp;crs=%s", "admin_main_menu", key, course);
  cs_set_value("back_url", url);
  cs_set_current_time();
  cs_set_course_info(conf);

  for(ptr = head; ptr; ptr=ptr->next)
  {
     snprintf(name, MAX_TMP_NAME, "student.%d.id", i);
     cs_set_value(name, ptr->data.id);
     snprintf(name, MAX_TMP_NAME, "student.%d.username", i);
     cs_set_value(name, ptr->data.username);
     snprintf(name, MAX_TMP_NAME, "student.%d.realname", i);
     cs_set_value(name, ptr->data.realname);
     i++;
  }
 #undef MAX_TMP_NAME 
}




int
main ()
{

  char course[MAX_PATH + 1];	/* from crs=? command line */
  char key[MAX_KEY + 1];	/* from id=? command line */
  int deleted;			/* was the user's directory removed? */
  char dirname[MAX_PATH + 1];
  CONFIG_STRUCT conf;
  SESSION user;
  P_NODE *head, *ptr;
  
  cs_cgi_init();
  head = read_parameters (course, key);
  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);	/* shared_util.c */
  if(user.group != ADMIN)
	cs_critical_error(ERR_REQUEST_DENIED, "");

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

 set_report_data (head, &conf, key, course);
 free_option_person_list (head);
 cs_set_int_value("display_deleted_students", 1);
 cs_cgi_display("admin_del_student_form", 1);
 cs_cgi_destroy();
  return 0;
}

