#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_super_list.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_user_index.h"
#include "manhat-lib/shared_search_profile.h"
#include "manhat-lib/shared_person_list.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_menu.h"

static P_NODE*
get_new_person_node(const char *str)
{
   P_NODE *ptr;

   ptr = (P_NODE*)malloc(sizeof(P_NODE));
   if(!parse_line(str, ptr))  /* shared_search_profile.h */
      cs_critical_error(ERR_PASS_PARAMETERS_ERROR_MSG,  "");
   ptr->next =0;
   return ptr;
}


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

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


static P_NODE *
read_parameters(char *key)
{
        cs_get_required_parameter("id", key, MAX_KEY);
	return build_list();
}





int 
verify_no_courses(const char *user_dir)
{
 char path[MAX_PATH + 1];
 FILE *fp;
 COURSE_LIST_NODE *head;   /* shared_super_list.h */
 
  snprintf(path, MAX_PATH + 1, "%s/%s", user_dir, COURSE_LIST_FNAME);
  fp = fopen(path, "r");
  if(!fp)
      return 1;   /* no course_list file */
  get_shared_lock(fileno(fp), 1);      /* shared_lock.c */
      
  head = build_course_list(fp);     /* shared_super_list.c */
  
  release_lock(fileno(fp));   /* shared_lock.c */
  fclose(fp);
  
  if(head)
    {
     super_free_course_list(head);   /* shared_super_list.c */
     //critical_error(L109_USER_STILL_HAS_CLASSES_ALERT, ERROR_CHECKLIST_IMAGE);
     return 0;
    }
   return 1;
}     




static void
set_results(P_NODE *head, int count)
{
#define MAX_TMP_NAME 30
  P_NODE *ptr;
  int i =0;
  char name[MAX_TMP_NAME];

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



void
remove_user( const char *key, P_NODE *head, int *count)
{
  char dir_path[MAX_PATH +1];
  P_NODE *ptr;
  

  for(ptr = head; ptr; ptr = ptr->next)
  {
    if(strlen(ptr->data.username) >4)
    {
       snprintf(dir_path, MAX_PATH +1,  "../%s/%c/%s", 
			USERS_DIR,sub_dir_name(ptr->data.username), ptr->data.username);
			
       if(verify_no_courses(dir_path))
      {
	 strncpy(ptr->data.lastname, "0", MAX_NAME +1);
         kill_directory (dir_path);    /* shared_file_util.c */
	 (*count)++;
      }
      else
	 strncpy(ptr->data.lastname, "1", MAX_NAME +1);
    }
  } 
  snprintf(dir_path, MAX_PATH + 1, "../%s", USERS_DIR);
  
  reindex_users(dir_path);    /* shared_user_index.c */
  
}





int 
main(void)
{
	char key[MAX_KEY +1];
	SESSION user;
        P_NODE *head;
        int count =0;

        cs_cgi_init();
	head = read_parameters(key);
	validate_super_key(key, &user);
	set_admin_page(&user, key, 0, "super_info_form");  /* shared_super_menu.c */
	remove_user (key, head, &count);
        set_results(head, count);
        free_option_person_list (head);
	cs_cgi_display("super_remove", 1);
	cs_cgi_destroy();
	return 0;
}

	
