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


#include "../global.h"
#include "shared_cs_util.h"
#include "shared_wnec_roster_log.h"
#include "shared_file_util.h"
#include "shared_lock.h"



static void
rotate_roster_logfile()
{
   char logfile[MAX_PATH +1];
   char new_filepath[MAX_PATH +1];
   char new_filename[MAX_FILENAME + 1];
   struct stat buf;
   char ch;
   FILE *fp;


   snprintf(logfile, MAX_PATH +1, "%s/%s", ROSTER_LOG_PATH, LOG_FNAME);
   if(stat(logfile, &buf) == 0)  /* if file exists */
   {
      if(buf.st_size > MAX_LOGIN_LOG_SIZE)   /* custom.h */
      {
        /* new filename contains the modification time DDMonYYYY of the current log file */
        strftime(new_filename, MAX_FILENAME + 1, "log_%d%b%Y_%H%M", localtime(&buf.st_mtime));
        snprintf(new_filepath,MAX_PATH + 1, "%s/%s", ROSTER_LOG_PATH, new_filename);
        
        /** If for some strange reason, new_filepath already exists, add an 'A' to the
        *** end of the filename, then begin search for a unique name 
        **/
            if(file_exists(new_filepath))
              {
                strcat(new_filepath,"A");
                for(ch = 'B'; file_exists(new_filepath) && (ch <= 'Z'); ch++)
                           *(new_filepath + strlen(new_filepath) - 1) = ch;
                           
                if(ch > 'Z')
                    return;   /* we just won't rotate the log if we can't get a unique filename */           
              }               

                                
   	 rename(logfile,new_filepath);
   	 fp = fopen(logfile, "a");   /* create an empty logfile */
   	 if(fp)
   	    fclose(fp);
      }
      
   }
}



FILE *open_roster_log()
{
  FILE *fp;
  char roster_path[MAX_PATH + 1]; 
   
   /* create the logs directory if it doesn't exist */
   if(!file_exists(ROSTER_LOG_PATH))  /* shared_file_util.c */
    {   
      if (mkdir (ROSTER_LOG_PATH, 0777))
        cs_critical_error (ERR_MKDIR_FAILED, ROSTER_LOG_PATH);
    }    

   snprintf(roster_path, MAX_PATH + 1, "%s/%s", ROSTER_LOG_PATH, LOG_FNAME);
   fp = fopen(roster_path, "a");
   if(!fp)
      cs_critical_error(ERR_FOPEN_WRITE_FAILED, roster_path);
   get_exclusive_lock(fileno(fp), 0);   /* shared_lock.c */
   
   return fp;
}   




void update_roster_log(const char *action,
                       const char *roster_filename,
                       const char* course_code,
                       const char* course_title,
                       int students_added,
                       int students_dropped,
                       const char *comments)
{


 FILE *fp;
 char timestring[MAX_TIMESTRING + 1];
 time_t now;
 
 now = time(NULL);

 rotate_roster_logfile();
 fp = open_roster_log();
 if(fp)
  {
   strftime(timestring, MAX_TIMESTRING + 1, "%a %m/%d/%Y %I:%M:%S %p", localtime(&now));
   fprintf(fp, "%s:%s", timestring, action);
   
   if(*course_code)
      fprintf(fp,":%s", course_code);
       
   if(*course_title)
      fprintf(fp,":%s", course_title);

   if(!strstr(action, ROSTER_CREATE_COURSE) ||
      !strstr(action, ROSTER_UPDATE_COURSE) )
    {
      if(students_added >= 0)
         fprintf(fp, ":%d added", students_added);

      if(students_dropped >= 0)
         fprintf(fp, ":%d dropped", students_dropped);
             
   }
   
   if(*roster_filename)
      fprintf(fp,":%s", roster_filename);
      
   if(*comments)
      fprintf(fp,":%s", comments);

   fprintf(fp,"\n");
   release_lock(fileno(fp));   /* shared_lock.c */
   fclose(fp);
 }  
           
}
 
 
