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

#include "global.h"
#include "custom.h"		/* for date FORMAT strings */

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_login_logs.h"
#include "manhat-lib/shared_cs_util.h"


void
list_roster_logfiles(const char *key)
{
#define MAX_TMP_NAME 30

   char time_last_modified_string[MAX_TIMESTRING + 1];
   int i = 0;
   int count;
   char name[MAX_TMP_NAME];
   char url[MAX_PATH +1];

   LOGFILE_NODE *head, *log_ptr;

   head = build_logfile_list(&count, ROSTER_LOG_PATH, "log");   /* shared_login_logs.c */
   
   /* for each log file */   
    for(log_ptr = head, i = 0; log_ptr; log_ptr = log_ptr->next, i++)
     {
      /* save modification time of log file */
       strftime(time_last_modified_string, MAX_TIMESTRING+1, DOW_DATE_TIME_FORMAT, localtime(&(log_ptr->buf.st_mtime)));
       snprintf(name, MAX_TMP_NAME, "log.%d.last_time", i);
       cs_set_value(name, time_last_modified_string);
           
       snprintf(name, MAX_TMP_NAME, "log.%d.filename", i);
       cs_set_value(name, log_ptr->filename);  /* col 2 is the filename */
       
       snprintf(name, MAX_TMP_NAME, "log.%d.size", i);
       cs_set_int_value(name, (long)(log_ptr->buf.st_size));  /* col 2 is the filename */
	   
       snprintf(url, MAX_PATH +1, "%s?id=%s&amp;logfile=%s", "super_detail_roster_log",  key, log_ptr->filename);
       snprintf(name, MAX_TMP_NAME, "log.%d.detail_url", i);
       cs_set_value(name, url);

	     

       /* insert button to delete a log, but only for the oldest available log */  
       if( (i+1) == count && count !=1)
        {
           snprintf(name, MAX_TMP_NAME, "log.%d.delete_url", i);
           snprintf(url, MAX_PATH +1, "%s?id=%s&amp;logfile=%s&amp;del=yes", "super_detail_roster_log",  key, log_ptr->filename);
           cs_set_value(name, url);
        }                      
     }
     
   free_logfile_list(head);      /* shared_login_logs.c */

#undef MAX_TMP_NAME 
     
}






int
main ()
{
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user;
 
  cs_cgi_init(); 
  read_param ( key);
  validate_super_key (key,&user);	/* shared_super_util.c*/
  list_roster_logfiles(key); 
  cs_cgi_display("super_roster_log_list", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

