#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "ClearSilver.h"
#include "sqlite3.h"
#include "global.h"
#include "custom.h"		/* for date FORMAT strings */
#include "grade.h"		/* for date FORMAT strings */
#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_grade_book_menu.h"
#include "manhat-lib/shared_grade_book_util.h"
#include "manhat-lib/shared_access.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
set_one_entry(int i, char* value,  char *url)
{
    #define MAX_TEMP_LEN 30

    char name[MAX_TEMP_LEN +1];

    snprintf(name, MAX_TEMP_LEN +1, "grade_book_release.%d.name", i);
    cs_set_value( name, value); /* shared_cs_util.h */
    if(strlen(url))
    {
       snprintf(name, MAX_TEMP_LEN +1, "grade_book_release.%d.url", i);
       cs_set_value(name, url); /* shared_cs_util.h */
    }

 #undef MAX_TEMP_LEN 
}





int
has_released_grades(CONFIG_STRUCT *conf)
{
   char db_path[MAX_PATH+1];

   snprintf(db_path, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_STUDENT_DB);


   if(!file_exists(db_path))   /* shared_file_util.c */
      return 0;
   return 1;
}

int
has_grade_book_db(CONFIG_STRUCT *conf)
{
   char db_path[MAX_PATH+1];

   snprintf(db_path, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_DB);


   if(!file_exists(db_path))   /* shared_file_util.c */
      return 0;

   return 1;
}


void
set_menu(const char *course, const char *key, CONFIG_STRUCT *conf, int can_write)
{
    int i = 0; 
    char url[MAX_PATH + 1];
    int released;
    int has_db;
    RELEASE_CONF release_conf;
    char time_string[MAX_TIMESTRING +1];

    has_db = has_grade_book_db(conf);

    if(has_db)
	cs_set_int_value("grades_release_form", 1);    
	

    released = has_released_grades(conf); 

    if(released)
    {
	cs_set_int_value("grades_last_release_note", 1);    
	if(read_release_conf(conf, &release_conf))
	{
	   if(release_conf.view_bar)
	       cs_set_value("release.view_bar", "view_bar");
	   else 
	       cs_set_value("release.view_bar_no", "view_bar_no");

	   if(release_conf.view_total)
	       cs_set_value("release.view_total", "view_total");
	   else 
	       cs_set_value("release.view_total_no", "view_total_no");

	   if(release_conf.view_max)
	       cs_set_value("release.view_max", "view_max");
	   else 
	       cs_set_value("release.view_max_no", "view_max_no");
	   strftime(time_string, MAX_TIMESTRING, DOW_DATE_TIME_FORMAT,localtime(&(release_conf.released_time)));		
	   cs_set_value("released_time", time_string);
	}
    }
    
    if(can_write && released) 
        snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grades_delete_form", course, key);
    else
        url[0] = '\0';
    set_one_entry( i, "grades_delete_form", url);
    i++;





    if(can_write && released) 
           snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grade_book_std_csv", course, key);
    else
        url[0] = '\0';

    set_one_entry( i, "grades_webdownload", url);
    i++;




    if( released) 
        snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grades_info", course, key);
    else
        url[0] = '\0';
    set_one_entry( i,"grades_info", url);
    i++;




    if( released) 
           snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grade_book_std", course, key);
    else
        url[0] = '\0';
    set_one_entry( i, "grades_view", url);
    i++;




    if(released) 
        snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grades_view_form", course, key);
    else
        url[0] = '\0';
    set_one_entry( i, "grades_view_form", url);
    i++;
   



}


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

  SESSION user;
  CONFIG_STRUCT conf;
  
  cs_cgi_init();   /* shared_cs_util.h */
  read_parameters ( course, key);

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

  cs_set_course_info(&conf);  
  set_grade_book_menu("grade_book_release", key, course); /* shared_grade_book_menu.h */
  cs_load_lang("grade_book_menu_util");

  if(user.group != FACULTY)
	cs_critical_error(ERR_REQUEST_DENIED, "");

  can_write = has_write_permission(&user, &conf);
  set_menu(course, key, &conf, can_write);

  cs_cgi_display ("grade_book_release", 1);
  cs_cgi_destroy();      

  return 0;			

}
	
	
