#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "time.h"
              
#include "global.h"
#include "custom.h"

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_key.h"
#include "manhat-lib/shared_super_menu.h"
#include "manhat-lib/shared_course_search.h"



static void
read_parameters (char *key)
{
  cs_get_required_parameter("id", key, MAX_KEY);
}


 
static void 
set_report_list_data(char *key)
{
 DIR *dir_p;
 struct dirent *dir_entry_p;
 char path[MAX_PATH + 1];
 char short_description[MAX_FILENAME + 1];   /* seems the len of this is often MAX_SEMESTER - must be at least that long */
 struct stat buf;
 int group_count = 0;
 char name[50];
 char value[200];
 
 
 dir_p = opendir (COURSE_PATH);
 if (dir_p)
  { 
   while (NULL != (dir_entry_p = readdir (dir_p)))
       {  
        if (strcmp (dir_entry_p->d_name, ".")
	      && strcmp (dir_entry_p->d_name, ".."))
        {
          snprintf(path, MAX_PATH + 1, "%s%s", COURSE_PATH, dir_entry_p->d_name);
          if(!stat(path, &buf) && S_ISDIR (buf.st_mode))   /* then this must be a course group directory */
               {
                 snprintf(name, 50, "group.%d.name", group_count);    /* 'name' is the internal name of the directory' */
                 cs_set_value(name, dir_entry_p->d_name);
                 
                 get_short_group_description(dir_entry_p->d_name, short_description);  /* shared_course_search.c */
                 snprintf(name, 50, "group.%d.description", group_count);    /* short description of this group */
                 cs_set_value(name, short_description);
                 
                 
                 snprintf(name, 50, "group.%d.describe_url", group_count);    /* url to program to change description of this group */
                 snprintf(value,200, "super_group_description?id=%s&amp;subdir=%s&amp;from=super_list_usage_reports",
                                        key, dir_entry_p->d_name);
                 
                 cs_set_value(name, value);
                 
                 snprintf(name, 50, "group.%d.create_report_url", group_count);    /* url to program to create a new report */
                 snprintf(value,200, "super_analyze_course_group?id=%s&amp;subdir=%s",
                                        key, dir_entry_p->d_name);
                 
                 cs_set_value(name, value);
                 
                 
                 /* set values for existing report, if any */
                 snprintf(path, MAX_PATH + 1, "%s%s/%s", COURSE_PATH, dir_entry_p->d_name, USAGE_REPORT_FNAME);
                 if(!stat(path, &buf))   
                  {
                    snprintf(name, 50, "group.%d.report_date", group_count);

                    /* DOW_DATE_TIME_FORMAT is #defined in custom.h */
                    strftime (value, 200, DOW_DATE_TIME_FORMAT, localtime (&buf.st_mtime));
                    cs_set_value(name, value);
                    
                    snprintf(name, 50, "group.%d.view_report_url", group_count);
                    snprintf(value, 200, "super_view_usage_report?id=%s&amp;subdir=%s",
                                                 key, dir_entry_p->d_name);
                    cs_set_value(name, value);                             
                  }
  
                group_count++;
               }
        }
       }   /* while  */
  } /* if dir_p */
}  


 
int
main()
{
 char key[MAX_KEY + 1];
 SESSION user;

 cs_cgi_init();   
 read_parameters(key);


 validate_super_key(key, &user);
 set_admin_page(&user, key, 0, "super_list_usage_reports");  /* shared_super_menu.c */
 set_report_list_data(key);
 
 cs_cgi_display ("super_list_usage_reports", 1);    /* shared_cs_util.c */
 cs_cgi_destroy();

 return 0;

}

