#include <stdio.h>
#include <stdlib.h>             /* for rand() */
#include <string.h>
#include <dirent.h>             /* Directory information.       */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


#include "global.h"
#include "custom.h"		/* for date FORMAT strings */


#include "ClearSilver.h"
#include "grade.h"	


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_grade_book_view.h"


	
void
read_parameters (char *course, char *key)
{
    cs_get_required_parameter("crs", course, MAX_PATH);
    cs_get_required_parameter("id", key, MAX_KEY);

}


void send_grade_file(CONFIG_STRUCT *conf, SESSION *user)
{
    char path[MAX_PATH +1];
    struct stat statbuffer;
    char *buffer;
    FILE *fp;
    char header[] = "</HEAD>";
    char *ptr, *start, *end;
    char tag[MAX_NAME +1];
    char id[MAX_ID +1];

    *id = '\0';

    snprintf(path, MAX_PATH +1, "%s%s/%s/%s", conf->course_path, GRADE_BOOK_DIR, "gradequick", "gradequick.html");
    if(stat (path, &statbuffer))
	 cs_critical_error(ERR_NO_RELEASED_GRADES, "");
    buffer = (char *) malloc(statbuffer.st_size * sizeof(char));
    if(!buffer)
	 cs_critical_error(ERR_MALLOC_FAILED, "send grade file");

    fp = fopen(path, "r");
    if(!fp)
	 cs_critical_error(ERR_FOPEN_READ_FAILED, "send grade file");

    if(fread(buffer, statbuffer.st_size, 1, fp) != 1)
	 cs_critical_error(ERR_FREAD_FAILED, "send grade file");
    fclose(fp);

    cs_get_optional_parameter("student_id", id, MAX_ID);
    if(user->group == STUDENT && strlen(id))
	    cs_critical_error(ERR_REQUEST_DENIED, "");

    send_content_type();
    if(user->group == FACULTY && strlen(id) ==0)
      fwrite(buffer, statbuffer.st_size, 1, stdout);
    else
    {
        if(strlen(id) ==0)
             strncpy(id, user->id, MAX_ID +1);

        ptr = strstr(buffer, header);
	if(!ptr)
	   cs_critical_error(ERR_GENERAL_ERROR, "NO grade");
        ptr = ptr + strlen(header);
        snprintf(tag, MAX_NAME +1, "<%s>", id);
	start = strstr(ptr, tag);
	if(start)
           start = start + strlen(tag);

        snprintf(tag, MAX_NAME +1, "</%s>", id);
	end = strstr(ptr, tag);
	if(!start || !end)
	   cs_critical_error(ERR_GENERAL_ERROR, "NO grade");
      
        if(user->group == STUDENT)
	   handle_new_file(conf, user);	
        	
	fwrite(buffer, ptr - buffer, 1, stdout);
	fprintf(stdout, "%s", "<BODY>\n");
	fwrite(start, end - start, 1, stdout);
        fprintf(stdout, "%s", "</BODY>\n</html>\n");

    }
    free(buffer); 
}




void  do_action(CONFIG_STRUCT *conf, SESSION *user)
{

    	
    send_grade_file(conf, user);
}





int
main ()
{
  char course[MAX_PATH +1];
  char key[MAX_KEY +1];

  SESSION user;
  CONFIG_STRUCT conf;


  cs_cgi_init();    /*shared_cs_util.c */

  read_parameters (course, key);
  read_configuration_file (course, &conf);	
  validate_key (key, &user, &conf);	

  do_action(&conf, &user);
  
  cs_cgi_destroy();           
                              
                              

  return 0;		

}






