#include <string.h>
#include <stdlib.h>
#include <time.h>


#include "../global.h"
#include "../custom.h"

#include "shared_strtrm.h"
#include "shared_cs_util.h"
#include "shared_lock.h"
#include "shared_central_log.h"
#include "shared_cs_util.h"


typedef struct podcast_log_record {
       char info_fname[MAX_FILENAME + 1];
       time_t download_time;
       char remote_host[MAX_PATH + 1];
       char remote_address[MAX_PATH + 1];
       char user_agent[MAX_PATH + 1];
       } PODCAST_LOG_RECORD;
       
       



static 
void get_existing_podcast_key(const char *path, char *podcast_key)
{
 FILE *fp;

 *podcast_key = '\0';
 
 fp =fopen(path, "r");
 if(fp)
  {
   fgets(podcast_key, MAX_KEY + 1, fp);
   fclose(fp);
   strtrm(podcast_key);
  } 
}




static 
void create_new_podcast_key(const char *key_path, char *podcast_key)
{
 FILE *fp;
 int i;
 
   srand ((int) time (NULL));
  

  for (i = 0; i < MAX_KEY; i++)
      podcast_key[i] = 97 + (int) (26.0 * rand () / (RAND_MAX + 1.0));
  podcast_key[i] = '\0';

 
  fp = fopen(key_path, "w");
  if(!fp)
     cs_critical_error(ERR_FOPEN_WRITE_FAILED, key_path);
  fprintf(fp, "%s", podcast_key);
  fclose(fp);   
 
} 


void get_podcast_key(CONFIG_STRUCT *conf, char *podcast_key)
{
 char key_path[MAX_PATH + 1];
 
 snprintf(key_path, MAX_PATH + 1, "%s%s/%s/subscribe_key.txt",
                    conf->course_path,
                    INBOXES_DIR,
                    PODCASTS_DIR);

 get_existing_podcast_key(key_path, podcast_key);

 if(!*podcast_key)
   create_new_podcast_key(key_path, podcast_key);

}



void  
validate_podcast_key(const char *podcast_key, CONFIG_STRUCT *conf)
{
 char key_path[MAX_PATH + 1];
 char correct_key[MAX_KEY + 1];
 
 snprintf(key_path, MAX_PATH + 1, "%s%s/%s/subscribe_key.txt",
                    conf->course_path,
                    INBOXES_DIR,
                    PODCASTS_DIR);

 get_existing_podcast_key(key_path, correct_key);
  
 if(strcmp(correct_key, podcast_key))
    cs_critical_error(ERR_REQUEST_DENIED,"");
    
}

  


int is_valid_podcast_file_extension(char *ext)
{
    
    return !strcasecmp(ext, "mp3") ||     /* mp3 audio file */
           !strcasecmp(ext, "mov") ||    /* quicktime video */
           !strcasecmp(ext, "m4a") ||    /* audio/mpeg */
           !strcasecmp(ext, "mp4") ||    /* video/mp4 */
           !strcasecmp(ext, "m4v") ||    /* video/mp4 */
           !strcasecmp(ext, "m4b");      /* audio mpeg with bookmarks - for extended podcasts */
           
}



static 
void get_podcast_download_log_path(const char *course_path, char *path)
{

  snprintf (path, MAX_PATH +1, "%s%s/%s/%s", 
        course_path,
        INBOXES_DIR,
        PODCASTS_DIR,
        PODCAST_DOWNLOAD_LOG_FNAME);

}




void log_podcast_download(const char *info_fname, CONFIG_STRUCT *conf)
{
  FILE *fp;
  int fd;
  char log_path[MAX_PATH +1];
  PODCAST_LOG_RECORD record;

  get_podcast_download_log_path(conf->course_path, log_path);
 
  /* set CGI.RemoteAddress and CGI.RemoteHost */
  lookup_remote_host();   /* shared_central_log.c */
  
  
        
  fp = fopen(log_path, "a+");
  if(!fp)
	  cs_critical_error(ERR_FOPEN_APPEND_FAILED, log_path);
  fd = fileno(fp);
  get_exclusive_lock(fd, 1);   /* shared_lock.c */

  strncpy(record.info_fname,info_fname, MAX_FILENAME + 1);
  *(record.info_fname + MAX_FILENAME) = '\0';
  
  record.download_time = time(NULL);
  
  strncpy(record.remote_host, hdf_get_value(global_cgi->hdf,"CGI.RemoteHost","???"),  MAX_PATH + 1);
  *(record.remote_host + MAX_PATH) = '\0';
  
  
  strncpy(record.remote_address, hdf_get_value(global_cgi->hdf,"CGI.RemoteAddress","???"),  MAX_PATH + 1);
  *(record.remote_address + MAX_PATH) = '\0';

  strncpy(record.user_agent, hdf_get_value(global_cgi->hdf,"HTTP.UserAgent","???"),  MAX_PATH + 1);
  *(record.user_agent + MAX_PATH) = '\0';
  
  fwrite(&record, 1, sizeof(PODCAST_LOG_RECORD), fp);

  release_lock(fd);  /*shared_lock.c */
  fclose(fp);
}



void
set_podcast_download_log_data(const char *info_fname, CONFIG_STRUCT *conf)
{
 char log_path[MAX_PATH + 1];
 FILE *fp;
 PODCAST_LOG_RECORD record;
 int i = 0;
 char name[100];
 char timestring[MAX_TIMESTRING + 1];
 
   
  get_podcast_download_log_path(conf->course_path, log_path);
  fp = fopen(log_path, "r");
  if(fp)
   {
    get_shared_lock(fileno(fp), 0);
    while(fread(&record, sizeof(PODCAST_LOG_RECORD),1, fp) == 1)
      {
        if(!strcmp(info_fname, record.info_fname))
         {
          snprintf(name, 100, "podcast_log.%d.time", i);
          strftime(timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT, localtime(&record.download_time));
          cs_set_value(name, timestring);

          snprintf(name, 100, "podcast_log.%d.info_fname", i);
          cs_set_value(name,record.info_fname);
          
          snprintf(name, 100, "podcast_log.%d.remote_host", i);
          cs_set_value(name, record.remote_host);
          
          snprintf(name, 100, "podcast_log.%d.remote_address", i);
          cs_set_value(name, record.remote_address);
          
          snprintf(name, 100, "podcast_log.%d.user_agent", i);
          cs_set_value(name, record.user_agent);
          
          i++;
         } 
      }    
     release_lock(fileno(fp));   /* shared_lock.c */
     fclose(fp);
    }      
}          



