
#include <stdio.h>
#include <sys/file.h>      
#include <string.h>
#include <time.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_survey_string.h"
#include "manhat-lib/shared_login_logs.h"
#include "manhat-lib/shared_cs_util.h"


typedef struct wnec_roster_log
    {

    char sub_action[MAX_PATH *2 +1];
	struct wnec_roster_log *next;

    }WNEC_ROSTER_LOG;

typedef struct wnec_list
{
    char action[MAX_PATH *2 +1];
    WNEC_ROSTER_LOG *head_seq;
    WNEC_ROSTER_LOG *cur_seq;
    struct wnec_list *next;

}WNEC_LIST;





void
free_log_data(WNEC_LIST *head)
{
     WNEC_LIST *ptr, *start;
	 WNEC_ROSTER_LOG *wptr;

    if(head)
    {
        start = head->next;

        while(start)
        {
	       ptr = start->next;
           while(start->head_seq)
		   {
		      wptr = start->head_seq->next;
			  free(start->head_seq);
			  start->head_seq = wptr;
		   }
		   free(start);
	       start = ptr;
        }
       free(head);
    }
}
      




WNEC_LIST *
build_data_list( const char *logfile)
{
	FILE *fp;
	char log_file[MAX_PATH +1]; 
	char line[MAX_PATH*2 +1];
	char *lptr, *lptr2 =0;
    WNEC_LIST *head = 0, *current =0, *ptr=0, *tail =0;
    WNEC_ROSTER_LOG *wptr;
	int end = 0;

        
        	   
	snprintf(log_file, MAX_PATH +1, "%s/%s", ROSTER_LOG_PATH, logfile);
	fp = fopen(log_file, "r");
    if(fp)
    {

	    head = (WNEC_LIST *)malloc(sizeof(WNEC_LIST)); /* dummy head */
	    head->next =0;
	    head->head_seq =0;
	    head->cur_seq =0;

	    get_shared_lock(fileno(fp), 1);
	    while(fgets(line, MAX_PATH *2, fp))
	    {
	       strtrm(line);
	       ptr = (WNEC_LIST *)malloc(sizeof(WNEC_LIST)); /* dummy head */
           strncpy(ptr->action, line, MAX_PATH * 2 +1);
		   ptr->next = 0;
		   ptr->cur_seq = 0;
		   ptr->head_seq = 0;

           lptr = strrchr(line, ':');
		   if(lptr)
		      lptr2 = strstr(lptr, "Start");

           
		   if(lptr2)
		   {
		       end = 0;
			   do
			   {
	              if(fgets(line, MAX_PATH *2, fp))
			      { 

	                  strtrm(line);

	                  wptr = (WNEC_ROSTER_LOG *)malloc(sizeof(WNEC_ROSTER_LOG)); 
			          wptr->next = 0;
				      strncpy(wptr->sub_action, line, MAX_PATH *2 +1);

					  if(!ptr->head_seq)
					       ptr->head_seq = wptr;
					  else
					      ptr->cur_seq->next = wptr;
					  ptr->cur_seq = wptr;

                      lptr = strrchr(line, ':');
		              if(lptr)
				      {
		                    lptr2 = strstr(lptr, "End");
							if(lptr2)
					             end = 1;
				      }
				 }
		                else
				   break;	
			   }while(end  != 1);
		   }


	       if(!tail)
	       {
		     tail = ptr;
		     current = ptr;
		     head->next = ptr;
	       }
	       else
	       {
		     head->next = ptr;
		     ptr->next = current;
		     current = head->next;
	       }
	  }

  }
  release_lock(fileno(fp));
  fclose(fp);
  return head;
}

void
set_data_list(WNEC_LIST *head)
{
   #define MAX_TMP_NAME 50
   char name[MAX_TMP_NAME];
   WNEC_LIST *ptr;
   int i = 0, j=0;

    if(head)
    {
      for(ptr = head->next; ptr; ptr = ptr->next)
      {
	      snprintf(name, MAX_TMP_NAME, "record.%d.action", i);
          cs_set_value(name,  ptr->action);
		  if(ptr->head_seq)
		  {
		     j = 0;
			 for(ptr->cur_seq = ptr->head_seq; ptr->cur_seq; ptr->cur_seq = ptr->cur_seq->next)
			 {
	            snprintf(name, MAX_TMP_NAME, "record.%d.sub.%d.action", i, j);
                cs_set_value(name,  ptr->cur_seq->sub_action);
			    j++; 
			 }

		  }
          i++;	
      }
    }

   #undef MAX_TMP_NAME 
}
        
void
print_one_log_data(const char *logfile)
{
    WNEC_LIST *head;
    
    head = build_data_list(logfile);
    set_data_list(head);
    free_log_data(head);
}



			
void
read_parameters( char *key, char *logfile, char *del)
{
     cs_get_required_parameter ("logfile", logfile, MAX_FILENAME);
     cs_get_required_parameter ("id", key, MAX_KEY);
     cs_get_optional_parameter ("del", del, MAX_KEY);
}
		
void   delete_log_file(const char *logfile)
{
     char path[MAX_PATH +1];
	 snprintf(path, MAX_PATH +1, "%s/%s", ROSTER_LOG_PATH, logfile);
     unlink(path);

}
		


int main()
{
    char key[MAX_KEY +1];
    char logfile[MAX_FILENAME +1];
	char delete[MAX_KEY +1];


    SESSION user;
    cs_cgi_init();
    read_parameters( key, logfile, delete);
    validate_super_key(key, &user);
    if(strlen(delete)> 0)
	{
	   delete_log_file(logfile);
	   cs_set_int_value("delete", 1);
	   cs_cgi_display("super_roster_log_list", 1);
	}
	else
	{
       print_one_log_data(logfile);

	   cs_set_int_value("show_detail", 1);
       cs_cgi_display("super_roster_log_list", 1);
    }
    cs_cgi_destroy();
    return 0;
}
