#include <stdio.h>
#include <time.h>

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

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_news_util.h"
#include "manhat-lib/shared_hotpot_exam_util.h"

SESSION user;
CONFIG_STRUCT conf;		/* the configuration read from config file */



static void
read_parameters (char *course, char *key, char *sender, char *info_fname, long *offset)
{
  char tmp[25];

  cs_get_required_parameter ("crs", course, MAX_FILENAME);	/* shared_cs_util.c */
  cs_get_required_parameter ("id", key, MAX_KEY);
  cs_get_required_parameter ("sender", sender, MAX_USERNAME);
  cs_get_required_parameter ("inf", info_fname, MAX_FILENAME);  
  cs_get_required_parameter ("loc", tmp, 24);  
  *offset = atol(tmp);

}



static void
set_warning_values(char *crs,  char *key,  char *sender,   char *info_fname, long offset,
                         GROUP_TYPE group, int access)
{
  char temp[MAX_PATH]; 
  
  cs_set_value("crs", crs);
  cs_set_value("id", key);
  cs_set_value("sender", sender);
  cs_set_value("inf", info_fname);
  
  snprintf(temp, MAX_PATH, "%ld", offset);
  cs_set_value("loc", temp);

  if(access == ACCESS_CAPTURE)   /* capturing - provide a url */
   {
    snprintf(temp, MAX_PATH, "%s?crs=%s&amp;id=%s&amp;sender=%s&amp;inf=%s&amp;loc=%ld",
                            "hotpot_exam_present", crs, key, sender,info_fname, offset);
    cs_set_value("capture_url", temp);
   }                         
  
  if(group == FACULTY)
	  cs_set_int_value("is_faculty", 1);

}



static void
set_verify_page_values(const char *course_path, const char *username,  char *crs,  char *key, 
                                           char *sender,  char *info_fname, long offset, GROUP_TYPE group,
                                          int access)
{
 time_t time_read = 0;
 char timestring[MAX_TIMESTRING + 1];
 char flag_path[MAX_PATH + 1];
 FILE *fp;
 int fd;
 CENTRAL_INBOX_RECORD data;


 if(group != FACULTY)  /* teachers can view the test as often as they like */
 {
  get_hotpot_score_path(course_path, username, sender, info_fname,flag_path); /* shared_hotpot_exam_util.c */

  fp = fopen(flag_path,"r");
  if(fp)
   {
    fd = fileno(fp);
    get_shared_lock(fd,1);   /* shared_lock.c */
    if(fread(&time_read, sizeof(time_t),1,fp) != 1)
       time_read = 0;
    release_lock(fd);    
    fclose(fp);
  } 
   
  if(time_read)
    { 
       strftime (timestring, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,localtime (&time_read));
       cs_set_value("already_read_time",timestring); 
    }
  else /* check to see if this quiz has been marked read only.*/
    {
       set_discussion_names (course_path, username, ASSIGNMENTS);   /* shared_news_util.c */
       read_msg_record(offset, &data);                              /* shared_news_util.c */
     
       /* BASIC_STATUS is #defined in global.h */
       if(BASIC_STATUS(data.status) == READ_ONLY)
           cs_critical_error(ERR_MSG_IS_READ_ONLY, "");
    }
 }
   
  /* still here? */

    set_warning_values(crs, key, sender, info_fname,offset, group, access);

}



int
main (void)
{
  char course[MAX_PATH + 1];	        /* from crs=? command line */
  char key[MAX_KEY + 1];	        /* from id=? command line */
  char sender[MAX_USERNAME + 1];	/* from sender=? command line - which teacher posted this exam? */
  char info_fname[MAX_FILENAME + 1];    /* from inf=? cmd line */
  long offset;                          /* from loc=? cmd line */

  cs_cgi_init();  
  read_parameters (course, key, sender, info_fname, &offset);

  read_configuration_file (course, &conf);	/* shared_util.c */
  validate_key (key, &user, &conf);		/* shared_util.c */

  cs_set_course_info(&conf);			/* shared_cs_util.c */
  cs_set_current_time(); 			/* shared_cs_util.c */

  set_verify_page_values(conf.course_path, user.username, course,key, 
  				sender,info_fname, offset, user.group, conf.access);
  cs_cgi_display("hotpot_exam_verify", 1);
  cs_cgi_destroy(); 
  return 0;
}
