#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_person_list.h"
#include "manhat-lib/shared_grade_book_release.h"
#include "manhat-lib/shared_copy_file.h"



void
read_parameters ( char *course, char *key, int *save_opt)
{
     char temp[MAX_KEY +1];

     cs_get_required_parameter("crs", course, MAX_PATH);
     cs_get_required_parameter("id", key, MAX_KEY);
     cs_get_required_parameter("save_option", temp, MAX_KEY);
     if(strcmp(temp, "save") == 0)
        *save_opt = 1;
     else
        *save_opt = 0;

}







void
deliver_new_file_to_students(CONFIG_STRUCT *conf, P_NODE *head)
{
   P_NODE *ptr;
   for(ptr = head; ptr; ptr = ptr->next)
   {
        if(ptr->data.group != FACULTY && *(ptr->data.realname) != '*')
          write_student_newfile (ptr->data.username, conf);
   }
}

void create_release_conf(CONFIG_STRUCT *conf)
{
    RELEASE_CONF release_conf;
    char path[MAX_PATH +1];
    FILE *fp;

    release_conf.view_bar = hdf_get_int_value(global_cgi->hdf, "Query.view_bar", 0);
    release_conf.view_total = hdf_get_int_value(global_cgi->hdf, "Query.view_total", 0);
    release_conf.view_max = hdf_get_int_value(global_cgi->hdf, "Query.view_max", 0);
    release_conf.released_time = time(NULL);

    snprintf(path, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_RELEASE_CONF_FNAME);
    fp = fopen(path, "w");
    if(!fp)
    {
        snprintf(path, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_STUDENT_DB);
	unlink(path);
	cs_critical_error(ERR_FOPEN_WRITE_FAILED, "create release conf");
    }
    if(fwrite(&release_conf, sizeof(RELEASE_CONF), 1, fp) != 1)
    {
        snprintf(path, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_STUDENT_DB);
	unlink(path);
	cs_critical_error(ERR_FWRITE_FAILED, "create release conf");
    }
    fclose(fp);

}

void copy_db(CONFIG_STRUCT *conf)
{
    char source_file[MAX_PATH +1];
    char dest_file[MAX_PATH +1];
    snprintf(source_file, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_DB);
    snprintf(dest_file, MAX_PATH +1, "%s%s/%s", conf->course_path, GRADE_BOOK_DIR, GRADE_BOOK_STUDENT_DB);
    copy_file (source_file, dest_file);
    create_release_conf(conf);
}


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

  SESSION user;
  CONFIG_STRUCT conf;

  P_NODE *head;

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

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

  next_url = hdf_get_value(global_cgi->hdf, "Query.nextUrl", "");
  if(strlen(next_url) ==0)
     cs_critical_error(ERR_PARAM_MISSING, "nextUrl");

  if(save == 0)
    printf("Location: %s\n\n", next_url);
  else
  {  
     head = build_option_person_list(&conf);
     copy_db(&conf);
     delete_redfiles (head, &conf); 
     deliver_new_file_to_students(&conf, head);
     free_option_person_list(head);
     printf("Location: %s\n\n", next_url);
  }
  cs_cgi_destroy();                   /** shared_cs_util.c */ 

  return 0;			/* exit successfully */

}

