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

#include "ClearSilver.h"

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

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_copy_tab_file.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_person_list.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, "item.%d.name", i);
    cs_set_value( name, value); /* shared_cs_util.h */
    if(strlen(url))
    {
       snprintf(name, MAX_TEMP_LEN +1, "item.%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];

   if(conf->grades == EXCEL_TAB_FILE_METHOD)   /* grade.h */     
      snprintf(db_path, MAX_PATH +1, "%s%s/%s", conf->course_path, "grade_book", GRADES_TAB_FNAME);
   else if(conf->grades == 3)   /* grade.h */
      snprintf(db_path, MAX_PATH +1, "%s%s/%s", conf->course_path, "grade_book", "gradequick");


   if(!file_exists(db_path))   /* shared_file_util.c */
   {
      if(conf->grades == EXCEL_TAB_FILE_METHOD)   /* grade.h */     
      {
	   if(!copy_tab_file(conf))
		  return 0;
	   if(!file_exists(db_path))
               return 0;
	   else
	       return 1;
      } 
      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;

    cs_set_back_url(course, key, "main_menu");  /* shared_cs_util.c */
    released = has_released_grades(conf); 
    
    
    
    if(conf->grades == EXCEL_TAB_FILE_METHOD)      /* #defined in grade.h */
    {
      cs_set_int_value("using_excel_method", 1);
      if(can_write)
          snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grades_upload_form", course, key);
      else
         url[0] = '\0';
       set_one_entry( i,"grades_upload_form", url);
       i++;
    }
    else
    {
      cs_set_int_value("using_excel_method", 0);      
      if(can_write)
          snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "gradequick_upload_form", course, key);
      else
         url[0] = '\0';
       set_one_entry( i,"gradequick_upload_form", url);
       i++;
    }
    
    
    
    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) 
    {
	if(conf->grades == EXCEL_TAB_FILE_METHOD)   /* defined in grade.h */
           snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grades_download", course, key);
	else
           snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grade_csv", course, key);

    }
    else
        url[0] = '\0';





    if(conf->grades == EXCEL_TAB_FILE_METHOD)  /* defined in grade.h */
       set_one_entry( i, "grades_download", 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) 
    {
        if(conf->grades == EXCEL_TAB_FILE_METHOD)     /* defined in grade.h */
           snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "grades_view", course, key);
        else
           snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s",  "gradequick_view", 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++;
   



    if(conf->grades == EXCEL_TAB_FILE_METHOD)   /* #defined in grade.h */
    {
       if(can_write)
        snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;go=yes",  "admin_download_passwd", course, key);
       else
        url[0] = '\0';

        set_one_entry( i, "admin_download_passwd" ,url);
    }
    else   /* #defined in grade.h */
    {
       if(can_write)
        snprintf(url, MAX_PATH +1, "%s?crs=%s&amp;id=%s&amp;go=yes&amp;csv=2",  "admin_download_passwd", course, key);
       else
        url[0] = '\0';

        set_one_entry( i, "gradequick_fixlength" ,url);


    }
}


	
	
	
	

	

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.c */

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

  cs_set_course_info(&conf);    	/* shared_cs_util.c */
  cs_set_current_time();    		/* shared_cs_util.c */
  validate_key (key, &user, &conf);	
  
  if( (user.group != FACULTY) || (conf.grades == 0) )
       cs_critical_error(ERR_REQUEST_DENIED, "");   /* shared_cs_util.h */
  else
  {
     can_write = has_write_permission(&user, &conf);    /* shared_access.h */
     set_menu(course, key, &conf, can_write);
     cs_cgi_display ("grades_fac_menu", 1);             /* shared_cs_util.h */
     cs_cgi_destroy();                                  /* shared_cs_util.h */ 
  }


  return 0;			/* exit successfully */

}




