#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_authenticate.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"
#include "manhat-lib/shared_super_menu.h"


void
list_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, LOG_PATH, "log");   /* shared_login_logs.c */
   
   /* print button to get browser/OS graphs **/
   snprintf(url, MAX_PATH +1, "%s?id=%s&amp;logfile=%s","super_login_graphs",  key, "all");
   cs_set_value("all_url", url);

   /* print button to get raw data */
    snprintf(url, MAX_PATH +1,"%s?id=%s&amp;logfile=%s", "super_log_data",  key, "all");
   cs_set_value("all_g_url", url);
                                       

   /* print button to show ADMIN: logins */
    snprintf(url, MAX_PATH +1, "%s?id=%s&amp;username=__admin__&amp;logfile=all",  "super_log_data",  key);
   cs_set_value("all_admin_url", url);
      
   
   /* 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);

 
          /* the first log file gets a label stating that it's the most recent or current
          ** log file.  The second file starts the list of rotated logs, and is labelled
          ** accordingly
          */
	   if(i==0 || i ==1)
	   {
             snprintf(name, MAX_TMP_NAME, "log.%d.history", i);
	     cs_set_int_value(name, i);
	   }
           
           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 */
          
           /* col 5 - print button to get browser/OS graphs */
	   snprintf(url, MAX_PATH +1, "%s?id=%s&amp;logfile=%s", "super_login_graphs",  key, log_ptr->filename);
           snprintf(name, MAX_TMP_NAME, "log.%d.g_url", i);
           cs_set_value(name, url);

	  /* col 6 - print button for raw listing of log data */	 
           snprintf(url, MAX_PATH +1, "%s?id=%s&amp;logfile=%s", "super_log_data",  key, log_ptr->filename);
           snprintf(name, MAX_TMP_NAME, "log.%d.d_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", "super_delete_stat_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*/
  set_admin_page(&user, key, 0, "super_stat_list");  /* shared_super_menu.c */
  list_logfiles(key); 
  cs_cgi_display("super_stat_list", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

