#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>

#include "global.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_course_search.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_menu.h"
	

void
read_parameters(  char *key, char *subdir,  char *from)
{
    
    cs_get_required_parameter ("id", key, MAX_KEY);
    cs_get_required_parameter ("subdir", subdir, MAX_FILENAME);
    cs_get_required_parameter ("from", from, MAX_FILENAME);
    
    
}



void
write_group_description(const char *subdir)
{
 char description[MAX_SEMESTER + 1];
 FILE *fp;
 char full_path[MAX_PATH + 1];
 char *str;
 
 cs_get_required_parameter("description", description, MAX_SEMESTER);  
    
 snprintf(full_path, MAX_PATH + 1, "%s%s/%s", *subdir == 'T'? TEMPLATE_PATH:COURSE_PATH , subdir, GROUP_DESCRIPTION_FNAME);
 fp=fopen(full_path, "w");
 if(!fp)
   cs_critical_error(ERR_FWRITE_FAILED, "");

 fprintf(fp, "%s\n",description);
 str = hdf_get_value(global_cgi->hdf, "Query.notes", 0);
 if(str)
    fprintf(fp, "%s", str);
 fclose(fp);
}


static
int empty_course_subdir(const char *subdir)
{
 char full_path[MAX_PATH + 1];
 DIR *dir_p;
 struct dirent *dir_entry_p;
 int directory_found = 0;
  
 snprintf(full_path, MAX_PATH + 1, "%s%s", *subdir == 'T'? TEMPLATE_PATH:COURSE_PATH , subdir); 
 dir_p = opendir(full_path);
 if(!dir_p)
    cs_critical_error(ERR_COURSE_GROUP_NOT_FOUND, "");

 while (NULL != (dir_entry_p = readdir (dir_p)))
  {
         if( (*(dir_entry_p->d_name)) != '.')
          {
           if(isdirectory(full_path, dir_entry_p->d_name))
             {
               directory_found = 1;
               break;
             }    
          }
  }  
 closedir(dir_p); 
 return !directory_found;
} 
  





static void
set_description_form ( const char *key,  const char *subdir, const char *from)
{
 #define MAX_TMP_NAME 30
 FILE *fp;
 char full_path[MAX_PATH + 1];
 char line[300];
 int subdir_empty, i =0;
 char name[MAX_TMP_NAME];
 
 subdir_empty = empty_course_subdir(subdir);
 cs_set_int_value("subdir_empty", subdir_empty);
 snprintf(full_path, MAX_PATH + 1, "%s%s/%s", *subdir == 'T'? TEMPLATE_PATH:COURSE_PATH , subdir, GROUP_DESCRIPTION_FNAME);
 fp=fopen(full_path, "r");
 
 *line = '\0'; 
 if(fp)
  {
   fgets(line, 300, fp);
   strtrm(line);   /* shared_strtrm.c */
   cs_set_value("desc", line);
  }
 if(fp)
   {
        while(fgets(line,300,fp))
	{
	   snprintf(name, MAX_TMP_NAME, "note.%d", i);
           cs_set_value(name, line);
	   i++;
	}
   }      
 if(fp)
   fclose(fp);
#undef MAX_TMP_NAME 
}






    
static
void delete_empty_subdir(const char *subdir)
{
char path[MAX_PATH + 1];

/* delete the description text file */
 snprintf(path, MAX_PATH + 1, "%s%s/%s", *subdir == 'T'? TEMPLATE_PATH:COURSE_PATH , 
                                                                subdir, GROUP_DESCRIPTION_FNAME);
 unlink(path);
 
 /* try to remove the directory */
 snprintf(path, MAX_PATH + 1, "%s%s", *subdir == 'T'? TEMPLATE_PATH:COURSE_PATH ,subdir);
 rmdir(path);
} 
                                                                






int
main ()
{
  char key[MAX_KEY + 1];	   /* from id=? command line */
  char subdir[MAX_FILENAME + 1];   /* from subdir=? command line */
  char from[MAX_FILENAME + 1];     /* from=? command line */
  SESSION user;
  char *update;                     /* from update=? command line */ 
  char  *delete;			  /* from delete=? command line */

  cs_cgi_init();  
  read_parameters ( key, subdir, from);
  validate_super_key (key, &user);	/* shared_super_util.c*/
  set_admin_page(&user, key, 0, "super_admin_page"); /* shared_super_menu.c */
  
  update = hdf_get_value(global_cgi->hdf, "Query.submit", 0);
  delete = hdf_get_value(global_cgi->hdf, "Query.delete", 0);
  if(update || delete)
    {
     if(update)
         write_group_description(subdir);
     else
         delete_empty_subdir(subdir);
             
     printf("Location: %s?id=%s\n\n", from,key);
    }
  else
   {   
    set_description_form(key, subdir, from);
    cs_cgi_display("super_group_description", 1);
   } 
   cs_cgi_destroy(); 
  return 0;			/* exit successfully */

}

